PAT_A1072#Gas Station

博客围绕加油站选址问题展开,需找到使加油站与住宅最小距离尽可能远且所有房子在服务范围内的最佳位置。给出输入输出规格、示例,关键在于最短路径求解,还提醒加油站可作中间结点,最后给出代码来源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Source:

PAT A1072 Gas Station (30 分)

Description:

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 (≤), the total number of houses; M (≤), the total number of the candidate locations for the gas stations; K (≤), 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

Keys:

Attention:

  • 加油站可以作为中间结点

Code:

  1 /*
  2 Data: 2019-06-18 17:04:24
  3 Problem: PAT_A1072#Gas Station
  4 AC: 43:23
  5 
  6 题目大意:
  7 加油站选取的最佳位置,在服务范围内,离居民区的最短距离尽可能的远
  8 如果最佳位置不唯一,挑选距离居民区平均距离最近,且编号最小的位置
  9 输入:
 10 第一行给出,房子数N,候选加油站数M,总路径数K,最大服务范围Ds
 11 接下来K行,v1,v2,dist
 12 输出:
 13 最佳位置编号
 14 最小距离,平均距离(一位小数)
 15 没有则No Solution
 16 */
 17 #include<cstdio>
 18 #include<string>
 19 #include<iostream>
 20 #include<vector>
 21 #include<algorithm>
 22 using namespace std;
 23 const int M=1e3+20,INF=1e9;
 24 int grap[M][M],vis[M],d[M];
 25 int n,m;
 26 
 27 int Str(string s)
 28 {
 29     if(s[0] == 'G')
 30     {
 31         s = s.substr(1);
 32         return n+atoi(s.c_str());
 33     }
 34     else
 35         return atoi(s.c_str());
 36 }
 37 
 38 void Dijskra(int s)
 39 {
 40     fill(d,d+M,INF);
 41     fill(vis,vis+M,0);
 42     d[s]=0;
 43     for(int i=1; i<=n+m; i++)
 44     {
 45         int u=-1, Min=INF;
 46         for(int j=1; j<=n+m; j++)
 47         {
 48             if(vis[j]==0 && d[j]<Min)
 49             {
 50                 Min = d[j];
 51                 u = j;
 52             }
 53         }
 54         if(u==-1)    return;
 55         vis[u]=1;
 56         for(int v=1; v<=n+m; v++)
 57             if(vis[v]==0 && grap[u][v]!=INF)
 58                 if(d[u]+grap[u][v] < d[v])
 59                     d[v] = d[u]+grap[u][v];
 60     }
 61 }
 62 
 63 int main()
 64 {
 65 #ifdef  ONLINE_JUDGE
 66 #else
 67     freopen("Test.txt", "r", stdin);
 68 #endif // ONLINE_JUDGE
 69 
 70     fill(grap[0],grap[0]+M*M,INF);
 71 
 72     int k,v1,v2,Ds;
 73     scanf("%d%d%d%d", &n,&m,&k,&Ds);
 74     for(int i=0; i<k; i++)
 75     {
 76         string index;
 77         cin >> index;
 78         v1 = Str(index);
 79         cin >> index;
 80         v2 = Str(index);
 81         scanf("%d", &grap[v1][v2]);
 82         grap[v2][v1]=grap[v1][v2];
 83     }
 84     int maxSum=INF,minDist=0,id=-1;
 85     for(int i=n+1; i<=n+m; i++)
 86     {
 87         Dijskra(i);
 88         int sum=0,dist=INF;
 89         for(int j=1; j<=n; j++)
 90         {
 91             if(d[j] > Ds)
 92             {
 93                 dist=-1;
 94                 break;
 95             }
 96             sum += d[j];
 97             if(d[j] < dist)
 98                 dist=d[j];
 99         }
100         if(dist==-1)
101             continue;
102         if(dist > minDist){
103             minDist = dist;
104             maxSum = sum;
105             id = i;
106         }
107         else if(dist==minDist && sum<maxSum){
108             maxSum=sum;
109             id = i;
110         }
111     }
112     if(id == -1)
113         printf("No Solution");
114     else
115         printf("G%d\n%.1f %.1f", id-n, (1.0)*minDist,(1.0)*maxSum/n);
116 
117     return 0;
118 }

 

转载于:https://www.cnblogs.com/blue-lin/p/11046287.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值