PAT 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 (<= 103), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 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 G1 to GM. 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 1:

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:

G1
2.0 3.3

Sample Input 2:

2 1 2 10
1 G1 9
2 G1 20

Sample Output 2:

No Solution

题目要求找到一个加油站,这个加油站既要能够在规定的距离范围内服务所有的居民点,又要距离最近的居民点尽可能的远。如果存在多个,就选择距离所有居民点的平均距离最近的加油站。如果还存在多个,就选择加油站下标最小的一个。 规定0~N - 1为居民点,N ~ N + M- 1为加油站,用图数据结构表示,图的大小为(n + m) * (n + m)。 这里我用Dijkstra得到每一个加油站到其他居民点或其他加油站的距离dist,如果在dist的0~N中,存在dist[i] > ds(ds是加油站的最远服务距离),那么这个加油站就不满足条件的。如果最后都找不到加油站就输出No Solution。 用Dijkstra实现的代码:

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
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <cmath>

using namespace std;

const int inf = 999999;

int **graph;
bool *isVisited;
int *dist;

void initGraph(int n, int m) {
graph = new int *[n + m];
for (int i = 0; i < n + m; i++) {
graph[i] = new int[n + m];
for (int j = 0; j < n + m; j++) {
graph[i][j] = inf;
}
}
}

int toInt(char *p, int n) {
int num = 0, start = 0;
if (p[0] == 'G') start = 1;
for (int i = start; i < strlen(p); i++) {
num = num * 10 + p[i] - '0';
}
if (p[0] == 'G') return num - 1 + n;
else return num - 1;
}

void initStatus(int n, int m) {
if (isVisited == NULL) {
isVisited = new bool[n + m];
}
if (dist == NULL) {
dist = new int[n + m];
}
for (int i = 0; i < n + m; i++) {
isVisited[i] = false;
dist[i] = inf;
}
}

int findMinDist(int n, int m) {
int min = 0;
for (int i = 1; i < n + m; i++) {
if (isVisited[min] || (!isVisited[i] && dist[min] > dist[i])) {
min = i;
}
}
if (isVisited[min]) return -1;
else return min;
}

void dijkstra(int s, int n, int m) {
dist[s] = 0;
while (true) {
s = findMinDist(n, m);
if (s == -1) {
break;
}
isVisited[s] = true;
for (int i = 0; i < n + m; i++) {
if (dist[i] > dist[s] + graph[s][i]) {
dist[i] = dist[s] + graph[s][i];
}
}
}
}

void updateG(int g, int ds, int n, int &finalG, double &min, double &aver) {
double tmin = inf, taver = 0;
for (int i = 0; i < n; i++) {
if (dist[i] > ds || dist[i] == inf) {
return;
}
if (dist[i] < tmin) {
tmin = dist[i];
}
taver += dist[i] * 1.0 / n;
}


if (tmin > min) {
min = tmin;
aver = taver;
finalG = g;
} else if (abs(tmin - min) <= 0.00001) {
if (taver < aver) {
aver = taver;
finalG = g;
}
}
}

int main() {
int n = 0, m = 0, k = 0, ds = 0, temp = 0;
scanf("%d %d %d %d", &n, &m, &k, &ds);
initGraph(n, m);
for (int i = 0; i < k; i++) {
char p1[6], p2[6];
scanf("%s %s %d", p1, p2, &temp);
int p1i = toInt(p1, n);
int p2i = toInt(p2, n);
graph[p1i][p2i] = graph[p2i][p1i] = temp;
}
int finalG = n + m;
double min = -1, aver = inf;
for (int i = n; i < n + m; i++) {
initStatus(n, m);
dijkstra(i, n, m);
updateG(i, ds, n, finalG, min, aver);
}
if (finalG == n + m) {
printf("No Solution\n");
} else {
printf("G%d\n", finalG - n + 1);
printf("%.1f %.1f\n", min, aver);
}
return 0;
}