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.

## 模板： Dijkstra+dfs，本题只需要加一步城市名string和标号int的相互转换。另外在计算平均happiness时，起点不计入分母。

#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
const int maxn = 300;
const int inf = 0x3fffffff;//不要设成0x7fffffff,否则相加时可能溢出
unordered_map<string, int>StI;
unordered_map<int, string>ItS;
vector<vector<int>>pre;
vector<int>temp, path;
int graph[maxn][maxn], happy[maxn], dis[maxn], mark[maxn];
int n, m, maxHappy = 0, route = 0;
string start;

void dijistra(int s) {
dis[s] = 0;
for (int i = 0; i < n; i++) {
int min = inf, pos = -1;
for (int j = 0; j < n; j++) {
if (mark[j] == 0 && dis[j] < min) {
pos = j;
min = dis[j];
}
}
if (pos == -1)return;
mark[pos] = 1;
for (int j = 0; j < n; j++) {
if (mark[j] == 0) {
if (dis[j] > dis[pos] + graph[pos][j]) {
dis[j] = dis[pos] + graph[pos][j];
pre[j].clear();
pre[j].push_back(pos);
}
else if (dis[j] == dis[pos] + graph[pos][j])
pre[j].push_back(pos);
}
}
}
}

void dfs(int e) {
if (e == 0) {
route++;
temp.push_back(0);
int sumHappy = 0;
for (int i = 0; i < temp.size(); i++)
sumHappy += happy[temp[i]];
if ((sumHappy > maxHappy)||((sumHappy==maxHappy)&&(sumHappy/temp.size()>maxHappy/path.size()))) {
maxHappy = sumHappy;
path = temp;
}
temp.pop_back();
return;
}
temp.push_back(e);
for (int i = 0; i < pre[e].size(); i++) {
dfs(pre[e][i]);
}
temp.pop_back();
}

int main() {
cin >> n >> m >> start;
pre.resize(n);
for (int i = 0; i < n; i++) {
happy[i] = 0;
dis[i] = inf;
mark[i] = 0;
for (int j = 0; j < n; j++)graph[i][j] = inf;
}
StI[start] = 0;
ItS = start;
happy = 0;
for (int i=1; i < n; i++) {
string name;
int h;
cin >> name >> h;
StI[name] = i;
ItS[i] = name;
happy[i] = h;
}
for (int i = 0; i < m; i++) {
string x, y;
int d;
cin >> x >> y >> d;
graph[StI[x]][StI[y]] = graph[StI[y]][StI[x]] = d;
}
dijistra(0);
dfs(StI["ROM"]);
cout << route << " " << dis[StI["ROM"]] << " " << maxHappy << " " << maxHappy / (path.size() - 1) << endl;
for (int i = path.size() - 1; i >= 0; i--) {
cout << ItS[path[i]];
if (i != 0)cout << "->";
else
cout << endl;
}
return 0;
} ©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客 