标签:des style color os io for ar 问题
3 2 1 2 5 6 2 3 4 5 1 3 0 0
9 11#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #define MX 100000 using namespace std; int map1[1005][1005], map2[1005][1005]; int p[1005],num1[1005],num2[1005]; int s,t,n; void dijkstra() { int min,v,w,next,sum=0,k,i; memset(p,0,sizeof(p)); for(v=1;v<=n;v++) { num1[v]=num2[v]=MX; } num1[s]=num2[s]=0; p[s]=1; for(i=1,v=s;i<=n;i++) { for(w=1;w<=n;w++) { if(!p[w]&&map1[v][w]!=MX) { if(num1[v]<num1[w]-map1[v][w]) { num1[w]=num1[v]+map1[v][w]; num2[w]=num2[v]+map2[v][w]; } else if(num1[v]==num1[w]-map1[v][w]) { if(num2[v]<num2[w]-map2[v][w]) { num2[w]=num2[v]+map2[v][w]; } } } } min=MX; for(w=1;w<=n;w++) { if(!p[w]&&num1[w]<min) { v=w; min=num1[w]; } } p[v]=1; } // cout<<num1[t]<<" "<<num2[t]<<endl; printf("%d %d\n",num1[t],num2[t]); } int main() { int m, x, y, z, v; while (scanf("%d%d",&n,&m)) { if(n==0&&m==0)break; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { map1[i][j] = MX; map2[i][j] = MX; } for (int i = 0; i < m; i++) { // cin >> x >> y >> z >> v; scanf("%d%d%d%d",&x,&y,&z,&v); if (z < map1[x][y]) { map1[x][y] = map1[y][x] = z; map2[x][y] = map2[y][x] = v; } else if (z == map1[x][y]) { if (v < map1[x][y]) { //map1[x][y] = map1[y][x] = z; map2[x][y] = map2[y][x] = v; } } } // cin>>s>>t; scanf("%d%d",&s,&t); dijkstra(); } return 0; }
最短路径问题(Dijkstra),布布扣,bubuko.com
标签:des style color os io for ar 问题
原文地址:http://blog.csdn.net/zhangweiacm/article/details/38517607