## 1072 Gas Station (30 points)

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

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

Solve the problem.
Store G1-GM in the subscript position
of N + 1-N + M; calculate Dijkstra's algorithm for N + 1-N + M;
propose the shortest time that the gas station goes to each city after each calculation distance, average distance and a longest distance;
first meet the longest distance Ds is smaller than the service area;
find the shortest distance from the position of maximum gas station;
if the shortest distance is the same as the average minimum distance to find the final location;
the specimens to find it from front to back , So no need to make judgments;

A structure is defined, resulting in a relatively large space occupation;

``````#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
#define MAXN 1020     //最大house数
#define MAXM 11      //最大候选点数
#define INFINITY 0x3fffffff
//把Gi保存在N后面
int N,M,K,Ds;
int  Graph[MAXN+MAXM][MAXN+MAXM];

struct station{
int  Maxdis=0;
int Mindis=INFINITY;
double Avrdis;
};
station Dijkstra(int n)  //起始下标为n
{
int dist[MAXN+MAXM];
bool collected[MAXN+MAXM];
fill(dist+1,dist+MAXN+MAXM+1,INFINITY);
fill(collected+1,collected+1+MAXN+MAXM,0);

dist[n]=0;

while(1){
//找到最小值
int min=INFINITY;
int v=-1;
for(int i=1;i<=N+M;i++)
if(!collected[i]&&dist[i]<min)
{
min=dist[i];
v=i;
}
//找到dist最小值
if(v==-1) break;
collected[v]=1;
for(int i=1;i<=N+M;i++)
{
if(!collected[i]){
if(dist[v]+Graph[v][i]<dist[i])
dist[i]=dist[v]+Graph[v][i];
}
}
}
station s;
int sum=0;
for(int i=1;i<=N;i++)      //判断到house的距离，不用判断到station的距离
{
if(i==n) continue;
if(s.Maxdis<dist[i]) s.Maxdis=dist[i];    //最大值
if(s.Mindis>dist[i]) s.Mindis=dist[i];    //最小值
sum+=dist[i];
}
s.Avrdis=((double)sum/N);

return s;

}

int main()
{

fill(Graph[0],Graph[0]+(MAXN+MAXM)*(MAXN+MAXM),INFINITY);
cin>>N>>M>>K>>Ds;
for(int i=0;i<K;i++)
{
string c1,c2;
int s;
cin>>c1>>c2>>s;
int a,b;
if(c1[0]=='G'){
a=stoi(c1.substr(1))+N;
}
else a=stoi(c1);
if(c2[0]=='G'){
b=stoi(c2.substr(1))+N;
}
else b=stoi(c2);
Graph[a][b]=s;
Graph[b][a]=s;
}                       //输入

//Graph为各点距离
//求每个加油站到居民区的最短距离
//Gi下标为i+N

int maxD=-1;
int minA=INFINITY;
int result;
station s1;

for(int i=1;i<=M;i++){
station s=Dijkstra(i+N);
if(s.Maxdis>Ds) continue;    //最大值过大——找下一个点
if(s.Mindis>maxD){      //找最小值最大的
maxD=s.Mindis;
result=i;
s1=s;
}
if(s.Mindis==maxD) //若相等
{
if(s.Avrdis<s1.Avrdis) //找平均值最小的
{
result=i;
s1=s;
}
}
}
if(maxD==-1)
printf("No Solution");
else{
printf("G%d\n%d.0 %.1f",result,s1.Mindis,s1.Avrdis);
}
//首先满足最大距离不超过Ds——要得到最大值
//再找到G1-M中的最小距离的最大值——要得到最小值
//要得到平均值——平均值的最小值
//下标的最小值
}
``````

Sample answers 3.3 and 3.2 are correct for this question, no rounding is required;
note the
input method

``````	cin>>N>>M>>K>>Ds;
for(int i=0;i<K;i++)
{
string c1,c2;
int s;
cin>>c1>>c2>>s;
int a,b;
if(c1[0]=='G'){
a=stoi(c1.substr(1))+N;
}
else a=stoi(c1);
if(c2[0]=='G'){
b=stoi(c2.substr(1))+N;
}
else b=stoi(c2);
Graph[a][b]=s;
Graph[b][a]=s;
}
``````