标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20555 Accepted Submission(s):
6098
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #define M 1005 5 #define MAX 0x3f3f3f3f 6 using namespace std; 7 int map[M][M],vis[M],dis[M],money[M][M],r[M]; 8 int main() 9 { 10 int n,m,i,j,s,t; 11 while(~scanf("%d%d",&n,&m)) 12 { 13 if(!n&&!m) break; 14 memset(vis,0,sizeof(vis)); 15 memset(dis,0,sizeof(dis)); 16 memset(r,0,sizeof(r)); 17 for(i=1; i<=n; i++) 18 for(j=1; j<=n; j++) 19 { 20 if(i==j) map[i][j]=0,money[i][j]=0; 21 else map[i][j]=MAX,money[i][j]=MAX; 22 } 23 int a,b,c,d; 24 while(m--) 25 { 26 scanf("%d%d%d%d",&a,&b,&c,&d); 27 if(map[a][b]>c) 28 { 29 map[a][b]=c,map[b][a]=c; 30 money[a][b]=d,money[b][a]=d; 31 } 32 } 33 scanf("%d%d",&s,&t); 34 vis[s]=1; 35 for(i=1; i<=n; i++) 36 { 37 dis[i]=map[s][i]; 38 r[i]=money[s][i]; 39 } 40 int w,min; 41 for(i=1; i<=n; i++) 42 { 43 min=MAX; 44 for(j=1; j<=n; j++) 45 if(!vis[j]&&min>dis[j]) 46 { 47 min=dis[j]; 48 w=j; 49 } 50 vis[w]=1; 51 for(j=1; j<=n; j++) 52 { 53 if(!vis[j]&&map[w][j]<MAX) 54 { 55 if(dis[j]>dis[w]+map[w][j]) 56 { 57 dis[j]=dis[w]+map[w][j]; 58 r[j]=r[w]+money[w][j]; 59 } 60 else if(dis[j]==dis[w]+map[w][j] && r[j]>r[w]+money[w][j]) //路程相同时,花费的处理 61 r[j]=r[w]+money[w][j]; 62 } 63 } 64 } 65 printf("%d %d\n",dis[t],r[t]); 66 } 67 return 0; 68 }
标签:
原文地址:http://www.cnblogs.com/pshw/p/5370880.html