1087 All Roads Lead to Rome (30 分)
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
题目大意:
给出n个城市以及k条路径还有出发的城市名称。随后n – 1行表示的是每个城市的幸福值,终点固定在罗马。随后的k 行是不同城市件的道路以及其消耗。
要求输出最少消耗的路径条数,最少的消耗以及最大的幸福值总和,以及平均幸福值(只取整数部分)。
思路:
实际上就是最短路径问题,请熟练dijkstra算法。在dijkstra算法上加上了路径条数num,以及幸福的点权happiness, 还有存储路径。
其次就是将string 转化为int 的问题。
参考代码:
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int e[210][210];
const int inf = 0x7fffffff;
vector<int> dis, w, weight, num, path, pt, vis;
map<string, int> strtoInt;
map<int, string> inttoStr;
int n, k, st, ed;
string c1, c2;
void dijkstra(int st){
fill(dis.begin(), dis.end(), inf);
dis[st] = 0, num[st] = 1;
for(int i = 0; i < n; ++i){
int u = -1, minv = inf;
for(int j = 0;j < n; ++j)
if(!vis[j] && minv > dis[j]) u = j, minv = dis[j];
if(u == -1) return ;
vis[u] = 1;
for(int v = 0; v < n; ++v)
if(!vis[v] && e[u][v]){
if(dis[u] + e[u][v] < dis[v]){
dis[v] = dis[u] + e[u][v];
num[v] = num[u];
path[v] = u;
w[v] = w[u] + weight[v];
pt[v] = pt[u] + 1;
}else if(dis[u] + e[u][v] == dis[v]){
num[v] += num[u];
if(w[u] + weight[v] > w[v]){
w[v] = w[u] + weight[v];
path[v] = u;
pt[v] = pt[u] + 1;
} else if(w[u] + weight[v] == w[v] && pt[u] + 1 < pt[v]){
pt[v] = pt[u] + 1;
path[v] = u;
}
}
}
}
}
void show(int v){
if(v == st){
printf("%s", inttoStr[v].c_str());
return;
}
show(path[v]);
printf("->%s", inttoStr[v].c_str());
}
int main(){
cin >> n >> k >> c1;
dis.resize(n), w.resize(n), weight.resize(n), num.resize(n);
pt.resize(n), path.resize(n), vis.resize(n);
strtoInt[c1] = st;
inttoStr[st] = c1;
for(int i = 1; i < n; ++i){
cin >> c1 >> weight[i];
strtoInt[c1] = i;
inttoStr[i] = c1;
}
for(int i = 0; i < k; ++i){
cin >> c1 >> c2;
int a = strtoInt[c1], b = strtoInt[c2];
cin >> e[a][b];
e[b][a] = e[a][b];
}
dijkstra(st);
ed = strtoInt["ROM"];
printf("%d %d %d %d\n", num[ed], dis[ed], w[ed], w[ed] / pt[ed]);
show(ed);
}