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
有几点需要注意:
- 为了避免麻烦,将所有地点映射到1-N上,这样在进行Dij算法时会容易许多。
- 保存到达每个节点的最短路径,虽说空间复杂度较高但是也没什么办法。
- 注意初始化时fill函数的使用,曾出现过错误。
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <climits>
using namespace std;
int N,K;
map<string,int> map1;
map<int,string> map2;
map<int,int> map3;//保存每个节点的happiness
int graph[205][205];
vector<vector<int>> paths[205];//到达各个节点的路径集合
int Dij(int s,int d){
bool visited[N+1];
int dis[N+1];
memset(visited, false, sizeof(visited));
fill(dis,dis+N+1,INT_MAX);
dis[s]=0;
vector<int> initPath;
initPath.push_back(s);
paths[s].push_back(initPath);
for (int k = 0; k < N; ++k) {
int index=0;
int minVal=INT_MAX;
for (int i = 1; i <=N ; ++i) {
if(!visited[i]&&dis[i]<minVal){
index=i;
minVal=dis[i];
}
}
visited[index]=true;
if(d==index){
return dis[index];
}
for (int i = 1; i <=N ; ++i) {
if(!visited[i]&&graph[index][i]!=-1){
if(dis[index]+graph[index][i]<dis[i]){
dis[i]=graph[index][i]+dis[index];
paths[i].clear();
for(auto vec:paths[index]){
vec.push_back(i);
paths[i].push_back(vec);
}
}else if(dis[index]+graph[index][i]==dis[i]){
for(auto vec:paths[index]){
vec.push_back(i);
paths[i].push_back(vec);
}
}
}
}
}
return dis[d];
}
int main() {
cin>>N>>K;
string start;
cin>>start;
map1[start]=1;
map2[1]=start;
int id=2;
for (int i = 0; i < N - 1; ++i) {
string name;
int val;
cin>>name>>val;
map1[name]=id;
map2[id]=name;
map3[id]=val;
id++;
}
memset(graph,-1, sizeof(graph));
for (int i = 0; i < K; ++i) {
string s1,s2;
int dis;
cin>>s1>>s2>>dis;
int id1=map1[s1],id2=map1[s2];
graph[id1][id2]=dis;
graph[id2][id1]=dis;
}
int s=map1[start];
int d=map1["ROM"];
int dis=Dij(s,d);
vector<vector<int>> vectors=paths[map1["ROM"]];
vector<int> hs;
for(const auto& p:vectors){
int s=0;
for(auto x:p){
s+=map3[x];
}
hs.push_back(s);
}
int maxh=INT_MIN;
int index=0;
for (int i = 0; i < hs.size(); ++i) {
if(hs[i]>maxh){
maxh=hs[i];
index=i;
}
}
auto bestp=vectors[index];
cout<<vectors.size()<<" "<<dis<<" "<<maxh<<" "<<maxh/(bestp.size()-1)<<endl;
cout<<map2[bestp[0]];
for (int j = 1; j < bestp.size(); ++j) {
cout<<"->"<<map2[bestp[j]];
}
return 0;
}