当前位置: 代码迷 >> 综合 >> PAT Advanced 1072 Gas Station(图,最短路)
  详细解决方案

PAT Advanced 1072 Gas Station(图,最短路)

热度:55   发布时间:2023-12-09 20:33:08.0

链接:PAT Advanced 1072

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



题意:

有N所居民房、M个加油站待建点以及K条无向边。现在要从M个加油站待建点中选出一个来建造加油站,使得该加油站距离最近的居民房尽可能远,且必须保证所有房子与该加油站的距离都不超过给定的服务范围DS。现在给出N、M、K、DS,以及K条无向边的端点及边权,输出应当选择的加油站编号、与该加油站最近的居民房的距离、该加油站距离所有居民房的平均距离。(如果有多个最近距离相同的解,那么选择平均距离最小的;如果平均 距离也相同,则选择编号最小的。)



思路:

居民房的编号是1 ~ N,加油站的编号是1 ~ M,但是他们都处于同一个图中,所以为方便处理,加油站的编号全部+N,图的总结点数则为N+M。

遍历这M个加油站,算出每一个的 距离最近的居民房的距离该加油站距离所有居民房的平均距离(为方便起见,计算距离之和即可),根据题意筛选答案即可。



#include<cstdio>
#include<iostream>
#include<queue>
#include<string>
#include<algorithm> 
using namespace std;
const int INF=1e9;
const int maxn=2000;
struct Node
{
    int num;int d
  相关解决方案