标签:
题目链接:
Time Limit: 1000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
#include <bits/stdc++.h> using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=0x3f3f3f3f; const int N=1100+5; int t,s,d; int dis[N][N],p[N],vis[N]; queue<int>qu; void spfa() { mst(vis,0); mst(p,inf); qu.push(0); vis[0]=1; p[0]=0; while(!qu.empty()) { int fr=qu.front(); qu.pop(); Riop(1010) { if(p[i]>dis[fr][i]+p[fr]) { p[i]=dis[fr][i]+p[fr]; if(!vis[i]) { qu.push(i); vis[i]=1; } } } vis[fr]=0; } } int main() { while(scanf("%d%d%d",&t,&s,&d)!=EOF) { mst(dis,inf); Riop(N-100)dis[i][i]=0; int u,v,w,a; Riop(t) { scanf("%d%d%d",&u,&v,&w); dis[u][v]=dis[v][u]=min(dis[u][v],w); } Riep(s) { scanf("%d",&a); dis[a][0]=dis[0][a]=0; } spfa(); int ans=inf; Riep(d) { scanf("%d",&a); // cout<<p[a]<<"#"<<endl; ans=min(ans,p[a]); } printf("%d\n",ans); } return 0; }
附上一个看一次就能彻底理解spfa的地址:
http://www.360doc.com/content/13/1208/22/14357424_335569176.shtml
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5472258.html