PAT-A-1087. All Roads Lead to Rome (30)
来源:互联网 发布:王家湾汉江公寓网络 编辑:程序博客网 时间:2024/05/22 07:52
1087. All Roads Lead to Rome (30)
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 HZHROM 100PKN 40GDN 55PRS 95BLN 80ROM GDN 1BLN ROM 1HZH PKN 1PRS ROM 2BLN HZH 2PKN GDN 1HZH PRS 1Sample Output:
3 3 195 97HZH->PRS->ROM
#include<iostream>#include<cstdio>#include<cstring>#include<map>#include<vector>#include<string>#include<algorithm>using namespace std;const int maxn = 220;const int inf = 1000000000;int n, m, st, g[maxn][maxn], weight[maxn];int d[maxn], numpath = 0, maxw = 0;double maxavg = 0;bool vis[maxn] = { false };vector<int> pre[maxn];vector<int> temp, path;map<int, string> inttostr;map<string, int> strtoint;void dijkstra(int s){ fill(d, d + maxn, 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) { temp.push_back(v); numpath++; int tempw = 0; for (int i = temp.size() - 2; i >= 0; i--) { tempw += weight[temp[i]]; } double tempavg = 1.0*tempw / (temp.size() - 1); if (tempw > maxw) { path = temp; maxw = tempw; maxavg = tempavg; } else if (tempw == maxw&&tempavg > maxavg) { path = temp; maxavg = tempavg; } temp.pop_back(); return; } temp.push_back(v); for (int i = 0; i < pre[v].size(); i++) { dfs(pre[v][i]); } temp.pop_back();}int main(){ string start, c1, c2; cin >> n >> m >> start; strtoint[start] = 0; inttostr[0] = start; for (int i = 1; i <= n - 1; i++) { cin >> c1 >> weight[i]; strtoint[c1] = i; inttostr[i] = c1; } fill(g[0], g[0] + maxn*maxn, inf); for (int i = 0; i < m; i++) { cin >> c1 >> c2; int id1 = strtoint[c1]; int id2 = strtoint[c2]; cin >> g[id1][id2]; g[id2][id1] = g[id1][id2]; } dijkstra(0); int rom = strtoint["ROM"]; dfs(rom); cout << numpath << " " << d[rom] << " " << maxw << " " << (int)maxavg << endl; for (int i = path.size() - 1; i >= 0; i--) { int id = path[i]; cout << inttostr[id]; if (i > 0) cout << "->"; } system("pause"); return 0;}
- PAT A 1087. All Roads Lead to Rome (30)
- PAT(A) - 1087. All Roads Lead to Rome (30)
- PAT-A-1087. All Roads Lead to Rome (30)
- Pat(A) 1087. All Roads Lead to Rome (30)
- PAT 1087. All Roads Lead to Rome
- 【PAT】1087. All Roads Lead to Rome
- pat 1087. All Roads Lead to Rome (30)
- PAT 1087. All Roads Lead to Rome (30)
- [PAT]1087. All Roads Lead to Rome (30)
- PAT 1087. All Roads Lead to Rome (30)
- 【PAT】1087. All Roads Lead to Rome (30)
- pat 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30) PAT
- pat 1087. All Roads Lead to Rome (30) 解答
- PAT甲级练习1087. All Roads Lead to Rome (30)
- 【PAT】【Advanced Level】1087. All Roads Lead to Rome (30)
- PAT 1087. All Roads Lead to Rome (30)
- PAT 甲级 1087. All Roads Lead to Rome (30)
- C语言程序设计(39)
- 关于水仙花数
- js原型链(2)
- 连连看(外观较简陋)
- 第41章 离散时间信号与系统 z 域分析
- PAT-A-1087. All Roads Lead to Rome (30)
- 敏捷开发,QA面临的10个挑战
- 从源码角度带你分析 Android View 事件分发 dispatchTouchEvent,onTouch,onTouchEvent,onClick逻辑顺序过程
- 深入 char * ,char ** ,char a[ ] ,char *a[] 内核
- strom 分布式的实时计算系统
- shader开始之旅
- Python中遇到IndentationError
- 将文件隐藏到图片
- PHP-可逆加密解密算法总结