Source:
Description:
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), 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 Klines 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 alwaysROM
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 recommanded. 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 recommanded 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
Keys:
Attention:
- 结点编号从1开始,因为映射判定的条件是结点编号==0
Code:
1 /* 2 Data: 2019-06-18 15:47:57 3 Problem: PAT_A1087#All Roads Lead to Rome 4 AC: 37:38 5 6 题目大意: 7 从起点至终点ROM花费最少且最幸福的一条路, 8 若不唯一,给出平均幸福指数最高的路径 9 10 输入: 11 第一行给出,城市数N,路径数K,起点城市 12 接下来N-1行,城市名,幸福指数 13 接下来K行,v1,v2,cost 14 输出: 15 最短路径数目,花费,幸福指数,平均幸福指数(整数部分,除去起点) 16 起点至终点的路径 17 */ 18 #include<cstdio> 19 #include<vector> 20 #include<string> 21 #include<iostream> 22 #include<map> 23 #include<algorithm> 24 using namespace std; 25 const int M=210,INF=1e9; 26 int grap[M][M],vis[M],d[M],w[M]; 27 int n,s,t,pt=1,cnt=0,optValue=0,optAvg=0; 28 map<string,int> mp; 29 string city[M],ROM="ROM"; 30 vector<int> pre[M],temp,opt; 31 32 int Str(string s) 33 { 34 if(mp[s]==0) 35 { 36 mp[s]=pt; 37 return pt++; 38 } 39 else 40 return mp[s]; 41 } 42 43 void Dijskra(int st) 44 { 45 fill(d,d+M,INF); 46 fill(vis,vis+M,0); 47 d[st]=0; 48 for(int i=1; i<=n; i++) 49 { 50 int u=-1,Min=INF; 51 for(int j=1; j<=n; j++) 52 { 53 if(vis[j]==0 && d[j]<Min) 54 { 55 u = j; 56 Min = d[j]; 57 } 58 } 59 if(u==-1) return; 60 vis[u]=1; 61 for(int v=1; v<=n; v++) 62 { 63 if(vis[v]==0 && grap[u][v]!=INF) 64 { 65 if(d[u]+grap[u][v] < d[v]) 66 { 67 d[v] = d[u] + grap[u][v]; 68 pre[v].clear(); 69 pre[v].push_back(u); 70 } 71 else if(d[u]+grap[u][v] == d[v]) 72 pre[v].push_back(u); 73 } 74 } 75 } 76 } 77 78 void DFS(int v) 79 { 80 if(v == s) 81 { 82 cnt++; 83 int value=0; 84 for(int i=temp.size()-1; i>=0; i--) 85 value += w[temp[i]]; 86 if(value > optValue) 87 { 88 optValue = value; 89 optAvg = value/temp.size(); 90 opt = temp; 91 } 92 else if(value == optValue) 93 { 94 if(value/temp.size() > optAvg) 95 { 96 optAvg = value/temp.size(); 97 opt = temp; 98 } 99 } 100 return; 101 } 102 103 temp.push_back(v); 104 for(int i=0; i<pre[v].size(); i++) 105 DFS(pre[v][i]); 106 temp.pop_back(); 107 } 108 109 int main() 110 { 111 #ifdef ONLINE_JUDGE 112 #else 113 freopen("Test.txt", "r", stdin); 114 #endif // ONLINE_JUDGE 115 116 fill(grap[0],grap[0]+M*M,INF); 117 fill(w,w+M,0); 118 119 string name; 120 int v1,v2,m; 121 scanf("%d%d", &n,&m); 122 cin >> name; 123 s = Str(name); 124 city[s]=name; 125 for(int i=1; i<n; i++) 126 { 127 cin >> name; 128 v1 = Str(name); 129 city[v1]=name; 130 if(name==ROM) 131 t = v1; 132 scanf("%d", &w[v1]); 133 } 134 for(int i=0; i<m; i++) 135 { 136 cin >> name; 137 v1 = Str(name); 138 cin >> name; 139 v2 = Str(name); 140 scanf("%d", &grap[v1][v2]); 141 grap[v2][v1]=grap[v1][v2]; 142 } 143 Dijskra(s); 144 DFS(t); 145 printf("%d %d %d %d\n", cnt,d[t],optValue,optAvg); 146 cout << city[s]; 147 for(int i=opt.size()-1; i>=0; i--) 148 cout << "->" << city[opt[i]]; 149 150 return 0; 151 }