PAT 甲级 刷题日记|A 1087 All Roads Lea
题目
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结尾无空行
思路
多做几个最短路径题目会发现,模板还是比较典型的。
字符串处理:map映射
最短路径:迪杰斯特拉算法,欢乐值、节点个数、路径数同步更新
图论的代码都会稍长一些,不要害怕
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 202;
const int inf = 1e9;
int e[maxn][maxn];
int visit[maxn];
int dis[maxn];
int val[maxn];
int hap[maxn];
int num[maxn];
int pre[maxn];
int pathnum[maxn];
map<string, int> name;
map<int, string> id;
int indexs = 1;
vector<int> ans;
int n, k;
string start;
int main() {
fill(e[0], e[0] + maxn * maxn, inf);
fill(dis, dis + maxn, inf);
cin>>n>>k>>start;
name[start] = indexs;
id[indexs] = start;
indexs++;
name["ROM"] = indexs;
id[indexs] = "ROM";
indexs++;
for (int i = 0; i < n - 1; i++) {
string na;
int happy;
cin>>na>>happy;
if (!name[na]) {
name[na] = indexs;
id[indexs] = na;
indexs++;
}
hap[name[na]] = happy;
}
for (int i = 0; i < k; i++) {
string str1, str2;
int l;
cin>>str1>>str2>>l;
if (!name[str1]) {
name[str1] = indexs;
id[indexs] = str1;
indexs++;
}
if (!name[str2]) {
name[str2] = indexs;
id[indexs] = str2;
indexs++;
}
e[name[str2]][name[str1]] = e[name[str1]][name[str2]] = l;
}
dis[1] = 0;
num[1] = 0;
val[1] = 0;
pre[1] = -1;
pathnum[1] = 1;
for (int i = 1; i <= n; i++) {
int u = -1, mindis = inf;
for (int j = 1; j <= n; j++) {
if (visit[j] == 0 && dis[j] <mindis) {
u = j;
mindis = dis[j];
}
}
if (u == -1) break;
visit[u] = 1;
for (int v = 1; v <= n; v++) {
if (visit[v] == 0 && e[u][v] != inf) {
if (dis[v] > dis[u] + e[u][v]) {
dis[v] = dis[u] + e[u][v];
pre[v] = u;
num[v] = num[u] + 1;
val[v] = val[u] + hap[v];
pathnum[v] = pathnum[u];
} else if (dis[v] == dis[u] + e[u][v]) {
pathnum[v] += pathnum[u];
if (val[v] < val[u] + hap[v]) {
pre[v] = u;
num[v] = num[u] + 1;
val[v] = val[u] + hap[v];
} else if (val[v] == val[u] + hap[v]) {
if (val[v]/num[v] < (val[u] + hap[v])/(num[u] + 1)) {
pre[v] = u;
num[v] = num[u] + 1;
}
}
}
}
}
}
cout<<pathnum[2]<<" "<<dis[2]<<" "<<val[2]<<" ";
cout<<val[2]/num[2]<<endl;
int tid = 2;
while (1) {
ans.push_back(tid);
tid = pre[tid];
if (tid == -1) break;
}
for (int i = ans.size() - 1; i >= 0; i--) {
cout<<id[ans[i]];
if (i != 0) cout<<"->";
}
}