1087 All Roads Lead to Rome

题目

1087 All Roads Lead to Rome (30)(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:

1
2
3
4
5
6
7
8
9
10
11
12
13
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:

1
2
3 3 195 97
HZH->PRS->ROM

想法

很经典的一道最短路径的题目

由于加入了场景,字符串的处理等,稍微烦一点

需要记录的东西比较多,需要记录路径、cost、happiness、经过的节点数

路径可以记录前驱


费了好大劲,处理完各种各样的要求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// Created by Zhao Xiaodong on 2018/8/14.
//
#include <iostream>
#include <queue>
#include <vector>
#include <climits>
#include <algorithm>
#include <unordered_map>
#include <string>
using namespace std;
int main() {
int N, K;
string start;
start.reserve(3);
cin >> N >> K >> start;
vector<string> name(N);
vector<int> happy(N);
unordered_map<string, int> id;
name[0] = start;
id[start] = 0;
happy[0] = 0;
for (int i = 1; i < N; i++) {
string s;
s.reserve(3);
int h;
cin >> s >> h;
name[i] = s;
id[s] = i;
happy[i] = h;
}
vector<vector<int>> m(N, vector<int>(N, INT_MAX));
for (int i = 0; i < K; i++) {
string a, b;
a.reserve(3);
b.reserve(3);
int cost;
cin >> a >> b >> cost;
m[id[a]][id[b]] = m[id[b]][id[a]] = cost;
}
vector<bool> visited(N, false);
vector<int> prev(N, -1);
vector<int> cost(N, INT_MAX);
vector<int> hop(N, INT_MAX);
vector<int> selections(N, 0);
vector<int> happiness(N, 0);
selections[0] = 1;
cost[0] = 0;
hop[0] = 0;
for (int k = 0; k < N; k++) {
int minCost = INT_MAX;
int curr = -1;
for (int j = 0; j < N; j++) {
if (!visited[j] && cost[j] < minCost) {
minCost = cost[j];
curr = j;
}
}
visited[curr] = true;
for (int i = 0; i < N; i++) {
if (!visited[i] && m[curr][i] < INT_MAX) {
if (selections[i] == 0 || cost[i] == m[curr][i] + cost[curr]) {
selections[i] += selections[curr];
}
if (cost[i] > m[curr][i] + cost[curr] ||
(cost[i] == m[curr][i] + cost[curr] && happiness[i] < happy[i] + happiness[curr]) ||
(cost[i] == m[curr][i] + cost[curr] && happiness[i] == happy[i] + happiness[curr] &&
hop[i] > hop[curr] + 1)
) {
prev[i] = curr;
cost[i] = m[curr][i] + cost[curr];
happiness[i] = happy[i] + happiness[curr];
hop[i] = hop[curr] + 1;
}
}
}
}
int endIdx = id["ROM"];
vector<int> prevChain;
int p = endIdx;
while (prev[p] >= 0) {
prevChain.push_back(prev[p]);
p = prev[p];
}
reverse(prevChain.begin(), prevChain.end());
cout << selections[endIdx] << " " << cost[endIdx] << " " << happiness[endIdx] << " "
<< (happiness[endIdx] == 0 ? 0 : ((int) ((double) happiness[endIdx] / hop[endIdx]))) << "\n";
for (int i = 0; i < prevChain.size(); i++) {
cout << name[prevChain[i]] << "->";
}
cout << "ROM";
return 0;
}

还有一个测试点没有过!!

不解。

TODO..