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 (≤), the total number of houses; M (≤), the total number of the candidate locations for the gas stations; K (≤), 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 G
1 to G
M.
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
#include<bits/stdc++.h> using namespace std; const int maxn=1111; #define eps 1e-8 #define inf 0x3fffffff int G[maxn][maxn]; bool vis[maxn]; int dist[maxn]; int n,m,k,t; void dijkstra(int index){ fill(dist,dist+maxn,inf); fill(vis,vis+maxn,false); dist[index]=0; for(int i=1;i<=n+m;i++){ int mind=-1,minv=inf; for(int i=1;i<=n+m;i++){ if(vis[i]==false&&dist[i]<minv){ minv=dist[i]; mind=i; } } int u=mind; if(u==-1){ return ; } vis[u]=true; for(int v=1;v<=m+n;v++){ if(vis[v]==false&&G[u][v]!=inf&&dist[u]+G[u][v]<dist[v]){ dist[v]=dist[u]+G[u][v]; } } } } int main(){ cin>>n>>m>>k>>t; string a,b; int a1,b1; int c; fill(G[0],G[0]+maxn*maxn,inf); for(int i=0;i<k;i++){ cin>>a>>b>>c; a1=b1=0; if(a[0]=='G'){ for(int x=1;x<a.length();x++){ a1=a1*10+(a[x]-'0'); } a1=a1+n; } else{ for(int x=0;x<a.length();x++){ a1=a1*10+(a[x]-'0'); } } if(b[0]=='G'){ for(int x=1;x<b.length();x++){ b1=b1*10+(b[x]-'0'); } b1=b1+n; } else{ for(int x=0;x<b.length();x++){ b1=b1*10+(b[x]-'0'); } } G[a1][b1]=G[b1][a1]=c; } double ansdis=-1; int ansinx=-1; double ansavg=inf; for(int i=n+1;i<=n+m;i++){ dijkstra(i); double avg=0,mindd=inf; for(int j=1;j<=n;j++){ if(dist[j]>t){ mindd=-1; break; } if(dist[j]<mindd){ mindd=dist[j]; } avg+=1.0*dist[j]/n; } if(mindd==-1){ continue; } if(mindd>ansdis){ ansdis=mindd; ansinx=i; ansavg=avg; } else if(mindd==ansdis){ if(avg<ansavg){ ansinx=i; ansavg=avg; } } } if(ansdis==-1){ printf("No Solution "); } else{ printf("G%d ",ansinx-n); printf("%.1f %.1f ",ansdis,ansavg); } return 0; }