• 【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
    
    • 1

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    Sample Output 1:

    G1
    2.0 3.3
    
    • 1
    • 2

    Sample Input 2:

    2 1 2 10
    1 G1 9
    2 G1 20
    
    • 1
    • 2
    • 3

    Sample Output 2:

    No Solution
    
    • 1
    题意

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

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

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

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

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

    思路

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

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

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

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

    代码
    #include
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
  • 相关阅读:
    JS中递归函数
    web前端期末大作业 html+css学生心理 7页主题网页设计
    小米/华为怎样找回手机联系人?告别焦虑,2个紧急救援指南
    docker学习记录(二)
    机器人系统 ROS 常用命令行工具
    一张图搞清楚wait、sleep、join、yield四者区别,面试官直接被征服!
    WMS系统出库管理:优化仓储流程
    Shell脚本编程(一) —— 变量定义(用户自定义变量、位置变量、预定义变量、环境变量)
    Apache Solr 身份认证绕过导致任意文件读取漏洞复现(CVE-2024-45216)
    修改Qt源码支持DPI粒度到QWidget
  • 原文地址:https://blog.csdn.net/Newin2020/article/details/127601589