标签:fine define chmod stc 迪杰斯特拉 space NPU otto 现在
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=2544
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 83843 Accepted Submission(s): 36272
#include<bits/stdc++.h> using namespace std; #define max_v 105 #define INF 99999 int edge[max_v][max_v]; int n,m; int used[max_v]; int dis[max_v]; void init() { memset(used,0,sizeof(used)); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { edge[i][j]=INF; } dis[i]=INF; } } void Dijkstra(int s) { for(int i=1; i<=n; i++) { dis[i]=edge[s][i]; } dis[s]=0; for(int i=1; i<=n; i++) { int index,mindis=INF; for(int j=1; j<=n; j++) { if(used[j]==0&&dis[j]<mindis) { mindis=dis[j]; index=j; } } used[index]=1; for(int j=1; j<=n; j++) { if(dis[index]+edge[index][j]<dis[j]) { dis[j]=dis[index]+edge[index][j]; } } } } int main() { while(~scanf("%d %d",&n,&m)) { if(n==0&&m==0) break; if(n==1) { printf("0\n"); continue; } init(); for(int i=0; i<m; i++) { int a,b,c; scanf("%d %d %d",&a,&b,&c); if(edge[a][b]>c)//预防重边 { edge[a][b]=edge[b][a]=c; } } Dijkstra(1); printf("%d\n",dis[n]); } }
标签:fine define chmod stc 迪杰斯特拉 space NPU otto 现在
原文地址:https://www.cnblogs.com/yinbiao/p/9197559.html