PAT Advanced 1072 Gas Station (30 )
题目描述
A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range.
Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendation. If there are more than one solution, output the one with the smallest average distance to all the houses. If such a solution is still not unique, output the one with the smallest index number.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive integers: N (≤
10
3
10^3
103), the total number of houses; M (≤10), the total number of the candidate locations for the gas stations; K (≤
1
0
4
10^4
104), the number of roads connecting the houses and the gas stations; and DS, the maximum service range of the gas station. It is hence assumed that all the houses are numbered from 1 to N, and all the candidate locations are numbered from G
1 to G
M.
Then K lines follow, each describes a road in the format
P1 P2 Dist
where P1
and P2
are the two ends of a road which can be either house numbers or gas station numbers, and Dist
is the integer length of the road.
Output Specification:
For each test case, print in the first line the index number of the best location. In the next line, print the minimum and the average distances between the solution and all the houses. The numbers in a line must be separated by a space and be accurate up to 1 decimal place. If the solution does not exist, simply output No Solution
.
Sample Input:
4 3 11 5
1 2 2
1 4 2
1 G1 4
1 G2 3
2 3 2
2 G2 1
3 4 2
3 G3 2
4 G1 3
G2 G1 1
G3 G2 2
Sample Output:
G1
2.0 3.3
解题思路
事实证明心烦意乱不适合写题。。补充一点,PAT里英语阅读很重要。。A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible.
硬是被我理解成了,加油站离居民区越近越好。。于是离真相越来越远。。论审题的重要性。以下给出Google翻译。。
必须在这样的位置建造加油站,使得车站与任何住宅之间的最小距离尽可能远。
然后自己写dijkstra算法的时候一个vis[v]写成了vis[i]。。关键是样例还过了。。于是提交上去,有几个测试点没过。。一开始以为是舍入的问题,用的是printf("%.1f", avg);
发现对于一位小数的四舍五入%.1f
有四舍六入五成双的说法,于是强加上0.05,无果。后来检查发现写错了。。写复杂一点的代码确实需要心静一点。
Code
- AC代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+11;
const int inf = 0x3f3f3f3f;
int G[maxn][maxn], vis[maxn], dis[maxn];
vector<int> edge[maxn];
unordered_map<string, int> m;
int minIdx = -1, minD;
double minA;
void dijkstra(int root, int N, int M, int Ds) {
memset(vis, 0, sizeof(vis));
memset(dis, inf, sizeof(dis));
dis[root] = 0;
while(true) {
int minDis = inf, u = -1;
for(int i = 1; i<=N+M; i++) {
if(!vis[i] && dis[i] < minDis) {
minDis = dis[u = i];
}
}
if(u == -1) break;
vis[u] = 1;
for(int i = 0; i<edge[u].size(); i++) {
int v = edge[u][i];
if(!vis[v] && G[u][v] != inf && dis[v] > dis[u]+G[u][v]) {
dis[v] = dis[u] + G[u][v];
}
}
}
int temMin = inf;
double sum = 0.0;
for(int i = 1; i<=N; i++) {
if(dis[i] > Ds) return;
temMin = min(temMin, dis[i]);
sum += dis[i]*1.0;
}
double avg = sum/N;
if(minIdx == -1 || minD < temMin) {
minIdx = root;
minD = temMin;
minA = sum/N;
} else if(minD == temMin && minA > avg) {
minIdx = root;
minA = avg;
}
}
int main() {
//freopen("in.txt", "r", stdin);
int N, M, K, Ds, d, u, v;
cin >> N >> M >> K >> Ds;
memset(G, inf, sizeof(G));
for(int i = 1; i<=M; i++) {
m["G"+to_string(i)] = N+i;
}
string str1, str2;
for(int i = 0; i<K; i++) {
cin >> str1 >> str2 >> d;
u = str1[0]=='G' ? m[str1] : stoi(str1);
v = str2[0]=='G' ? m[str2] : stoi(str2);
G[u][v] = G[v][u] = d;
edge[u].push_back(v);
edge[v].push_back(u);
}
for(int i = N+1; i<=N+M; i++) {
dijkstra(i, N, M, Ds);
}
if(minIdx == -1) {
printf("No Solution\n");
} else {
printf("G%d\n", minIdx-N);
printf("%.1f %.1f\n", minD*1.0, minA);
}
return 0;
}