1072. Gas Station

题目

https://pintia.cn/problem-sets/994805342720868352/problems/994805396953219072

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 ), the total number of houses; M ( ≤10 ), the total number of the candidate locations for the gas stations; K ( ≤10^4 ), the number of roads connecting the houses and the gas stations; and D_S, 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 G1 to GM.

Then K lines follow, each describes a road in the format

1
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 1:

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

1
2
G1
2.0 3.3

Sample Input 2:

1
2
3
2 1 2 10
1 G1 9
2 G1 20

Sample Output 2:

1
No Solution

想法

找的距离最近house最远的比较容易

找距离最远的house最近的话,需要遍历

还要计算平均距离,那么肯定要遍历一遍了

又是Dijkstra,加一堆麻烦的东西,唉,这种题要写很久orz

答案

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
//
// Created by Zhao Xiaodong on 2018/8/24.
//
#include <queue>
#include <vector>
#include <algorithm>
#include <iostream>
#include <climits>
using namespace std;
int main() {
int N, M, K, D;
scanf("%d %d %d %d", &N, &M, &K, &D);
vector<vector<int>> g(M + N + 1, vector<int>(M + N + 1, INT_MAX));
for (int i = 0; i < K; i++) {
char p1[10];
char p2[10];
int dist;
int pi1, pi2;
scanf("%s %s %d", &p1, &p2, &dist);
if (isdigit(p1[0])) {
pi1 = stoi(p1);
} else {
pi1 = stoi(p1 + 1) + N;
}
if (isdigit(p2[0])) {
pi2 = stoi(p2);
} else {
pi2 = stoi(p2 + 1) + N;
}
g[pi1][pi2] = g[pi2][pi1] = dist;
}
int bestGS = -1;
double maxMinDist = -1.0;
double minAvgDist = 1000000000;
for (int gs = N + 1; gs <= N + M; gs++) {
vector<int> dist(N + M + 1, INT_MAX);
vector<int> done(N + M + 1, false);
dist[gs] = 0;
for (int i = 1; i < N + M + 1; i++) {
int minD = INT_MAX;
int curr = 0;
for (int j = 1; j < N + M + 1; j++) {
if (!done[j] && dist[j] < minD) {
minD = dist[j];
curr = j;
}
}
done[curr] = true;
for (int k = 1; k < N + M + 1; k++) {
if (g[curr][k] != INT_MAX && dist[curr] + g[curr][k] < dist[k]) {
dist[k] = dist[curr] + g[curr][k];
}
}
}
int totalDist = 0;
int minDist = INT_MAX;
bool isValid = true;
for (int i = 1; i < N + 1; i++) {
if (dist[i] > D) {
isValid = false;
break;
}
totalDist += dist[i];
if (dist[i] < minDist)
minDist = dist[i];
}
if (isValid) {
if (minDist > maxMinDist) {
maxMinDist = minDist;
bestGS = gs - N;
minAvgDist = 1.0 * totalDist / N;
} else if (minDist == maxMinDist) {
if (1.0 * totalDist / N < minAvgDist) {
bestGS = gs - N;
minAvgDist = 1.0 * totalDist / N;
}
}
}
}
if (bestGS != -1) {
printf("G%d\n", bestGS);
printf("%.1f %.1f", maxMinDist, minAvgDist);
} else {
printf("No Solution");
}
return 0;
}