标签:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34764 Accepted Submission(s): 15062
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<cstdlib> #include<string> using namespace std; #define INF 1<<30 int dist[110],a[110][110],n,m,vis[110]; void Dijkstra() { int minn,pos; for(int i=1;i<=n;i++) dist[i]=INF; for(int i=1;i<=n;i++) dist[i]=a[1][i]; dist[1]=0,vis[1]=1; for(int i=1;i<=n;i++) { minn=INF; for(int j=1;j<=n;j++) if(!vis[j]&&dist[j]<minn) { minn=dist[j]; pos=j; } vis[pos]=1; for(int j=1;j<=n;j++) { if(!vis[j]&&dist[pos]+a[pos][j]<dist[j]) dist[j]=dist[pos]+a[pos][j]; } } } int main() { while(scanf("%d%d",&n,&m)) { memset(vis,0,sizeof(vis)); if(n==0&&m==0) break; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) a[i][j]=INF; a[i][i]=0; } for(int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); if(z<a[x][y]) a[x][y]=a[y][x]=z; } Dijkstra(); printf("%d\n",dist[n]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4229158.html