A1087. All Roads Lead to Rome

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 1

Sample Output:

3 3 195 97
HZH->PRS->ROM
 #include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
int G[][], happy[];
int visit[], dst[];
vector<int> pre[];
const int INF = ;
map<string, int> str2num;
map<int, string> num2str;
int N, K;
string start, fina = "ROM";
int start_, fina_, cnt = ;
void dijkstra(int s){
fill(visit, visit + , );
fill(dst, dst + , INF);
dst[s] = ;
for(int i = ; i < N; i++){
int u = -, minLen = INF;
for(int j = ; j < N; j++){
if(visit[j] == && dst[j] < minLen){
minLen = dst[j];
u = j;
}
}
if(u == -)
return;
visit[u] = ;
for(int j = ; j < N; j++){
if(visit[j] == && G[u][j] != INF){
if(G[u][j] + dst[u] < dst[j]){
dst[j] = G[u][j] + dst[u];
pre[j].clear();
pre[j].push_back(u);
}else if(G[u][j] + dst[u] == dst[j]){
pre[j].push_back(u);
}
}
}
}
} vector<int> path, ans;
int pathNum = , maxHapp = -, maxNum = -;
void DFS(int vt){
path.push_back(vt);
if(vt == start_){
pathNum++;
int sumTemp = ;
for(int i = path.size() - ; i >= ; i--){
sumTemp += happy[path[i]];
}
if(sumTemp > maxHapp){
maxNum = path.size() - ;
maxHapp = sumTemp;
ans = path;
}else if(sumTemp == maxHapp && path.size() < maxNum){
maxNum = path.size() - ;
maxHapp = sumTemp;
ans = path;
}
path.pop_back();
return;
}
int Len = pre[vt].size();
for(int i = ; i < Len; i++){
DFS(pre[vt][i]);
}
path.pop_back();
return;
}
int strNum(string ss){
if(str2num.count(ss) == ){
str2num[ss] = cnt;
num2str[cnt] = ss;
return cnt++;
}else{
return str2num[ss];
}
}
int main(){
cin >> N >> K >> start;
string temps, temps2;
fill(G[], G[] + *, INF);
int tempL;
for(int i = ; i < N - ; i++){
cin >> temps >> tempL;
happy[strNum(temps)] = tempL;
}
for(int i = ; i < K; i++){
cin >> temps >> temps2 >> tempL;
int u1 = strNum(temps), u2 = strNum(temps2);
G[u1][u2] = G[u2][u1] = tempL;
}
start_ = strNum(start);
fina_ = strNum(fina);
dijkstra(start_);
int costSum = dst[fina_];
DFS(fina_);
printf("%d %d %d %d\n%s", pathNum, costSum, maxHapp, maxHapp / maxNum, num2str[start_].c_str());
for(int i = ans.size() - ; i >= ; i--){
printf("->%s", num2str[ans[i]].c_str());
}
cin >> N;
return ;
}

总结:

1、题意:求出最短路,如果有多条,则求出一路上点权之和(开始节点不计入)最大的一条,如果还有多条,则求出点权之和的均值最大的一条(依然不计入开始节点)。

2、输入的城市名字通过map转化为整数节点,使用一个全局变量cnt分配节点编号。

3、pre[K]表示K节点的所有前驱节点。整个pre数组需要从目的地节点回溯到开始节点。

上一篇:微信小程序下拉框


下一篇:php curl抓取类分享