标签:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42716 Accepted Submission(s): 18715
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2
#include<cstdio> #include<cstring> #define max 110 #define INF 0x3f3f3f int n,map[max][max];//map记录两点间的最短路径 void dijkstra() { int dis[max];//记录起始点到当前点的最短路径 int visit[max];//标记是否访问过了 int i,j,next,min; memset(visit,0,sizeof(visit)); for(i=1;i<=n;i++) dis[i]=map[1][i]; visit[1]=1; for(i=2;i<=n;++i) { min=INF; for(j=1;j<=n;++j) { if(visit[j]==0&&min>dis[j]) { min=dis[j]; next=j; } } visit[next]=1; for(j=1;j<=n;j++) { if(visit[j]==0&&dis[j]>dis[next]+map[next][j]) dis[j]=dis[next]+map[next][j]; } } printf("%d\n",dis[n]); } int main() { int m,a,b,c; while(scanf("%d%d",&n,&m)&&n||m) { memset(map,INF,sizeof(map)); while(m--) { scanf("%d%d%d",&a,&b,&c); if(map[a][b]>c) map[a][b]=map[b][a]=c; } dijkstra(); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDOJ 2544 最短路(最短路径 dijkstra算法)
标签:
原文地址:http://blog.csdn.net/zwj1452267376/article/details/47729727