http://pat.zju.edu.cn/contests/pat-practise/1030


1030. Travel Plan (30)
时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output
0 2 3 3 40
1、城市从0开始编号;
2、dis初始化;
3、读入数据的准备。
4、优先队列默认是大顶堆。

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<memory.h>  
  4. #include<algorithm>  
  5. #include<cstring>  
  6. #include<queue>  
  7. #include<cmath>  
  8. #include<vector>  
  9. #include<cstdlib>  
  10. #include<cmath>  
  11. #include<iomanip>  
  12. #include<string>  
  13. #include<map>  
  14.   
  15. using namespace std;  
  16. #define MAX 0xFFFFFFF  
  17. int mapd[501][501];  
  18. int mapp[501][501];  
  19. int dis[501];  
  20. int ps[501];  
  21. int pre[501];  
  22. struct Node{  
  23.     int dis;  
  24.     int p;  
  25.     int th;  
  26.     bool operator<(const Node & node)const{  
  27.         if(dis!=node.dis)  
  28.             return dis>node.dis;  
  29.         return p>node.p;  
  30.     }  
  31. };  
  32.   
  33. int main(){  
  34.   
  35.     //freopen("in.txt", "r", stdin);  
  36.     int n, m, s, d;  
  37.     priority_queue<Node> q;  
  38.     cin>>n>>m>>s>>d;  
  39.   
  40.   
  41.     for(int i=0;i<n;++i)  
  42.         for(int j=0;j<n;++j){  
  43.             mapd[i][j] = MAX;  
  44.             mapp[i][j] = MAX;  
  45.             dis[i] = MAX;  
  46.         }  
  47.   
  48.     int a, b, dd, c;  
  49.     for(int i=0;i<m;++i){  
  50.         scanf("%d%d%d%d", &a, &b, &dd, &c);  
  51.         if(mapd[a][b]==MAX || mapd[a][b]!=MAX && dd<mapd[a][b]){  
  52.             mapd[a][b] = mapd[b][a] = dd;  
  53.             mapp[a][b] = mapp[b][a] = c;  
  54.         }else if(mapd[a][b]!=MAX && dd==mapd[a][b] && c<mapp[a][b]){  
  55.             mapp[a][b] = mapp[b][a] = c;  
  56.         }  
  57.     }  
  58.   
  59.     dis[s] = 0;  
  60.     Node node;  
  61.     node.dis = 0;  
  62.     node.p = 0;  
  63.     node.th = s;  
  64.     q.push(node);  
  65.     while(!q.empty()){  
  66.         Node node = q.top();  
  67.         q.pop();  
  68.         for(int i=0;i<n;++i){  
  69.             if(mapd[node.th][i]!=MAX && dis[node.th]+mapd[node.th][i]<dis[i]){  
  70.                 dis[i] = dis[node.th]+mapd[node.th][i];  
  71.                 ps[i] = ps[node.th] + mapp[node.th][i];  
  72.                 Node tmp;  
  73.                 tmp.dis = dis[i];  
  74.                 tmp.th = i;  
  75.                 tmp.p = ps[node.th] + mapp[node.th][i];  
  76.                 pre[i] = node.th;  
  77.                 q.push(tmp);  
  78.             }else if(mapd[node.th][i]!=MAX && dis[node.th]+mapd[node.th][i]==dis[i] && ps[node.th]+mapp[node.th][i]<ps[i]){  
  79.                 ps[i] = ps[node.th]+mapp[node.th][i];  
  80.                 Node tmp;  
  81.                 tmp.dis = dis[i];  
  82.                 tmp.th = i;  
  83.                 tmp.p = ps[i];  
  84.                 pre[i] = node.th;  
  85.                 q.push(tmp);  
  86.             }  
  87.         }  
  88.     }  
  89.   
  90.     vector<int> ans;  
  91.     int now = d;  
  92.     while(now!=s){  
  93.         ans.push_back(now);  
  94.         now = pre[now];  
  95.     }  
  96.     ans.push_back(s);  
  97.   
  98.     printf("%d", ans[ans.size()-1]);  
  99.     for(int i=ans.size()-2;i>=0;i--){  
  100.         printf(" %d", ans[i]);  
  101.     }  
  102.     printf(" %d %d\n", dis[d], ps[d]);  
  103.     //fclose(stdin);  
  104.      
  105. }  
  106.           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值