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
代码长度限制
16 KB
时间限制
200 ms
内存限制
思路:
//输出顺序要求:1.最短路,2.路相同输出最快乐的,3.快乐相同输出节点最少的
//输出:相同最短路的个数,路径长,快乐,快乐/经过城市数,路
//解法:先dijikstra再dfs,在其中记录路径
#include<bits/stdc++.h>
using namespace std;
int n,m,c1,c2;
unordered_map<string,int>mp1;
unordered_map<int,string>mp2;
int distmat[200][200];
int mindist[200];
vector<int> v[200];
int happys[200];
vector<int> ans(200,0);
int zysize = 0;
int zyhappy = 0;
void Dijikstra(){
int visited[200] = {0};
int last = c1;
mindist[last] = 0;
for(int i=0;i<n;i++){
visited[last] = 1;
//更新所有的最小距离
for(int j = 0;j<n;j++) mindist[j] = min(mindist[j],mindist[last]+distmat[last][j]);
int k=-1;
//更新last为最小距离节点
for(int j = 0;j<n;j++) if(!visited[j]&&(k<0||mindist[j]<mindist[k]))k=j;
last = k;
}
}
void dfs(int cur,int curdist,int curhappy,vector<int>& path){
if(curdist>mindist[cur]) return;
path.push_back(cur);
if(cur==c2){
zysize++;
if(curhappy>zyhappy||(curhappy==zyhappy&&ans.size()>path.size())) ans = path;
if(curhappy>zyhappy) zyhappy = curhappy;
}else{
for(auto a:v[cur]) dfs(a,curdist+distmat[cur][a],curhappy+happys[a],path);
}
path.pop_back();
}
int main(){
string name;
cin>>n>>m;
cin>>name;
mp1[name] = 0;
mp2[0] = name;
c1 = 0;
int happy;
memset(distmat,0x3f,sizeof(distmat));
memset(mindist,0x3f,sizeof(mindist));
for(int i=1;i<n;i++){
cin>>name>>happy;
mp1[name] = i;
mp2[i] = name;
happys[i] = happy;
if(name == "ROM") c2 = mp1[name];
}
string a1,a2;
int l;
for(int i=0;i<m;i++){
cin>>a1>>a2;
scanf("%d",&l);
v[mp1[a1]].push_back(mp1[a2]);
v[mp1[a2]].push_back(mp1[a1]);
distmat[mp1[a1]][mp1[a2]] = l;
distmat[mp1[a2]][mp1[a1]] = l;
}
Dijikstra();
vector<int> path;
dfs(c1,0,0,path);
cout<<zysize<<" "<<mindist[c2]<<" "<<zyhappy;
printf(" %d\n",zyhappy/(ans.size()-1));
for(int i=0;i<ans.size();i++){
if(i==0) cout<<mp2[ans[i]];
else cout<<"->"<<mp2[ans[i]];
}
}