A1087 All Roads Lead to Rome

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include<bits/stdc++.h>
#include<map>
#define MAX 210
using namespace std;
bool vis[MAX];
int cnt=0;
int dis[MAX],num[MAX],wei[MAX];
int g[MAX][MAX];
map<string,int> dict;
map<int,string> road;
#define INF 0x3f3f3f
int sum=0;
int ed,st,n,m;
vector<int> tmp,path,pre[MAX];
int findDict(string s){
if(!dict.count(s)){
road[sum]=s;
sum++;
return dict[s]=sum-1;
}
else return dict[s];
}
void dij(int st){
fill(dis,dis+MAX,INF);
dis[st]=0;
for(int i=0;i<n;i++){
int min=INF,u=-1;
for(int j=0;j<n;j++){
//if(dis[j]<min)!
if(vis[j]==false&&dis[j]<min){
min=dis[j];
u=j;
}
}
if(u==-1) return;
vis[u]=true;
for(int v=0;v<n;v++){
if(vis[v]) continue;
if(g[u][v]==INF) continue;
if(dis[u]+g[u][v]<dis[v]){
dis[v]=dis[u]+g[u][v];
pre[v].clear();
pre[v].push_back(u);
// num[v]=num[u];
}
else if(dis[u]+g[u][v]==dis[v]){
pre[v].push_back(u);
// num[v]+=num[u];
}
}
}
}
int maxi=-1;
double mavg=-1;
void dfs(int ed){
if(ed==st){//dfs中遍历的路径都是最短的
cnt++;//最短路径条数
int w=0;
tmp.push_back(ed);
for(int j=tmp.size()-2;j>=0;j--){
w+=wei[tmp[j]];
}
//w*1.0
double avg=w*1.0/(tmp.size()-1);
if(maxi<w) {
maxi=w;
path=tmp;
mavg=avg;
}
else if(maxi==w&&mavg<avg){

mavg=avg;
path=tmp;
}
tmp.pop_back();
return;
}
tmp.push_back(ed);
for(int k=0;k<pre[ed].size();k++)
dfs(pre[ed][k]);
tmp.pop_back();
}
int main(){
string start,end="ROM";
cin>>n>>m;
cin>>start;
for(int i=0;i<n-1;i++)
{
string s;
int t;
cin>>s>>t;
wei[findDict(s)]=t;
}

st=findDict(start);
ed=findDict(end);//!
wei[st]=0;
fill(g[0],g[0]+MAX*MAX,INF);
for(int i=0;i<m;i++){
string s1,s2;
int t;
cin>>s1>>s2;
cin>>t;
int a=findDict(s1),b=findDict(s2);
g[a][b]=g[b][a]=t;
}
dij(st);

dfs(ed);

cout<<cnt<<" "<<dis[ed]<<" "<<maxi<<" "<<(int)mavg<<endl;
for(int k=path.size()-1;k>=0;k--)
{
cout<<road[path[k]];
if(k!=0) cout<<"->";
}


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<bits/stdc++.h>
using namespace std;
#define MAX 202
#define INF 0x3f3f3f
map<string,int> dict;
map<int,string> is;
int cnt=0;
int n,m;
int g[MAX][MAX],vis[MAX],dis[MAX];
int cost[MAX],pre[MAX];
int c[MAX];
int num[MAX],pt[MAX];
int strToInt(string city){
if(!dict.count(city)){
dict[city]=cnt++;
is[dict[city]]=city;
}
return dict[city];
}
void dij(int st,int ed){
fill(dis,dis+MAX,INF);

dis[st]=0;
num[st]=1;
pt[st]=0;
pre[st]=-1;
c[st]=cost[st]; //!
for(int j=0;j<n;j++){

int maxn=INF,k=-1;
for(int i=0;i<n;i++)
{
if(dis[i]<maxn&&!vis[i]){
k=i;
maxn=dis[i];
}
}
if(k==-1) return;
//cout<<"k:"<<k<<endl;
vis[k]=true;
for(int i=0;i<n;i++){
if(g[k][i]!=INF&&!vis[i]){
if(dis[i]>dis[k]+g[k][i]){
dis[i]=dis[k]+g[k][i];
c[i]=c[k]+cost[i];
num[i]=num[k];
//记录经过点的个数
pt[i]=pt[k]+1;
pre[i]=k;
} //没看题 先点权 后平均点权
else if(dis[i]==dis[k]+g[k][i]){
num[i]+=num[k];
if(c[i]<c[k]+cost[i]){
c[i]=c[k]+cost[i];
pre[i]=k;
pt[i]=pt[k]+1;
}
else if(c[i]==c[k]+cost[i]){
double kavg=(c[k]+cost[i])*1.0/(pt[k]+1);
double iavg=c[i]*(1.0)/pt[i];
if(kavg>iavg){
pre[i]=k;
pt[i]=pt[k]+1;
}
}



}
}
}
}

}

void dfs(int end){
//st(HZH)->-1 终止 HZH(->PRS ->ROM)
if(pre[end]==-1) {
cout<<is[end];
return;
}
dfs(pre[end]);
cout<<"->";//!
cout<<is[end];


}
int main(){
string st;
cin>>n>>m;
cin>>st;
fill(g[0],g[0]+MAX*MAX,INF);
for(int i=0;i<n-1;i++){
string str;int x;
cin>>str;
cin>>x;
int id=strToInt(str);
cost[id]=x;
}
for(int j=0;j<m;j++){
string chu,dao;
cin>>chu>>dao;
int stt=strToInt(chu),edd=strToInt(dao);
cin>>g[stt][edd];
g[edd][stt]=g[stt][edd];
}
int beg=strToInt(st),end=strToInt("ROM");
dij(beg,end);
cout<<num[end]<<" "<<dis[end]<<" "<<c[end]<<" "<<c[end]/pt[end]<<endl;//注意先后顺序
dfs(end);
}