zoukankan      html  css  js  c++  java
  • P1339 [USACO09OCT]热浪Heat Wave[spfa板子]

    不存在的惯例放个题目链接https://www.luogu.org/problem/P1339

    点有2500个,边只有6200个,是稀疏图,于是决定学一下spfa

    spfa板子题,直接上代码了

    心得就是稍微学了一下stl()

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<vector>
     6 #include<queue>
     7 using namespace std;
     8 const int maxn = 2550;
     9 const int INF = 9999999;
    10 struct node{
    11     int v;//终点
    12     int weight;//权值
    13 };
    14 vector<node>mp[maxn];//下标是起点
    15 int dis[maxn],vis[maxn];//dis是起点到下标的长,vis判断是否入队过
    16 void spfa(int src){
    17     int q;
    18     queue<int>Q;
    19     dis[src]=0;
    20     vis[src]=1;
    21     Q.push(src);
    22     while(!Q.empty()){
    23         q=Q.front();
    24         Q.pop();
    25         vis[q]=0;
    26         for(int i=0;i<mp[q].size();i++){
    27             if(dis[q]+mp[q][i].weight<dis[mp[q][i].v]){
    28                 dis[mp[q][i].v]=dis[q]+mp[q][i].weight;
    29                 if(!vis[mp[q][i].v]){
    30                     Q.push(mp[q][i].v);
    31                     vis[mp[q][i].v]=1;
    32                 }
    33             }
    34         }
    35     }
    36     return;
    37 }
    38 
    39 int main(){
    40     int t,c,ts,te;
    41     cin>>t>>c>>ts>>te;
    42     for(int i=1;i<=t;i++)
    43         dis[i]=INF;
    44     while(c--){
    45         int a,b,tmp;
    46         scanf("%d %d %d",&a,&b,&tmp);
    47         mp[a].push_back((node){b,tmp});
    48         mp[b].push_back((node){a,tmp});
    49     }
    50     spfa(ts);
    51     printf("%d
    ",dis[te]);
    52     return 0;
    53 }
  • 相关阅读:
    方法
    数组
    Scanner类+Random
    运算符2
    运算符1
    Linux中Oracle的安装
    redis安装常见错误
    redis常用命令
    Linux中redis安装
    修改Oracle字符集
  • 原文地址:https://www.cnblogs.com/yoshinaripb/p/11318270.html
Copyright © 2011-2022 走看看