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
1 // hahaha.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <stdio.h> 6 #include <iostream> 7 #include <vector> 8 #include <map> 9 #include <string> 10 #include <cstdio> 11 #include <set> 12 #include <algorithm> 13 #include <string.h> 14 using namespace std; 15 const int maxn=1020; 16 int n,m,k,ds; 17 18 int G[maxn][maxn]; 19 20 int getid(char a[]) 21 { 22 int len=strlen(a); 23 int id=0; 24 for(int i=0;i<len;i++) 25 { 26 if(a[i]!='G') 27 { 28 id=id*10+a[i]-'0'; 29 } 30 } 31 if(a[0]=='G')return id+n; 32 else 33 return id; 34 } 35 const int INF=1000000000; 36 int d[maxn]; 37 int vis[maxn]={false}; 38 void dij(int s) 39 { 40 memset(vis,false,sizeof(vis)); 41 fill(d,d+maxn,INF); 42 d[s]=0; 43 for(int i=1;i<=m+n;i++) 44 { 45 int u=-1,min=INF; 46 for(int j=1;j<=m+n;j++) 47 { 48 if(d[j]<min && vis[j]==false) 49 { 50 u=j; 51 min=d[j]; 52 } 53 } 54 if(u==-1)return; 55 vis[u]=true; 56 for(int k=1;k<=n+m;k++) 57 { 58 if(vis[k]==false&&G[u][k]!=INF) 59 { 60 if(G[u][k]+d[u]<d[k]) 61 { 62 d[k]=G[k][u]+d[u]; 63 } 64 } 65 } 66 } 67 } 68 69 70 71 int main() 72 { 73 scanf("%d %d %d %d",&n,&m,&k,&ds); 74 75 fill(G[0],G[0]+maxn*maxn,INF); 76 for(int i=0;i<k;i++) 77 { 78 char p1[15],p2[15]; 79 int dist; 80 scanf("%s %s %d",p1,p2,&dist); 81 int id1=getid(p1); 82 int id2=getid(p2); 83 G[id1][id2]=dist; 84 G[id2][id1]=dist; 85 86 } 87 //输入完毕 88 89 //处理图,对加油站处理,求平均距离,和最大的最近距离 90 double ansdis=-1,ansavg=INF; 91 int ansid=-1; 92 for(int i=n+1;i<=n+m;i++) 93 { 94 double mindis=INF,avg=0; 95 bool flag=false; 96 double sum=0; 97 dij(i); 98 for(int j=1;j<=n;j++) 99 { 100 if(d[j]>ds) 101 { 102 flag=true; 103 break; 104 } 105 if(d[j]<mindis)mindis=d[j]; 106 sum+=d[j]; 107 } 108 if(flag==true)continue; 109 110 avg=sum/n; 111 if(mindis>ansdis) 112 { 113 ansid=i; 114 ansdis=mindis; 115 ansavg=avg; 116 } 117 else if(mindis==ansdis&&avg<ansavg) 118 { 119 ansid=i; 120 ansavg=avg; 121 } 122 } 123 124 if(ansid==-1)printf("No Solution "); 125 else 126 { 127 printf("G%d ",ansid-n); 128 printf("%.1f %.1f ",ansdis,ansavg); 129 } 130 return 0; 131 }