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
题目大意:有cityNum个城市,有roadNum条路,每个城市有对应的幸福值。给定出发城市,求从出发城市到ROM的最短路径。如果路径有多条,选择幸福值总和最大的。如果仍有多条,选择平均幸福值最大的(忽略出发城市)。
分析:对这道题输入的处理是一个很大的难点,一开始想将字符串按照单个字符的ASCII码转化成数字,用二维数组存储路径信息。发现二维数组太大而放弃。后参考柳神代码,用map存储字符串与数字的对应信息。其他的用迪杰斯特拉算法就能实现,不过记录的数据多些罢了(包括最短路径条数,最短路径,幸福值总和)
AC代码
#include<bits/stdc++.h>
using namespace std;
const int inf = 9999999;
int cityNum, roadNum;
int road[201][201], weight[201], minDis[201],num[201],maxWeight[201],path[201];
bool visit[200];
map<string, int> jihe1;
map<int, string> jihe2;
int main(){
fill(road[0], road[0] + 201 * 201, inf);
fill(minDis, minDis + 201, inf);
fill(path, path + 201, -1);
string temp,temp2;
int temp3;
scanf("%d %d", &cityNum, &roadNum);
cin >> temp;
jihe1[temp] = 1;
jihe2[1] = temp;
weight[1] = 0;
for (int i = 2; i <= cityNum;i++){
cin >> temp >> temp3;
jihe1[temp] = i;
jihe2[i] = temp;
weight[i] = temp3;
}
for (int i = 0; i < roadNum;i++){
cin >> temp >> temp2 >> temp3;
road[jihe1[temp]][jihe1[temp2]] = road[jihe1[temp2]][jihe1[temp]] = temp3;
}
minDis[1] = 0;
num[1] = 1;
for (int i = 0; i < cityNum;i++){
int u = -1, minn = inf;
for (int j = 1; j <= cityNum;j++){
if(visit[j]==false&&minDis[j]<minn){
u = j;
minn = minDis[j];
}
}
if(u==-1)
break;
visit[u] = true;
for (int v = 1; v <= cityNum;v++){
if(visit[v]==false&&road[u][v]!=inf){
if(minDis[u]+road[u][v]<minDis[v]){
minDis[v] = minDis[u] + road[u][v];
num[v] = num[u];
maxWeight[v] = maxWeight[u] + weight[v];
path[v] = u;
}
else if(minDis[u]+road[u][v]==minDis[v]){
num[v] += num[u];
if(maxWeight[v]<maxWeight[u]+weight[v]){
maxWeight[v] = maxWeight[u] + weight[v];
path[v] = u;
}
}
}
}
}
int stack[201];
int a = jihe1["ROM"],top=-1;
while(a!=-1){
stack[++top] = a;
a = path[a];
}
printf("%d %d %d %d\n", num[jihe1["ROM"]], minDis[jihe1["ROM"]], maxWeight[jihe1["ROM"]], maxWeight[jihe1["ROM"]] / top);
while(top!=0)
cout << jihe2[stack[top--]] <<"->";
cout << jihe2[stack[top]];
return 0;
}
我看网上的大多数解法都是迪杰斯特拉算法加+DFS,DFS是为了解决最后一种情况(最短路径相同,幸福值总和相同,选择平均幸福值最大的)。在这里我觉得可以不用DFS,因为一个一个结点遍历,如果有多条最短路径的话,一定先找到经过结点数少的那条,如果他们的幸福值总和相同,则结点数少的平均幸福值一定大。