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
各项系数说明:
whappy[maxn]={0};//到达v点的最大happy
seq[maxn];//到达v点的最小序数
same[maxn];//到达v点的相同道路数量
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<map>
using namespace std;
#define INF 10000000
#define maxn 200
bool visited[maxn]={false};
int road[maxn][maxn],dist[maxn],happy[maxn]={0},whappy[maxn]={0},seq[maxn],n,m,stid,path[maxn],same[maxn];
vector<int> city[maxn];
string st;
map<string ,int> stnum;
map<int,string> numts;
int fdist(){
int min=INF,t=-1;
for(int i=1;i<n;i++){
if(!visited[i]&&dist[i]<min){
min=dist[i];
t=i;
}
}
return t;
}
void dis(){
dist[0]=0;
seq[0]=0;
whappy[0]=0;
same[0]=1;
int v=0;
while(v!=-1){
visited[v]=true;
for(int i=0;i<city[v].size();i++){
int k=city[v][i];
if(!visited[k]&&road[v][k]!=INF){
if(dist[k]>dist[v]+road[v][k]){
same[k]=same[v];
dist[k]=dist[v]+road[v][k];
path[k]=v;
seq[k]=seq[v]+1;
whappy[k]=whappy[v]+happy[k];
continue;
}
else if(dist[k]==dist[v]+road[v][k]){
same[k]+=same[v];
if(whappy[v]+happy[k]>whappy[k]){
path[k]=v;
whappy[k]=whappy[v]+happy[k];
seq[k]=seq[v]+1;
continue;
}
else if(whappy[v]+happy[k]==whappy[k]){
if(seq[v]+1<seq[k]){
path[k]=v;
seq[k]=seq[v]+1;
continue;
}
}
}
}
}
v=fdist();
}
}
int main(){
freopen("in.txt","r",stdin);
cin>>n>>m>>st;
stnum[st]=0;
numts[0]=st;
for(int i=1;i<n;i++){
string s;
cin>>s;
cin>>happy[i];
stnum[s]=i;
numts[i]=s;
}
string s1,s2;
fill(road[0],road[0]+maxn*maxn,INF);
fill(seq,seq+maxn,INF);
fill(dist,dist+maxn,INF);
int t;
for(int i=0;i<m;i++){
cin>>s1>>s2>>t;
int id1=stnum[s1],id2=stnum[s2];
road[id1][id2]=t;
road[id2][id1]=t;
city[id1].push_back(id2);
city[id2].push_back(id1);
}
dis();
stack<int> s;
int rid=stnum["ROM"];
int z=rid;
while(rid!=stid){
s.push(rid);
rid=path[rid];
}
printf("%d %d %d %d\n",same[z],dist[z],whappy[z],whappy[z]/seq[z]);
cout<<st;
while(!s.empty()){
cout<<"->"<<numts[s.top()];
s.pop();
}
return 0;
}