链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27692 Accepted Submission(s): 10019
缺一则WA
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> #include <map> #define INF 1000000000 #define MAXX 210 int mp[MAXX][MAXX]; int visit[MAXX],d[MAXX]; void Dijkstra(int st,int n) { int i,j,y; memset(visit,0,sizeof(visit)); for(int i=0;i<n;i++) { d[i]=mp[st][i]; } d[st]=0; for(int i=0;i<n;i++) { int m=INF,x=1; for(y=0;y<n;y++) { if(!visit[y] && d[y]<=m) { m = d[x = y]; } } visit[x]=1; for(y=0; y<n; y++) { if(!visit[y] && d[y]>d[x]+mp[x][y]) { d[y]=d[x]+mp[x][y]; } } } } int main() { int n,m,i,j; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0; i<MAXX; i++) for(j=0; j<MAXX; j++) mp[i][j]=INF; int a,b,c,st,ed; for(i=0; i<m; i++) { scanf("%d%d%d",&a,&b,&c); mp[a][b]=mp[a][b]>c?c:mp[a][b]; mp[b][a]=mp[a][b]; } scanf("%d%d",&st,&ed); if(st == ed) { printf("0\n"); continue; } Dijkstra(st,n); if(d[ed] == INF) { printf("-1\n"); } else { printf("%d\n",d[ed]); } } return 0; }
hdu 1874(Dijkstra ),布布扣,bubuko.com
原文:http://www.cnblogs.com/ccccnzb/p/3890962.html