1. 程式人生 > >PAT 1072 Gas Station (30)

PAT 1072 Gas Station (30)

esp 狀態 solution sam 不知道 specific 解決方案 sep ans

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 (<= 10^3^), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 10^4^), the number of roads connecting the houses and the gas stations; and D~S~, 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

題目大意:找出每個加油站到所有用戶的最小距離,找出最小距離最大的加油站;
註意點:這一題要維護的變量太多了, 所以取號變量名就比較重要了
節點是否訪問的狀態是在循環內改變的,不應該在循環外面改變
判斷是否有解決方案,是判斷離加油站最遠的用戶距離是否在加油站的服務範圍之類
好像犯了類似的錯誤啊, 最開始把起點和重點設置為長度為3的字符數組, 題中說用戶可能有1000個,所以字符串應該開到5; 不知道為啥把字符數組轉換成數字要出錯, 用string比較方便, 測試點最多1000個不會超時;

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<vector>
 4 #include<cstring>
 5 using namespace std;
 6 int n, m, k, MaxDis;
 7 const int inf = 99999999;
 8 vector<vector<int> > G(1020, vector<int>(1020, inf));
 9 vector<int> vis(1020, false), dis(1020, inf);
10 
11 int index(string s){
12   int ans=0;
13   if(s[0]==G){
14     s = s.substr(1);
15     ans = n +stoi(s);
16   }
17   else ans = stoi(s);
18   return ans;
19 }
20 int main(){
21   scanf("%d%d%d%d", &n, &m, &k, &MaxDis);
22   int i;
23   for(i=0; i<k; i++){
24     string a, b;
25     int length, idx1, idx2;
26     cin>>a>>b;
27     scanf("%d", &length);
28     idx1 = index(a);
29     idx2 = index(b);
30     G[idx1][idx2] = G[idx2][idx1] = length;
31   }
32   double finalMinDis = 0, finalAvg=inf;
33   int gIdx;
34   for(i=1; i <= m; i++){
35     fill(vis.begin(), vis.end(), false);
36       fill(dis.begin(), dis.end(), inf);
37     dis[n+i] = 0;
38     double minDis=inf, totalDis=0, maxDis=0;
39     for(int j=1; j <= m+n; j++){
40       int minn = inf, u = -1;
41       for(int idx=1; idx <= m+n; idx++){
42         if(!vis[idx] && dis[idx] < minn){
43           minn = dis[idx];
44           u = idx;
45         }
46       }
47       if(u==-1) break;
48       vis[u] = true;
49       for(int v=1; v<=n+m; v++){
50         if(!vis[v] && G[u][v]!=inf){
51           if(dis[v]>dis[u]+G[u][v]){
52             dis[v]=dis[u]+G[u][v];
53           }
54         }
55       }
56     }
57     for(int s=1; s<=n; s++) {
58         totalDis += dis[s];
59         if(dis[s]>maxDis) maxDis = dis[s];
60         if(dis[s]<minDis) minDis = dis[s];
61     }
62     double tempAvg = totalDis*1.0/n;
63     if(minDis>finalMinDis && maxDis<=MaxDis){
64       finalMinDis = minDis;
65       finalAvg = tempAvg;
66       gIdx = i;
67     }else if(minDis==finalMinDis){
68       if(tempAvg<finalAvg){
69             finalMinDis = minDis;
70         finalAvg = tempAvg;
71         gIdx = i;
72       }
73     }
74   }
75   if(finalMinDis!=0) printf("G%d\n%.1f %.1f\n", gIdx, finalMinDis, finalAvg);
76   else printf("No Solution\n");
77   return 0;
78 }



PAT 1072 Gas Station (30)