ACM暑假集训标准程序_Day5_1009

来源:互联网 发布:第四方支付程序源码 编辑:程序博客网 时间:2024/05/22 06:34

SPFA模板题

如果用临接矩阵存图,请注意判断重边,保留边权较小的那一条。


#include<queue>#include<iostream>#include<cstdio>#include<vector>#include<cmath>using namespace std;const int MAX=100005;int n,tp,i,s,m,e,w;const int MaxData=0x7fffff;struct edge_ {    int to,w;}edge,temp;vector<edge_>adjmap[MAX];int dis[MAX];int spfa(int s) {    queue<int>tag;    int i;    for(i=0; i<n+1; ++i) {        dis[i] = MaxData;    }    dis[s]=0;    tag.push(s);    int tp,to;    while(!tag.empty()){      tp=tag.front();      tag.pop();      for(int i=0;i<adjmap[tp].size();i++){        to=adjmap[tp][i].to;        if(dis[to]>dis[tp]+adjmap[tp][i].w){           dis[to]=dis[tp]+adjmap[tp][i].w;          tag.push(to);        }      }    }    return -1;}int main(){    int t;    cin>>t;  while(cin>>n>>m&&t--){      int from,to;      cin>>from>>to;    for(i=0;i<MAX;i++)    adjmap[i].clear();    for(i=0;i<m;i++){        scanf("%d%d%d",&s,&e,&w);        temp.to=s;        temp.w=w;        adjmap[e].push_back(temp);        temp.to=e;        adjmap[s].push_back(temp);    }        spfa(from);        if(dis[to]!=MaxData)            cout<<dis[to]<<endl;        else            cout<<"Poor,Jony."<<endl;  }}


原创粉丝点击