PAT 1072. Gas Station (30)
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
此题不难,只是考察了最简单的Dijkstra算法,读清楚题意即可,代码如下:
1 #include<iostream> 2 #include<string> 3 #include<iomanip> 4 #include"stdio.h" 5 using namespace std; 6 7 int N,M,K,Ds; 8 int **G; 9 10 bool *collected; 11 int *dist; 12 13 void Dijkstra(int); 14 int GetMinDist(); 15 16 int main() 17 { 18 scanf("%d%d%d%d",&N,&M,&K,&Ds); 19 20 //初始化G 21 G = new int* [N+M]; 22 dist = new int [N+M]; 23 collected = new bool [N+M]; 24 for (int i=0;i<N+M;i++) 25 { 26 G[i] = new int [N+M]; 27 collected[i] = false; 28 dist[i] = 65536; 29 } 30 for (int i=0;i<N+M;i++) 31 for (int j=0;j<N+M;j++) 32 G[i][j] = 0; 33 34 int iP1,iP2; 35 string P1,P2; 36 int Dist; 37 38 //开始输入道路信息 39 for (int i=0;i<K;i++) 40 { 41 cin >> P1 >> P2 >> Dist; 42 if (P1[0] == 'G') 43 { 44 P1.erase(P1.begin()); 45 iP1 = atoi(P1.c_str())+N-1; 46 } 47 else 48 iP1 = atoi(P1.c_str())-1; 49 50 if (P2[0] == 'G') 51 { 52 P2.erase(P2.begin()); 53 iP2 = atoi(P2.c_str())+N-1; 54 } 55 else 56 iP2 = atoi(P2.c_str())-1; 57 58 G[iP1][iP2] = Dist; 59 G[iP2][iP1] = Dist; 60 } 61 62 //开始Dijkstra 63 int mind=65536; 64 double averd=0.; 65 bool flag_all = true; 66 int minG=-1,mindd=-1; 67 double averdd=-1.; 68 69 for (int i=N;i<N+M;i++) //对每一个G进行Dijkstra 70 { 71 Dijkstra(i); 72 //对dist[]进行扫描,找出最小和平均值,或者没有 73 for (int j=0;j<N;j++) 74 { 75 averd += dist[j]; 76 if (mind > dist[j]) 77 mind = dist[j]; 78 if (dist[j] > Ds) 79 { 80 flag_all = false; 81 break; 82 } 83 } 84 if (flag_all) 85 { 86 averd = averd / double(N); 87 if (mindd < mind) 88 { 89 mindd = mind; 90 minG=i; 91 averdd = averd; 92 } 93 else if (mindd == mind) 94 { 95 if (averdd > averd) 96 { 97 minG=i; 98 averdd = averd; 99 } 100 } 101 } 102 103 //复原各变量 104 for (int j=0;j<N+M;j++) 105 { 106 collected[j] = false; 107 dist[j] = 65536; 108 } 109 mind=65536; 110 averd=0.; 111 flag_all = true; 112 } 113 114 if (minG == -1) 115 cout << "No Solution" << endl; 116 else 117 { 118 cout << 'G' << minG-N+1 << endl; 119 cout << fixed << setprecision(1) << double(mindd) << ' ' << fixed << setprecision(1) << averdd << endl; 120 } 121 122 return 0; 123 } 124 125 126 void Dijkstra(int S) 127 { 128 //初始化源节点 129 collected[S]=true; 130 dist[S]=0; 131 132 //初始化与S相邻的节点 133 for (int i=0;i<N+M;i++) 134 { 135 if (G[S][i]) 136 { 137 dist[i]=G[S][i]; 138 } 139 } 140 141 //开始贪心算法 142 int V; 143 while (1) 144 { 145 V = GetMinDist(); 146 if (V == -1) 147 break; 148 collected[V]=true; 149 150 for (int i=0;i<N+M;i++) 151 { 152 if (!collected[i] && G[V][i] && dist[i] > dist[V]+G[V][i]) 153 dist[i] = dist[V]+G[V][i]; 154 } 155 } 156 } 157 158 int GetMinDist() 159 { 160 int min_d = 65536,min_d_i=-1; 161 162 for (int i=0;i<N+M;i++) 163 if (!collected[i] && dist[i] < min_d) 164 { 165 min_d = dist[i]; 166 min_d_i = i; 167 } 168 return min_d_i; 169 }
浙公网安备 33010602011771号