PAT 1087 All Roads Lead to Rome
题目:
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<=N<=200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N-1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format "City1 City2 Cost". Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.
Output Specification:
For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommended. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.
Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommended route. Then in the next line, you are supposed to print the route in the format "City1->City2->...->ROM".
Sample Input:6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1
Sample Output:
3 3 195 97
HZH->PRS->ROM
地址:http://pat.zju.edu.cn/contests/pat-a-practise/1087
用dijkstra求最短路径,比较麻烦的时在找节点时的判断标准。通常,我写dijkstra的模板是这样的:
- 根据已知信息初始化最短cost数组,比如这里的lcost lhapp ahapp等(42行~54行)
- 当未遍历到终点时一直进入循环(55行)
- 循环体里面先根据最短的cost数组找出当前的最短路径节点(56行~73行)
- 遍历该节点(74行)
- 通过该节点更新所有于该节点相连的邻接节点(75行~100行)
- 返回第二步继续循环
按照题目中判断最短路径的标准是,先比较cost,cost较小为最短路径,如果cost一样比较happiness,happiness越大路径越好,若happiness也一样,则比较平均happiness。另外,题目还要求计算出所有cost一样的路径个数,这样在第5步时,如果找到cost一样的路径时,要把路径个数叠加上去(体现在第86行代码)。代码:
1 #include <map>
2 #include <vector>
3 #include <stdio.h>
4 #include <string.h>
5 #include <string>
6 using namespace std;
7
8 const int N = 201;
9 int cost[N][N];
10 int happ[N];
11 bool istraved[N];
12 int lcost[N];
13 int lhapp[N];
14 int pathNum[N];
15 double ahapp[N];
16 int main()
17 {
18 int n, k;
19 char str_in1[5];
20 char str_in2[5];
21 while(scanf("%d%d%s",&n,&k,str_in1) != EOF){
22 map<string,int> nodeNum;
23 vector<vector<int> >path(n+1,vector<int>());
24 memset(cost,0,sizeof(cost));
25 memset(happ,0,sizeof(happ));
26 memset(istraved,0,sizeof(istraved));
27 memset(lcost,0,sizeof(lcost));
28 memset(lhapp,0,sizeof(lhapp));
29 memset(pathNum,0,sizeof(pathNum));
30 memset(ahapp,0,sizeof(ahapp));
31 nodeNum[str_in1] = 1;
32 for(int i = 2; i < n+1; ++i){
33 scanf("%s%d",str_in1,&happ[i]);
34 nodeNum[str_in1] = i;
35 }
36 for(int i = 0; i < k; ++i){
37 scanf("%s%s",str_in1,str_in2);
38 scanf("%d",&cost[nodeNum[str_in1]][nodeNum[str_in2]]);
39 cost[nodeNum[str_in2]][nodeNum[str_in1]] = cost[nodeNum[str_in1]][nodeNum[str_in2]];
40
41 }
42 int s = 1;
43 int e = nodeNum["ROM"];
44 istraved[s] = true;
45 for(int i = 1; i <= n; ++i){
46 lcost[i] = cost[s][i];
47 if(cost[s][i]){
48 lhapp[i] = happ[i];
49 ahapp[i] = happ[i];
50 pathNum[i] = 1;
51 path[i].push_back(s);
52 }
53 }
54 pathNum[s] = 1;
55 while(!istraved[e]){
56 int mincost = 0x7fffffff;
57 int maxhapp = -1;
58 double maxahapp = -1;
59 int pos = 0;
60 for(int i = 1; i <= n; ++i){
61 if(!istraved[i] && lcost[i]){
62 if(
63 mincost > lcost[i] ||
64 (mincost == lcost[i] && maxhapp < lhapp[i]) ||
65 (mincost == lcost[i] && maxhapp == lhapp[i] && maxahapp < ahapp[i])
66 ){
67 mincost = lcost[i];
68 maxhapp = lhapp[i];
69 maxahapp = ahapp[i];
70 pos = i;
71 }
72 }
73 }
74 istraved[pos] = true;
75 for(int i = 1; i <= n; ++i){
76 if(!istraved[i] && cost[pos][i]){
77 if(lcost[i] == 0 || lcost[i] > lcost[pos] + cost[pos][i]){
78 lcost[i] = lcost[pos] + cost[pos][i];
79 lhapp[i] = lhapp[pos] + happ[i];
80 pathNum[i] = pathNum[pos];
81 path[i].clear();
82 path[i].insert(path[i].end(),path[pos].begin(),path[pos].end());
83 path[i].push_back(pos);
84 ahapp[i] = (lhapp[i] * 1.0) / path[i].size();
85 }else if(lcost[i] == lcost[pos] + cost[pos][i]){
86 pathNum[i] += pathNum[pos];
87 if(
88 lhapp[i] < lhapp[pos] + happ[i] ||
89 lhapp[i] == lhapp[pos] + happ[i] && ahapp[i] < (lhapp[i]*1.0)/(path[pos].size() + 1)
90 ){
91
92 lhapp[i] = lhapp[pos] + happ[i];
93 path[i].clear();
94 path[i].insert(path[i].end(),path[pos].begin(),path[pos].end());
95 path[i].push_back(pos);
96 ahapp[i] = (lhapp[i] * 1.0) / path[i].size();
97 }
98 }
99 }
100 }
101
102 }
103 printf("%d %d %d ",pathNum[e],lcost[e],lhapp[e]);
104 if(path[e].empty())
105 printf("0\n");
106 else
107 printf("%d\n",lhapp[e]/path[e].size());
108 for(int i = 0; i < path[e].size(); ++i){
109 map<string,int>::iterator it = nodeNum.begin();
110 for(it; it != nodeNum.end(); ++it){
111 if(it->second == path[e][i]){
112 printf("%s->",it->first.c_str());
113 }
114 }
115 }
116 printf("ROM\n");
117 }
118 return 0;
119 }