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 Klines 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
思路:这是一个Dijkstra的常规题,唯一的改动就是string到int的转换,使用map来映射,看了大家都是dfs+dijkstra,有个坑踩了2小时,然后终于只使用dijkstra莽了~在幸福总数一定的情况下,求平均可以换为找最少的经过节点解决
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<unordered_map>
#define max_inf 0x3f3f3f3f
using namespace std;
int n, k;
string s;
unordered_map<string, int> city_num;
unordered_map<int, string> num_city;
int graph[210][210];
bool visit[210] = {false};
int total_cost[210];
int happiness[210] = {0};
int cnt_happiness[210] = {0};
int cnt[210] = {0};
int cnt_roat[210] = {0};
int path[210] = {0};
void dijkstra(int start){
fill(total_cost, total_cost + 210, max_inf);
fill(cnt_roat, cnt_roat + 210, 1);
total_cost[start] = 0;
for(int i = 0; i < n; i++){
int u = - 1;
int min = max_inf;
for(int j = 1; j <= n; j++){
if(!visit[j] && total_cost[j] < min){
u = j;
min = total_cost[j];
}
}
if(u == -1){
return;
}
visit[u] = true;
for(int j = 1; j <= n; j++){
if(!visit[j] && graph[u][j] != max_inf){
if(total_cost[j] > total_cost[u] + graph[u][j]){
total_cost[j] = total_cost[u] + graph[u][j];
cnt_happiness[j] = cnt_happiness[u] + happiness[j];
cnt[j] = cnt[u] + 1;
path[j] = u;
cnt_roat[j] = cnt_roat[u];//绝杀
}else if(total_cost[j] == total_cost[u] + graph[u][j])
{
cnt_roat[j] = cnt_roat[j] + cnt_roat[u];
if(cnt_happiness[j] < cnt_happiness[u] + happiness[j]){
cnt_happiness[j] = cnt_happiness[u] + happiness[j];
cnt[j] = cnt[u] + 1;
path[j] = u;
}else if(cnt_happiness[j] == cnt_happiness[u] + happiness[j] && cnt[j] > cnt[u] + 1)
{
cnt[j] = cnt[u] + 1;
path[j] = u;
}
}
}
}
}
}
int main(){
fill(graph[0], graph[0] + 210 * 210, max_inf);
cin >> n >> k >> s;
for(int i = 1; i <= n - 1; i++){
string temp;
cin >> temp;
city_num[temp] = i;
num_city[i] = temp;
int happy;
scanf("%d", &happy);
happiness[city_num[temp]] = happy;
}
city_num[s] = n;
num_city[n] = s;
for(int i = 0; i < k; i++){
string temp_a, temp_b;
cin >> temp_a >> temp_b;
int cost;
scanf("%d", &cost);
graph[city_num[temp_a]][city_num[temp_b]] = cost;
graph[city_num[temp_b]][city_num[temp_a]] = cost;
}
dijkstra(city_num[s]);
int x = city_num["ROM"];
printf("%d %d %d %d\n", cnt_roat[x], total_cost[x], cnt_happiness[x], cnt_happiness[x] / cnt[x]);
vector<int> temp;
while (x != 0)
{
temp.push_back(x);
x = path[x];
}
for(int i = temp.size() - 1; i >= 0; i--){
printf("%s%s", num_city[temp[i]].c_str(), i == 0 ? "" : "->");
}
printf("\n");
return 0;
}