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这道题相对于A1111来说要简单一点,解题思路一致,注意的点就是用两个map来转换地名和数字。#include<iostream>#include<string>#include<vector>#include<stack>#include<map>#include<algorithm>using namespace std;const int maxv = 210;const int INF = 0x7fffffff;int n, m;int G[maxv][maxv];int happy[maxv];bool vis[maxv];int d[maxv];string start;map<string, int> CityToInt;map<int, string> IntToCity;stack<int > Path;vector<int > Pre[maxv];void init() { fill(G[0], G[0] + maxv*maxv, INF); fill(happy, happy + maxv, 0); fill(vis, vis + maxv, false); fill(d, d + maxv, INF);}void Dijkstra(int s) { 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[v] > d[u] + G[u][v]) { d[v] = d[u] + G[u][v]; Pre[v].clear(); Pre[v].push_back(u); } else if (d[v] == d[u] + G[u][v]) { Pre[v].push_back(u); } } } }}int totalPath = 0;int leastCost = INF;int mostHappy = 0;int avgHappy = 0;void DFS(int s) { static stack<int >path; static int C; static int H; if (s == CityToInt[start]) { totalPath++; leastCost = C; path.push(s); if (H > mostHappy) { mostHappy = H; Path = path; path.pop(); } } else { H += happy[s]; for (int i = 0;i < Pre[s].size();i++) { C += G[Pre[s][i]][s]; path.push(s); DFS(Pre[s][i]); path.pop(); C -= G[Pre[s][i]][s]; } H -= happy[s]; }}int main() { init(); cin >> n >> m; string str1,str2; int cost; for (int i = 0;i < n;i++) { if (i == 0) { cin >> start; CityToInt[start] = i; IntToCity[i] = start; } else { cin >> str1 >> cost; CityToInt[str1] = i; IntToCity[i] = str1; happy[i] = cost; } } for (int i = 0;i < m;i++) { cin >> str1 >> str2 >> cost; G[CityToInt[str1]][CityToInt[str2]] = G[CityToInt[str2]][CityToInt[str1]]= cost; } Dijkstra(CityToInt[start]); DFS(CityToInt["ROM"]); avgHappy = mostHappy / ((Path.size()-1)*1.0); cout << totalPath << ' ' << leastCost << ' ' << mostHappy << ' ' << avgHappy << endl; int len = Path.size(); for (int i = 0;i < len;i++) { if (i != len - 1) cout << IntToCity[Path.top()] << "->"; else cout << IntToCity[Path.top()] << endl; Path.pop(); } return 0;}
新闻热点
疑难解答