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

仅用dijstra：

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 #include "stdio.h" #include "math.h" #include "string.h" #include "iostream" //#include "stdlib.h" #include "vector" #include "set" #include "map" //#include "stack" #include "queue" #include "algorithm" using namespace std; const int MAXV = 410;//最大顶点数 const int INF = 1000000000;//无穷大 //n 城市数目, k 边数, G 邻接矩阵, w 点权 //d[]最短距离,dw[]最大点权,num[]最短路径条数 //pt[]记录最短路径上的顶点数,pre[]记录前驱 int n, k, G[MAXV][MAXV], weight[MAXV]; int d[MAXV], w[MAXV], num[MAXV], pt[MAXV], pre[MAXV]; bool vis[MAXV] = {false}; map<string, int> cityToIndex; map<int, string> indexToCity; void Dijkstra(int s){ fill(d, d + MAXV, INF); memset(w, 0, sizeof(w)); memset(num, 0, sizeof(num)); memset(pt, 0, sizeof(pt)); for (int i = 0; i < n; i++) pre[i] = i;//前驱初始化 d[s] = 0; w[s] = 0; num[s] = 1; for (int i = 0; i < n; i++) {//循环n次 int u = -1, MIN = INF; for (int j = 0; j < n; j++) { if (vis[j] == false && d[j] < MIN) { u = j; MIN = d[j]; } } if (u == -1) return; vis[u] = true; for (int v = 0; v < n; v++) { if (vis[v] == false && G[u][v] != INF) { if (d[u] + G[u][v] < d[v]) { d[v] = d[u] + G[u][v];//优化d[v] w[v] = w[u] + weight[v]; num[v] = num[u]; pt[v] = pt[u] + 1; pre[v] = u; }else if (d[u] + G[u][v] == d[v]){ num[v] += num[u];//加最短路径条数 if (w[u] + weight[v] > w[v]) { w[v] = w[u] + weight[v]; pt[v] = pt[u] + 1; pre[v] = u; }else if(w[u] + weight[v] == w[v]){ double uAvg = 1.0 * (w[u] + weight[v]) / (pt[u] + 1); double vAvg = 1.0 * w[v] / pt[v]; if (uAvg > vAvg) { pt[v] = pt[u] + 1; pre[v] = u; } } } } } } } void printPath(int v){ if (v == 0) { cout << indexToCity[v]; return; } printPath(pre[v]); cout <<"->"<<indexToCity[v]; } int main(){ string start, str1, str2; cin >> n >> k >> start; cityToIndex[start] = 0; indexToCity[0] = start; for (int i = 1; i <= n - 1; i++) { cin >> str1 >> weight[i];//读入happy值，点权 cityToIndex[str1] = i; indexToCity[i] = str1; } fill(G[0], G[0] + MAXV * MAXV, INF);//初始化图G for (int i = 0; i < k; i++){ cin >> str1 >> str2; int c1 = cityToIndex[str1], c2 = cityToIndex[str2]; cin >> G[c1][c2]; G[c2][c1] = G[c1][c2];//无向边 } Dijkstra(0); int rom = cityToIndex["ROM"]; printf("%d %d %d %d\n", num[rom], d[rom], w[rom], w[rom]/pt[rom]); printPath(rom); return 0; }

dijstra + dfs:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 #include "stdio.h" #include "math.h" #include "string.h" #include "iostream" //#include "stdlib.h" #include "vector" #include "set" #include "map" //#include "stack" #include "queue" #include "algorithm" using namespace std; const int MAXV = 410;//最大顶点数 const int INF = 1000000000;//无穷大 //n 城市数目, k 边数, G 邻接矩阵, w 点权 //d[]最短距离,numPath记录最短路径条数 //maxW记录最大点权之和，maxAvg为最大平均点权 int n, k, G[MAXV][MAXV], weight[MAXV]; int d[MAXV], numPath = 0, maxW = 0; double maxAvg = 0; bool vis[MAXV] = {false}; vector<int> pre[MAXV]; vector<int> tempPath, path; map<string, int> cityToIndex; map<int, string> indexToCity; void Dijstra(int s){//起点s fill(d, d + MAXV, INF); d[s] = 0; for (int i = 0; i < n; i++) { int u = -1, MIN = INF; for (int j = 0; j < n; j++) { if (vis[j] == false && d[j] < MIN) { u = j; MIN = d[j]; } } if (u == -1) return; vis[u] = true; for (int v = 0; v < n; v++) { if (vis[v] == false && G[u][v] != INF) { if (d[u] + G[u][v] < d[v]) { d[v] = d[u] + G[u][v]; pre[v].clear(); pre[v].push_back(u); } else if(d[u] + G[u][v] == d[v]){ pre[v].push_back(u); } } } } } void DFS(int v){ if (v == 0) { tempPath.push_back(v); numPath++; int tempW = 0;//临时路径tempPath的点权之和 for (int i = tempPath.size() - 2; i >= 0; i--) { int id = tempPath[i]; tempW += weight[id]; } double tempAvg = 1.0 * tempW / (tempPath.size() - 1); if (tempW > maxW) {//当前点权和更大 maxW = tempW; maxAvg = tempAvg; path = tempPath; }else if (tempW == maxAvg && tempAvg > maxAvg){ maxAvg = tempAvg; path = tempPath; } tempPath.pop_back(); return; } tempPath.push_back(v); for (int i = 0; i < pre[v].size(); i++) { DFS(pre[v][i]); } tempPath.pop_back(); } int main(){ string start, city1, city2; cin >> n >> k >> start; cityToIndex[start] = 0; indexToCity[0] = start; for (int i = 1; i <= n -1; i++) { cin >> city1 >> weight[i]; cityToIndex[city1] = i; indexToCity[i] = city1; } fill(G[0], G[0] + MAXV * MAXV, INF); for (int i = 0; i < k; i++) { cin >> city1 >> city2; int c1 = cityToIndex[city1], c2 = cityToIndex[city2]; cin >> G[c1][c2]; G[c2][c1] = G[c1][c2]; } Dijstra(0); int rom = cityToIndex["ROM"]; DFS(rom); printf("%d %d %d %d\n", numPath, d[rom], maxW, (int)maxAvg); for (int i = path.size() - 1; i >= 0; i--) { cout << indexToCity[path[i]]; if (i > 0) { cout <<"->"; } } return 0; }