0%

1087 All Roads Lead to Rome

题意

输入格式

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.

输出格式

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:

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

思路:

我与其他主要有两点不同,一是用pass number,即通过的城市个数来等价代替了average glad参数。 因为遍历的时候average glad值不好根据上一个城市来递推,但pass number值可以。

第二个不同是没有将相同cost的路径存到一个二维数组中再进行后处理,我直接在Dijkstra算法中判断cost, glad, pass number参数,迭代结束生成的即为最优路径。关于总路径数的问题,我用一个diffpath变量,在迭代中根据父节点的情况来递推子节点的路径数。

具体见代码:

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<iostream>
#include<vector>
#include<string>
#include<utility>
using namespace std;
class City {
public:
string name="";
int glad=0;
int index = -1;
vector <pair<int, int>> link;
};
vector<City> c(26 * 26 * 26);
const int INFINITE = 100000;
int hah(string s) {
int result=0;
result += 26 * 26 * (s[0] - 'A');
result += 26 * (s[1] - 'A');
result += (s[2] - 'A');
return result;
}
int main() {
int n,k;
string temps;
int temp;
cin >> n >> k >> temps;
int start = hah(temps);
vector<int> myc;
myc.push_back(start);
c[start].name = temps;
c[start].index = 0;
for (int i = 1; i < n; i++) {
cin >> temps >> temp;
c[hah(temps)].name = temps;
c[hah(temps)].glad = temp;
myc.push_back(hah(temps));
c[hah(temps)].index = i;
}
string temps1, temps2;
for (int i = 0; i < k; i++) {
cin >> temps1 >> temps2 >> temp;
int temp1 = hah(temps1);
int temp2 = hah(temps2);
c[temp1].link.push_back({temp2,temp });
c[temp2].link.push_back({ temp1,temp });
}
int count;
vector<int> collect(n,0);
vector<int> path(n, -1);
vector<int> dist(n+1, INFINITE);
vector<int> glad(n+1, 0);
vector<double> passnum(n+1,INFINITE);
vector<int> diffpath(n, 0);
dist[0] = 0;
passnum[0] = 0;
diffpath[0] = 1;
while (1) {
int v=n;
for (int i = 0; i < n; i++) {
if (collect[i]) continue;
if (dist[i] < dist[v]) {
v = i;
}
else if (dist[i] == dist[v]) {
if (glad[i] > glad[v]) v = i;
else if (glad[i] == glad[v]) {
if (passnum[i] < passnum[v]) v = i;
}
}
}
if (v == n) break;
collect[v] = 1;
int now = myc[v];
for (int i = 0; i < c[now].link.size(); i++) {
auto cty = c[now];
if (collect[c[cty.link[i].first].index]) continue;
auto nextcty = c[cty.link[i].first];
int cost = cty.link[i].second;
if (dist[nextcty.index] > dist[cty.index] + cost) {
//刷新路径数为父节点的路径数
diffpath[nextcty.index] = diffpath[cty.index];
dist[nextcty.index] = dist[cty.index] + cost;
path[nextcty.index] = cty.index;
glad[nextcty.index] = glad[cty.index] + nextcty.glad;
passnum[nextcty.index] = passnum[cty.index] + 1;
continue;
}
if (dist[nextcty.index] == dist[cty.index] + cost) {
//刷新路径数为本身路径数加上新增同等地位父节点的路径数
diffpath[nextcty.index]+=diffpath[cty.index];
if (glad[nextcty.index] < glad[cty.index] + nextcty.glad) {
path[nextcty.index] = cty.index;
glad[nextcty.index] = glad[cty.index] + nextcty.glad;
passnum[nextcty.index] = passnum[cty.index] + 1;
}
else if (glad[nextcty.index] == glad[cty.index] + nextcty.glad) {
if (passnum[nextcty.index] > passnum[cty.index] + 1) {
path[nextcty.index] = cty.index;
passnum[nextcty.index] = passnum[cty.index] + 1;
}
}
}
}
}
vector<int> stack;
int cty = c[hah("ROM")].index;
int dis = cty;
int totselect;
while (cty != -1) {
stack.push_back(cty);
cty = path[cty];
}
cout << diffpath[dis] << ' ' << dist[dis] << ' ' << glad[dis] << ' ' << glad[dis] / (int)passnum[dis] << endl;
bool first = 1;
while (!stack.empty()) {
if (first) first = 0;
else cout << "->";
cout << c[myc[stack.back()]].name;
stack.pop_back();
}
}

ps: 为了加快城市关系建立,引入了一个哈希表,可能有些多余,反而使代码结构看起来有些杂乱。