标签:des style blog http color java os io
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 28050 Accepted Submission(s): 10157
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<stdlib.h> 5 #include<algorithm> 6 using namespace std; 7 const int MAXN=2000+10; 8 const int INF=0x3f3f3f3f; 9 int dis[MAXN],vis[MAXN],w[MAXN][MAXN]; 10 int n,m,s,e; 11 void dijkstra(int s) 12 { 13 memset(vis,0,sizeof(vis)); 14 for(int i=0;i<n;i++) 15 dis[i]=INF; 16 dis[s]=0;//从起点开始走起 17 for(int i=0;i<n;i++) 18 { 19 int temp,maxn=INF; 20 for(int j=0;j<n;j++) 21 { 22 if(!vis[j]&&dis[j]<maxn) 23 { 24 maxn=dis[j]; 25 temp=j; 26 } 27 } 28 vis[temp]=1; 29 for(int i=0;i<n;i++) 30 dis[i]=min(dis[i],dis[temp]+w[temp][i]); 31 } 32 } 33 int main() 34 { 35 //freopen("in.txt","r",stdin); 36 while(scanf("%d %d",&n,&m)!=EOF) 37 { 38 for(int i=0;i<n;i++) 39 for(int j=0;j<n;j++) 40 w[i][j]=INF; 41 int star,en,val; 42 for(int i=0;i<m;i++) 43 { 44 scanf("%d %d %d",&star,&en,&val); 45 if(val<w[star][en])//防止重边 46 w[star][en]=w[en][star]=val; 47 } 48 scanf("%d %d",&s,&e); 49 dijkstra(s); 50 if(dis[e]==INF) 51 printf("-1\n"); 52 else 53 printf("%d\n",dis[e]); 54 } 55 return 0; 56 }
HDU 1874 畅通工程续 (最短路),布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/clliff/p/3904968.html