【PAT甲级 - C++题解】1072 Gas Station

简介: 【PAT甲级 - C++题解】1072 Gas Station

1072 Gas Station

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


题意

加油站的建造位置必须使加油站与距离它最近的房屋的距离尽可能远。


与此同时,它还必须保证所有房屋都在其服务范围内。


现在,给出了城市地图和加油站的几个候选位置,请你提供最佳建议。


如果有多个解决方案,请输出选取位置与所有房屋的平均距离最小的解决方案。


如果这样的解决方案仍然不是唯一的,请输出选取位置编号最小的解决方案。


思路

该题需要满足的条件优先级从上到下:

  1. 所有房屋都在加油站的服务范围内。
  2. 使距离加油站最近的那个房屋的距离尽可能的大。
  3. 使加油站距离所有房屋的总距离尽可能的小。
  4. 加油站的编号从小到大。


由于给定的房屋最大不超过 1000 且加油站数量不超过 10 ,数据量不是很大,故可以枚举每个加油站,对其都进行一次迪杰斯特拉算法求最短路。

然后根据上述要求,找到最符合要求的那个答案即可。注意最后输出答案时,离加油站最近的房屋距离以及总距离都保留 1 位小数。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1020, INF = 0x3f3f3f3f;
int n, m, K, D;
int dist[N];
int g[N][N];
bool st[N];
//将字符串转换成数字
int get(string x)
{
    //将加油站编号转换成n~n+10间的数字
    if (x[0] == 'G')   return stoi(x.substr(1)) + n;
    return stoi(x);
}
//迪杰斯特拉算法
void dijkstra(int start, int& mind, int& sumd)
{
    //初始化
    memset(dist, 0x3f, sizeof dist);
    memset(st, 0, sizeof st);
    dist[start] = 0;
    //执行最短路算法
    for (int i = 0; i < n + m; i++)
    {
        int t = -1;
        for (int j = 1; j <= n + m; j++)
            if (!st[j] && (t == -1 || dist[j] < dist[t]))
                t = j;
        st[t] = true;
        for (int j = 1; j <= n + m; j++)
            dist[j] = min(dist[j], dist[t] + g[t][j]);
    }
    //找到最近房屋并计算到房屋的总距离
    mind = INF, sumd = 0;
    for (int i = 1; i <= n; i++)
    {
        //如果有房屋不在加油站服务返回内,则该加油站不符合要求
        if (dist[i] > D)
        {
            mind = -INF;
            return;
        }
        mind = min(mind, dist[i]);
        sumd += dist[i];
    }
}
int main()
{
    cin >> n >> m >> K >> D;
    //输入每条道路信息
    memset(g, 0x3f, sizeof g);
    for (int i = 0; i < K; i++)
    {
        string a, b;
        int z;
        cin >> a >> b >> z;
        int x = get(a), y = get(b);
        g[x][y] = g[y][x] = min(g[x][y], z);
    }
    //遍历每个加油站,计算最短路
    int res = -1, mind = 0, sumd = INF;
    for (int i = n + 1; i <= n + m; i++)
    {
        int d1, d2;
        dijkstra(i, d1, d2);
        if (d1 > mind) mind = d1, sumd = d2, res = i;
        else if (d1 == mind && d2 < sumd)    sumd = d2, res = i;
    }
    //输出最终答案
    if (res == -1) puts("No Solution");
    else printf("G%d\n%.1f %.1f\n", res - n, (double)mind, (double)sumd / n + 1e-8);
    return 0;
}
目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
66 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
82 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
77 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
76 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
79 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
80 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
137 0
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
58 0
|
存储 人工智能 C++
【PAT甲级 - C++题解】1085 Perfect Sequence
【PAT甲级 - C++题解】1085 Perfect Sequence
72 0
|
C++
【PAT甲级 - C++题解】1046 Shortest Distance
【PAT甲级 - C++题解】1046 Shortest Distance
67 0