pat 1072 gas station

一点教训

注释部分出错,逻辑没有理清,其他的基本上很常规

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <sstream>
#define MAX 1020 //居民房与加油站的个数总和可以有1010,因此数组大小需要大于1010
#define INF 1000000000
using namespace std;
int n , m , k , Ds;
int G[MAX][MAX];
map<string , int> stringToInt;
map<int , string> intToString;
int numGas;
bool vis[MAX] = {false};
double d[MAX];
int pre[MAX] = {0};
void Dijstra(int s)
{
fill(d , d+ MAX , INF);
fill(vis , vis + MAX , false);
d[s] = 0;
int i, j;
for(i = 0 ; i < numGas ; ++i)
pre[i] = i;
for(i = 0 ; i < numGas ; ++i)
{
int u = -1 , min = INF;
for(j = 1 ; j <= numGas ; ++j)
{
if(vis[j] == false && min > d[j])
{
u = j;
min = d[j];
}
}
if(u == -1)
return;
vis[u] = true;
for(j = 1 ; j <= numGas ; ++j)
{
if(vis[j] == false && G[u][j] != INF)
{
if(d[u] + G[u][j] < d[j])
{
d[j] = d[u] + G[u][j];
pre[j] = u;
}
}
}
}
}
int change(string x)
{
if(stringToInt.find(x) != stringToInt.end())
{
return stringToInt[x];
}
else
{
if(x[0] == 'G')
{
stringToInt[x] = ++numGas;
intToString[numGas] = x;
return numGas;
}
else
{
stringstream ss;
int index;
ss<<x;
ss>>index;
stringToInt[x] = index;
intToString[index] = x;
return index;
}
}
}
bool isC = false;
int main()
{
cin>>n>>m>>k>>Ds;
fill(G[0] , G[0] + MAX * MAX , INF);
numGas = n;
int i;
for(i = 0 ; i < k ; ++i)
{
int dis;
string a , b;
cin>>a>>b>>dis;
int id1 = change(a);
int id2 = change(b);
G[id1][id2] = dis;
G[id2][id1] = G[id1][id2];
}
/*int maxIndex = -1 , minIndex = -1 ;
double avgR = 0 ,maxRoute = 0 ,minRoute = INF ,minAvg = INF;
int minAvgIndex = -1;
for(i = n+1 ; i <= numGas ; ++i)
{
Dijstra(i);
// DFS(1 , i);
for(int j = 1 ; j <= n ; ++j)
{
avgR += d[j];
if(d[j] > Ds)
{
isC = true;
break;
}
// cout<<d[j]<<" ";
if(minRoute > d[j])
{
minRoute = d[j];
minIndex = j;
}
}
if(isC)
{
minRoute = INF;
minIndex = -1;
isC = false;
continue;
}
//cout<<endl;
avgR /= n;
if(maxRoute < minRoute)
{
maxRoute = minRoute;
maxIndex = i;
minAvg = avgR;
}
else if(maxRoute == minRoute)
{
if(minAvg > avgR)
{
minAvg = avgR;
maxIndex = i;
}
else if(minAvg == avgR)
{
if(i < maxIndex)
{
maxIndex = i;
}
}
}
avgR = 0;
}
if(maxIndex == -1)
{
cout<<"No Solution";
}
else
{
cout<<intToString[maxIndex]<<endl;
printf("%.1f %.1f" , maxRoute , minAvg);
}
*/
//ansDis 存放使最大的最短距离
//ansAvg 存放最小的平均距离,ansID存放最终加油站id
double ansDis = -1 , ansAvg = INF;
int ansID = -1;
for(i = n +1 ; i <= n+m ; ++i)
{
double minDis = INF , avg = 0;
Dijstra(i);
for(int j = 1 ; j <= n ; ++j)
{
if(d[j] > Ds)
{
minDis = -1;
break;
}
if(d[j] < minDis)
minDis = d[j];
avg += 1.0 * d[j]/n;
}
if(minDis == -1)
continue;
if(minDis > ansDis)
{
ansID = i;
ansDis = minDis;
ansAvg = avg;
}
else if(minDis == ansDis && avg < ansAvg)
{
ansID = i;
ansAvg = avg;
}
}
if(ansID == -1)
printf("No Solution");
else
{
cout<<intToString[ansID]<<endl;
printf("%.1f %.1f" , ansDis , ansAvg);
}
return 0;
}

热评文章