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