标签:
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
思路: 这道题需要细心,首先是这句话,
the minimum distance between the station and any of the residential housing is as far away as possible. //any of其中的任何一个。
求最小距离的最大值 ------第一尺度
求平均距离的最小值 ------第二尺度
求序列号最小的 ------第三尺度
求这些尺度时以往的题是在迪杰斯特拉内部实现,而此回不是,利用dij产生的距离数组进行数据上的统计。
其中第三尺度利用数组顺序遍历时内部自然实现。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 #define MAX 1020 //需要设置成1020 因为加油站加城市一共可能是1010个 7 const int INF=0x3fffffff; 8 int G[MAX][MAX]; 9 int d[MAX]; 10 bool visited[MAX]; 11 int N,M,K,D; 12 13 void Dij(int id) 14 { 15 memset(visited,false,sizeof(visited)); 16 fill(d,d+MAX,INF); 17 d[id]=0; 18 bool flag=true; //检验是否满足第一要求 19 for(int i=0;i<N+M;i++) 20 { 21 int min=INF,minpt=-1; 22 for(int j=1;j<=N+M;j++) 23 { 24 if(!visited[j]&&d[j]<min) 25 { 26 minpt=j; 27 min=d[j]; 28 } 29 } 30 if(minpt==-1) 31 return; 32 visited[minpt]=true; 33 //update 34 for(int v=1;v<=N+M;v++) 35 { 36 if(!visited[v]&&G[minpt][v]!=INF&&d[minpt]+G[minpt][v]<d[v]) 37 { 38 d[v]=d[minpt]+G[minpt][v]; 39 } 40 } 41 42 } 43 } 44 int GetId(char str []) 45 { 46 int sum=0; 47 for(int i=0;str[i]!=‘\0‘;i++) 48 { 49 if(str[i]==‘G‘) 50 continue; 51 else 52 sum=sum*10+str[i]-‘0‘; 53 } 54 if(str[0]==‘G‘) 55 return N+sum; 56 else 57 return sum; 58 } 59 int main(int argc, char *argv[]) 60 { 61 scanf("%d%d%d%d",&N,&M,&K,&D); 62 fill(G[0],G[0]+MAX*MAX,INF); 63 char city1[5],city2[5]; 64 for(int i=0;i<K;i++) 65 { 66 //全部变成映射 67 int length; 68 scanf("%s %s %d",city1,city2,&length); 69 int id1=GetId(city1); 70 int id2=GetId(city2); 71 G[id1][id2]=G[id2][id1]=length; 72 } 73 double ansDis=-1,ansAvg=INF; //double 也可以弄成INF吗? 74 int ansId=-1; 75 for(int i=N+1;i<=N+M;i++) 76 { 77 double min=INF,avg=0; 78 Dij(i); 79 for(int j=1;j<=N;j++) 80 { 81 if(d[j]>D) 82 { 83 min=-1; 84 break; 85 } 86 avg+=d[j]; 87 if(d[j]<min) 88 { 89 min=d[j]; 90 } 91 } 92 if(min!=-1&&min>ansDis) 93 { 94 ansId=i; 95 ansAvg=avg; 96 ansDis=min; 97 } 98 else if(min==ansDis&&min!=-1&&avg<ansAvg) 99 { 100 ansId=i; 101 ansAvg=avg; 102 ansDis=min; 103 } 104 } 105 if(ansId==-1) 106 printf("No Solution\n"); 107 else 108 { 109 printf("G%d\n",ansId-N); 110 printf("%.1lf %.1lf\n",ansDis,ansAvg/N); 111 } 112 return 0; 113 }
标签:
原文地址:http://www.cnblogs.com/GoFly/p/4306567.html