Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42585 Accepted Submission(s): 18653
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2
AC-code:
#include<cstdio> #define max 0x3f3f3f3f #define min(a,b) (a>b?b:a) int n,dis[105],cost[105][105]; void dijkstra(int a) { int i,vis[105]; for(i=1;i<=n;i++) { dis[i]=max; vis[i]=0; } dis[a]=0; while(1) { int v=-1; for(i=1;i<=n;i++) if(!vis[i]&&(v==-1||dis[i]<dis[v])) v=i; if(v==-1) break; vis[v]=1; for(i=1;i<=n;i++) dis[i]=min(dis[i],dis[v]+cost[v][i]); } } int main() { int m,i,j,a,b,c; while(scanf("%d%d",&n,&m),n||m) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) cost[i][j]=max; for(i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); cost[a][b]=cost[b][a]=min(cost[a][b],c); } dijkstra(1); printf("%d\n",dis[n]); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/lin14543/article/details/47723125