先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!)。如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 选 编号小的。
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
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 #define MAX 1050 6 int INF = 1000000; 7 8 struct Station 9 { 10 bool ifis; 11 double MIN; 12 double Avg; 13 }; 14 15 int Grap[MAX][MAX]; 16 17 Station Sta[11]; 18 19 bool visit[MAX]; 20 21 int d[MAX]; 22 23 void Dijkstra(int begin,int NodeNum) 24 { 25 d[begin] = 0; 26 for(int i = 0;i<NodeNum ;i++) 27 { 28 int index = -1; 29 int MIN = INF; 30 for(int j = 1;j <= NodeNum ;j++) 31 { 32 if(!visit[j] && MIN > d[j]) 33 { 34 MIN = d[j]; 35 index = j; 36 } 37 } 38 39 if(index== -1) return ; 40 41 visit[index] = true ; 42 43 for(int v = 1; v <= NodeNum ;v++) 44 { 45 if(!visit[v] && Grap[index][v]!=INF) 46 { 47 if(d[index]+ Grap[index][v] < d[v]) 48 { 49 d[v] = d[index]+ Grap[index][v]; 50 } 51 } 52 } 53 } 54 } 55 56 int main() 57 { 58 int i,j,N,M,K,Ds,Dtem,x,y ; 59 scanf("%d%d%d%d",&N,&M,&K,&Ds); 60 char dis1[10],dis2[10]; 61 62 for(i = 1 ;i <= N + M ;i++) 63 { 64 for(j = 1 ;j <= N + M ;j++) 65 { 66 Grap[i][j] = INF ; 67 } 68 } 69 70 for(i = 1 ; i<= M ;i++) 71 Sta[i].ifis = true; 72 73 for( i = 0;i <K;i++) 74 { 75 scanf("%s%s%d",dis1,dis2,&Dtem); 76 if(dis1[0]!='G') x = atoi(dis1); 77 else 78 { 79 char tem[10]; 80 for(j = 1; j <strlen(dis1);j++) 81 tem[j-1] = dis1[j]; 82 tem[j-1] = '