A1087 All Roads Lead to Rome (30 point(s))

Dijskstra--最少花费,最大权重和,最少结点数,相同权重路径数

1. 原文

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
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:

1
2
3 3 195 97
HZH->PRS->ROM

2. 解析

测试❌:为无向图

1
v[b][a]=v[a][b];

字符串 - int转换:

1
2
3
4
5
6
7
8
9
10
11
int convert(char c[]){
string str=c;
if (strtoint[str]>0)
{
return strtoint[str];
}else{
strtoint[str]=idx;
intostr[idx]=str;
return idx++;
}
}

3. AC代码

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
#include<cstdio>
#include<map>
#include<string>
using namespace std;
const int maxn=210;
const int Inf=312312312;
map<string,int> strtoint;
map<int,string> intostr;
int weight[maxn]={};
int cost[maxn][maxn];
int co[maxn];
int w[maxn]={};
bool visit[maxn]={false};
int num[maxn]={};
int pre[maxn]={};
int cnt[maxn]={};
int idx=1;
int convert(char c[]){
string str=c;
if (strtoint[str]>0)
{
return strtoint[str];
}else{
strtoint[str]=idx;
intostr[idx]=str;
return idx++;
}
}
char str[4],str2[4];
int s=0; int key=0;
void dfs(int t){
if (t==s)
{
return;
}else{
dfs(pre[t]);
}
printf("->%s",intostr[t].c_str());
}
int main()
{
fill(cost[0],cost[0]+maxn*maxn,Inf);
int n,k;
scanf("%d%d%s",&n,&k,str);
s=convert(str);
for (int i = 0; i < n-1; ++i)
{
scanf("%s",str);
scanf("%d",&weight[convert(str)]);
}
for (int i = 0; i < k; ++i)
{
scanf("%s%s",str,str2);
int a=convert(str);
int b=convert(str2);
scanf("%d",&cost[a][b]);
cost[b][a]=cost[a][b];
}
fill(co,co+maxn,Inf);
fill(num,num+maxn,Inf);
co[s]=0;
num[s]=1;
w[s]=0;
cnt[s]=1;
for (int i = 1; i <idx ; ++i)
{
int u=-1; int min=Inf;
for (int j = 1; j < idx; ++j)
{
if (visit[j]==false&&co[j]<min)
{
u=j;
min=co[j];
}
}
if (u==-1)
{
break;
}
visit[u]=true;
for (int v = 1; v < idx; ++v)
{
if (visit[v]==false&&cost[u][v]!=Inf)
{
if (co[u]+cost[u][v]<co[v])
{
co[v]=co[u]+cost[u][v];
num[v]=num[u]+1;
w[v]=w[u]+weight[v];
pre[v]=u;
cnt[v]=cnt[u];
}else if(co[u]+cost[u][v]==co[v]){
if (w[u]+weight[v]>w[v])
{
w[v]=w[u]+weight[v];
num[v]=num[u]+1;
pre[v]=u;
}else if(w[u]+weight[v]==w[v]){
if (num[u]+1<num[v])
{
num[v]=num[u]+1;
pre[v]=u;
}
}
cnt[v]+=cnt[u];
}
}
}
}
int t=strtoint["ROM"];
printf("%d %d %d %d\n", cnt[t],co[t],w[t],(int)1.0*w[t]/(num[t]-1));
printf("%s",intostr[s].c_str());
dfs(t);
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1087/
  • 发布时间: 2019-09-03 12:31
  • 更新时间: 2021-10-29 14:04
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!