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: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 1Sample Output:
3 3 195 97 HZH->PRS->ROM
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
int happy[210], route[210][210], visited[210], path[210], dist[210], dist_happy[210], cnt=0, ans[210];
map<string, int> idx;
map<int, string> xdi;
int name2id(char str[]) {
string s = string(str);
if(idx.count(s)) {
return idx[s];
} else {
xdi[cnt] = s;
idx[s] = cnt++;
return idx[s];
}
}
int main() {
int n, k, hy,cost;
char start[5], city[5], c1[5], c2[5];
scanf("%d %d", &n, &k);
scanf("%s", start);
int begin = name2id(start), end = -1;
for(int i = 1; i < n; i++) {
scanf("%s %d", city, &hy);
int tmp = name2id(city);
happy[tmp] = hy;
if(strcmp(city, "ROM") == 0) {
end = tmp;
}
}
for (int i = 0; i < k; ++i)
{
scanf("%s %s %d", c1, c2, &cost);
int t1 = idx[string(c1)], t2 = idx[string(c2)];
route[t1][t2] = cost;
route[t2][t1] = cost;
}
visited[begin] = 1;
int minn = 0x7fffffff;
for(int i = 0; i < n; i++) {
if(route[begin][i] == 0) continue;
dist[i] = route[begin][i];
path[i] = 1;
dist_happy[i] = happy[i];
if(dist[i] < minn) ans[i] = begin;
}
int index = begin;
for (int i = 1; i < n; ++i)
{
minn = 0x7fffffff;
for(int j = 0; j < n; j++) {
if(visited[j] == 1 || dist[j] == 0) continue;
if(dist[j] < minn) {
minn = dist[j];
index = j;
}
}
visited[index] = 1;
//printf("**%s** %d\n", xdi[index].c_str(), dist_happy[index]);
for (int j = 0; j < n; ++j)
{
if(visited[j] == 1 || route[index][j] == 0) continue;
if(dist[j] == 0) {
dist[j] = dist[index] + route[index][j];
path[j] = path[index];
dist_happy[j] = dist_happy[index] + happy[j];
ans[j] = index;
} else if(dist[j] > dist[index]+route[index][j]) {
dist[j] = dist[index]+route[index][j];
dist_happy[j] = dist_happy[index] + happy[j];
path[j] = path[index];
ans[j] = index;
} else if (dist[j] == dist[index]+route[index][j]) {
path[j] += path[index];
if(dist_happy[j] < dist_happy[index]+happy[j]) {
dist_happy[j] = dist_happy[index]+happy[j];
ans[j] = index;
}
}
//printf("**%d** %s\n", dist_happy[j], xdi[j].c_str());
}
}
vector<string> vec;
int tmp = end;
while(begin != tmp) {
vec.push_back(xdi[tmp]);
tmp = ans[tmp];
}
vec.push_back(xdi[begin]);
printf("%d %d %d %d\n", path[end], dist[end], dist_happy[end], dist_happy[end]/(int(vec.size())-1));
for(int i = vec.size()-1; i >= 0;i--) {
if(i == vec.size()-1) {
printf("%s", vec[i].c_str());
} else printf("->%s", vec[i].c_str());
}
printf("\n");
return 0;
}
本文介绍了一个寻找从城市到罗马的最低成本且最高幸福感旅行路线的问题。通过输入不同城市间的路线成本和各城市的幸福感值,利用图算法求解最短路径并推荐最佳路线。
428

被折叠的 条评论
为什么被折叠?



