标签:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34898 Accepted Submission(s): 15109
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #include<set> #include<vector> using namespace std; #define INF 1<<30 vector<int> e[10010],w[10010]; queue<int> q; int n,m,dist[110],vis[110]; void spfa() { int x; while(!q.empty()) q.pop(); memset(vis,0,sizeof(vis)); vis[1]=1; q.push(1); while(!q.empty()) { x=q.front(),q.pop(); for(int i=0;i<e[x].size();i++) { if(dist[x]+w[x][i]<dist[e[x][i]]) { dist[e[x][i]]=dist[x]+w[x][i]; if(!vis[e[x][i]]) vis[e[x][i]]=1,q.push(e[x][i]); } } vis[x]=0; } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; for(int i=0;i<=10000;i++) e[i].clear(),w[i].clear(); for(int i=1;i<=n;i++) dist[i]=INF; dist[1]=0; for(int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); e[x].push_back(y); e[y].push_back(x); w[x].push_back(z); w[y].push_back(z); } spfa(); printf("%d\n",dist[n]); } }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4238789.html