码迷,mamicode.com
首页 > 其他好文 > 详细

PAT 1072 Gas Station (30)

时间:2018-06-28 18:23:03      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:ota   color   where   IV   ice   with   AC   bre   字符串   

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)

标签:ota   color   where   IV   ice   with   AC   bre   字符串   

原文地址:https://www.cnblogs.com/mr-stn/p/9239537.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!