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个城市,m条路,除了起点外每个城市都有自己的幸福值,求出从起点到ROM花费最少的路径,如果路径不唯一则输出幸福值总数最大的,如果还是不唯一则输出平均幸福值最大的,注意求平均幸福值除以的数量不包括起点。此处因为城市不再像之前给的是数字,而是字符串,所以要用map进行转换
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
const int maxn=210,inf=1000000000;
int n,k,maxsum=-1,maxavg=-1,beginn=0,endd=0;
int g[maxn][maxn],dis[maxn],happy[maxn],num[maxn]={0};
vector<int> pre[maxn];
vector<int> path,temp;
bool vis[maxn];
map<string,int> strtoint;//保存的是城市名字和对应的编号
map<int,string> inttostr;//保存的是编号对应的城市名字
void dijkstra(int s){
fill(dis,dis+maxn,inf);
fill(vis,vis+maxn,false);
dis[s]=0;
num[s]=1;
for(int i=0;i<n;i++){
int u=-1,minn=inf;
for(int j=1;j<=n;j++){
if(vis[j]==false&&dis[j]<minn){
u=j;
minn=dis[j];
}
}
if(u==-1) return;
vis[u]=true;
for(int v=1;v<=n;v++){
if(vis[v]==false&&g[u][v]!=0){
if(dis[u]+g[u][v]<dis[v]){
dis[v]=dis[u]+g[u][v];
num[v]=num[u];
pre[v].clear();
pre[v].push_back(u);
}else if(dis[u]+g[u][v]==dis[v]){
pre[v].push_back(u);
num[v]+=num[u];
}
}
}
}
}
int index=1;
int changetoint(string s){
if(strtoint[s]==0){
strtoint[s]=index;
inttostr[index]=s;
return index++;
}else return strtoint[s];
}
void dfs(int v){
temp.push_back(v);
if(v==beginn){
int sum=0,averge=0;
for(int i=temp.size()-2;i>=0;i--){
int id=temp[i];
sum+=happy[id];
}
averge=sum/(temp.size()-1);
if(sum>maxsum){
maxsum=sum;
maxavg=averge;
path=temp;
}else if(sum==maxsum&&averge>maxavg){
path=temp;
maxavg=averge;
}
}
for(int i=0;i<pre[v].size();i++){
dfs(pre[v][i]);
}
temp.pop_back();
}
int main(){
string s,s1;
int h,c;
cin>>n>>k>>s;
beginn=changetoint(s);
for(int i=0;i<n-1;i++){
cin>>s>>h;
if(s[0]=='R'&&s[1]=='O'&&s[2]=='M') endd=changetoint(s);
happy[changetoint(s)]=h;
}
for(int i=0;i<k;i++){
cin>>s>>s1>>c;
int id1=changetoint(s);
int id2=changetoint(s1);
g[id1][id2]=g[id2][id1]=c;
}
dijkstra(beginn);
dfs(endd);
printf("%d %d %d %d\n",num[endd],dis[endd],maxsum,maxavg);
cout<<inttostr[1];
for(int i=path.size()-2;i>=0;i--){
cout<<"->"<<inttostr[path[i]];
}
printf("\n");
return 0;
}