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

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1072 Gas Station (pintia.cn)
🔑中文翻译:加油站
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值