PAT A1087 All Roads Lead to Rome
1087 All Roads Lead to Rome
分数 30
作者 CHEN, Yue
单位 浙江大学
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
将字符串转化为数字下标进行存储,用map来实现int和string之间的映射关系;
*
* 当题目中要 求超过三个标尺时,先用Dijkstra算法求出每个顶点的前驱节点,
* 即u是由哪个点哪个点(哪些点)转化过来能使d[u] 的值变小,使d[u]变小的
* 点就是应该求的u的前驱节点;
/*** 将字符串转化为数字下标进行存储,用map来实现int和string之间的映射关系;* * 当题目中要 求超过三个标尺时,先用Dijkstra算法求出每个顶点的前驱节点,* 即u是由哪个点哪个点(哪些点)转化过来能使d[u] 的值变小,使d[u]变小的* 点就是应该求的u的前驱节点;
*/#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>using namespace std;const int N = 210, INF = 1e9;
int g[N][N], d[N], w[N]; //图,距离数组,幸福值数组
bool hs[N];
int Nv, Ne, st, ed; //顶点数目,边数目,起点,终点
int idx; //字符转化为数字的下标编号//最小花费,最大幸福值,最大平均幸福值,最短路径数目
int MINC = INF, MAXH, MAXA, num;
vector<int> temp, path; //路径编号
vector<int> pre[N]; //前驱节点
map<int, string> idx_city; //字符串与数字编号的一一映射
map<string, int> city_idx;int add(string s)
{city_idx[s] = idx;idx_city[idx] = s;return idx++;
}void Read()
{fill(*g, *g+N*N, INF); //初始化string sta;cin >> Nv >> Ne >> sta;st = add(sta);for(int i=1; i<Nv; ++i){string s;int hap;cin >> s >> hap;int u = add(s);w[u] = hap;}ed = city_idx["ROM"];for(int i=0; i<Ne; ++i){string a, b;int u, v, cost;cin >> a >> b >> cost;u = city_idx[a], v = city_idx[b];g[u][v] = g[v][u] = min(g[u][v], cost);}
}void Dijkstra(int st)
{fill(d, d+N, INF);d[st] = 0;for(int i=0; i<Nv; ++i){int MIN = INF, u = -1;for(int j=0; j<Nv; ++j)if(hs[j] == 0 && d[j] < MIN){MIN = d[j];u = j;}if(u == -1) return;hs[u] = 1;for(int j=0; j<Nv; ++j)if(hs[j] == 0 && g[u][j] != INF){int cost = g[u][j];if(d[u] + cost < d[j]){d[j] = d[u] + cost;pre[j].clear();pre[j].push_back(u);}else if(d[u] + cost == d[j])pre[j].push_back(u);}}
}void dfs(int v)
{if(v == st){temp.push_back(v);++num; int cost = 0, hap = 0;for(int j=temp.size()-1; j>0; --j){int fs = temp[j], sc = temp[j-1];cost += g[fs][sc];hap += w[sc];}int avg = hap / (temp.size() - 1);//求平均值没有算起点;if(cost < MINC) {MINC = cost;MAXH = hap;MAXA = avg;path = temp;}else if(cost == MINC && hap > MAXH){MAXH = hap;MAXA = avg;path = temp;}else if(cost == MINC && hap == MAXH && avg > MAXA){MAXA = avg;path = temp;}temp.pop_back();return;}temp.push_back(v);for(auto ele : pre[v])dfs(ele);temp.pop_back(); //回溯
}void Print()
{cout << num << ' ' << MINC << ' ' << MAXH << ' ' << MAXA << endl;for(int i=path.size()-1; i>=0; --i){if(i != path.size()-1) cout << "->";cout << idx_city[path[i]];}
}int main()
{Read();Dijkstra(st);dfs(ed);Print();return 0;
}