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 2Sample Output 1:
G1 2.0 3.3Sample Input 2:
2 1 2 10 1 G1 9 2 G1 20Sample Output 2:
No Solution
/**************************侯尼玛**************************/
一道简单的图算法题,我的做法是对每个Candidate节点做一次迪杰斯特拉算法,然后选择最优的节点。唯一需要注意的一点是这道题的节点选择条件比较绕:
(1)Candidate节点到每个house的最近距离都必须大于服务范围。
(2)从满足条件1的Candidate节点中选择离全部house的最近距离最远的那个(就像是在现实生活中建工厂,选址的时候肯定得在保证能服务到全部住户的情况 下离住户尽可能得远)
(3)从满足条件2的Candidate节点中选择离全部house的平均距离最近的那个(这道题有一个问题,就是题干中的测试例输出的平均值是经过四舍五入的,但是提交的代码中输出的平均值得直接去尾而不能四舍五入,否则第二个测试例不能通过。不知道是题目出错了还是我的代码有什么漏洞)
最后附上代码:
#include
#include
#include
#include
using namespace std;
const int INF = 0x3FFFFFFF;
bool Dijstra (int ** vertex,double &MinDistance,double &AverageDistance,int HouseNum,int CandidNum,int CandidLocal,int ServeRange)
{
int LocalNum = HouseNum+CandidNum;
bool * LocalVisited = new bool [LocalNum+1]();
int * PaceValue = new int [LocalNum+1];
for(int i=1;i<=LocalNum;i++)
PaceValue[i]=INF;
PaceValue[CandidLocal]=0;
for(int i= 0;i<=LocalNum;i++)
{
int MinPace=INF;
int s=-1;
for(int j=1;j<=LocalNum;j++)
if(PaceValue[j]ServeRange)
return false;
if(PaceValue[i]>STRLoc1>>STRLoc2>>TempValue;
INTLoc1=INTLoc2=0;
if(STRLoc1[0]=='G')
{
for(size_t s=1;sMinDistanceAll)
{
ChoosedLocal=HouseNum+i;
MinDistanceAll= MinDistance;
AverageDistanceAll=AverageDistance;
}
else if(MinDistance==MinDistanceAll && AverageDistance