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
voiddfs(int cur, int dest, int cost, int happy, int unit, vector<int> path, int n){ if (cur == dest) { if (cost < final_cost) { final_cost = cost; final_unit = unit; final_happiness = happy; routes = 1; final_path = path; } elseif (cost == final_cost) { routes += 1; if (happy > final_happiness) { final_happiness = happy; final_unit = unit; final_path = path; } elseif (happy == final_happiness) { if (happy / unit > final_happiness / final_unit) { final_unit = unit; final_path = path; } } } return; } for (int i = 0; i < n; i++) { if (!isVisited[i] && cities[cur][i] != 0) { isVisited[i] = true; path.push_back(i); dfs(i, dest, cost + cities[cur][i], happy + happiness[i], unit + 1, path, n); path.pop_back(); isVisited[i] = false; } } }
voidprintPath(){ cout << itos[final_path[0]]; for (int i = 1; i < final_path.size(); i++) { cout << "->" << itos[final_path[i]]; } }
intmain(){ int n = 0, k = 0; string s; cin >> n >> k >> s; si[s] = 0; itos[0] = s; for (int i = 1; i < n; i++) { cin >> s >> happiness[i]; si[s] = i; itos[i] = s; } for (int i = 0; i < k; i++) { string a, b; cin >> a >> b; cin >> cities[si[a]][si[b]]; cities[si[b]][si[a]] = cities[si[a]][si[b]]; }