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 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
``````

# 解析

``````#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
const int INF = 0x3fffffff;
unordered_map<string, int> Name2index;
unordered_map<int, string> index2Name;
struct Node {
int v, cost;
Node() = default;
Node(int _v,int _cost):v(_v),cost(_cost){}
};
vector<vector<Node>> G;
vector<int> Happy;
vector<vector<int>> pre;
void dijkstra(int s,vector<int> & d) {
vector<bool> vis(G.size(), false);
pre.resize(G.size());
d.resize(G.size(), INF);
d[s] = 0;
for (int i = 0; i < G.size(); i++) {
int u = -1, min = INF;
for (int j = 0; j < G.size(); j++) {
if (vis[j] == false && min > d[j]) {
min = d[j];
u = j;
}
}
vis[u] = true;
for (auto x : G[u]) {
int v = x.v;
if (vis[v] == false) {
if (d[v] > d[u] + x.cost) {
pre[v].clear();
pre[v].push_back(u);
d[v] = d[u] + x.cost;
}
else if (d[v] == d[u] + x.cost) {
pre[v].push_back(u);
}
}
}
}
}
int num = 0;  //最短；路径条数
int opthappy=-1, avghappy=-1;
vector<int> temppath, path;
void DFS(int s, int v) {
if (s == v) {
temppath.push_back(v);
num++;
int H = 0;
for (int i = temppath.size() - 1; i >= 0; i--)
H += Happy[temppath[i]];
if (opthappy < H) {
opthappy = H;
avghappy = H / (temppath.size() - 1);
path = temppath;
}
else if (opthappy == H) {
H /= temppath.size() - 1;
if (H > avghappy) {
avghappy = H;
path = temppath;
}
}
temppath.pop_back();
}
temppath.push_back(v);
for (auto x : pre[v])
DFS(s, x);
temppath.pop_back();
}
int main()
{
int N, K;
scanf("%d %d", &N, &K);
string start;
cin >> start;
Name2index[start] = 0;
index2Name = start;
Happy.resize(N, 0);
G.resize(N);
string city;
int happy_value;
for (int i = 1; i < N; i++) {
cin >> city >> happy_value;
Name2index[city] = i;
index2Name[i] = city;
Happy[i] = happy_value;
}
string city1, city2;
int cost;
for (int i = 0; i < K; i++) {
cin >> city1 >> city2 >> cost;
G[Name2index[city1]].push_back(Node(Name2index[city2], cost));
G[Name2index[city2]].push_back(Node(Name2index[city1], cost));
}
vector<int> d;
dijkstra(0,d);
DFS(0, Name2index[string("ROM")]);
printf("%d %d %d %d\n", num, d[Name2index[string("ROM")]], opthappy, avghappy);
for (int i = path.size()-1; i >=0; i--) {
printf("%s", index2Name[path[i]].c_str());
if (i == 0)    printf("\n");
else	printf("->");
}
}
``````