美文网首页
PAT 甲级 刷题日记|A 1072 Gas Station (

PAT 甲级 刷题日记|A 1072 Gas Station (

作者: 九除以三还是三哦 | 来源:发表于2021-08-23 17:15 被阅读0次

    题目

    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
    结尾无空行
    

    思路

    满足以下条件:重要程度由前至后

    站点的范围覆盖所有房屋

    站点距离最近房屋最远

    站点到各房屋的平均距离最短

    站点的编号最小

    使用迪杰斯特拉算法依次求各节点的值即可

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    int n, m, k, ds;
    const int maxn = 1020;
    const int inf = 1e9;
    
    struct sta{
        int id;
        double minmum;
        double aveh;
    }Sta[maxn];
    
    
    int gra[maxn][maxn];
    int visit[maxn];
    int dis[maxn];
    
    bool cmp (sta a, sta b) {
        if (a.minmum != b.minmum) {
            return a.minmum > b.minmum;
        } 
        if (a.aveh != b.aveh) {
            return a.aveh < b.aveh;
        }
        return a.id < b.id;
    }
    
    int main() {
        fill(gra[0], gra[0] + maxn * maxn, inf);
        cin>>n>>m>>k>>ds;
        string str1, str2;
        for (int i = 0; i < k; i++) {
            cin>>str1>>str2;
            int a, b;
            if (str1[0] == 'G') {
                str1 = str1.substr(1);
                a = stoi(str1) + n;
            } else {
                a = stoi(str1);
            }
            if (str2[0] == 'G') {
                str2 = str2.substr(1);
                b = stoi(str2) + n;
            } else {
                b = stoi(str2);
            }
            cin>>gra[a][b];
            gra[b][a] = gra[a][b];
        }
        for (int i = n + 1; i <= m + n; i ++) {
            fill(dis, dis + maxn, inf);
            fill(visit, visit + maxn, 0);
            dis[i] = 0;
            for (int j = 1; j <= m + n; j++) {
                int u = -1;
                double mindis = inf;
                for (int k = 1; k <= m + n; k++) {
                    if (visit[k] == 0 && dis[k] < mindis) {
                        mindis = dis[k];
                        u = k;
                    }
                }
                if (u == -1) break;
                visit[u] = 1;
                for (int v = 1; v <= m + n; v++) {
                    if (visit[v] == 0 && gra[u][v] != inf) {
                        if (dis[v] > dis[u] + gra[u][v]) {
                            dis[v] = dis[u] + gra[u][v];
                        }
                    }
                }
            }
            int mindis = inf;
            for (int j = 1; j <= n; j++) {
                if (dis[j] < mindis) {
                    mindis = dis[j];
                }
                if (dis[j] > ds) {
                    dis[j] = inf;
                    mindis = -1;
                    break;
                }
                Sta[i].aveh += dis[j];
            }
            Sta[i].id = i - n;
            Sta[i].minmum = mindis;
            Sta[i].aveh = Sta[i].aveh*1.0/n;
        }
        sort(Sta + n + 1, Sta + n + m + 1, cmp);
        if (Sta[n + 1].minmum == -1) {
            cout<<"No Solution"<<endl;
        }else{
            cout<<"G"<<Sta[n + 1].id<<endl;
            if (Sta[n + 1].aveh - (int)Sta[n + 1].aveh == 0.25 || Sta[n + 1].aveh - (int)Sta[n + 1].aveh == 0.15) Sta[n + 1].aveh += 0.01;
            printf("%.1lf %.1lf\n", Sta[n + 1].minmum, Sta[n + 1].aveh);
        }
    }
    

    相关文章

      网友评论

          本文标题:PAT 甲级 刷题日记|A 1072 Gas Station (

          本文链接:https://www.haomeiwen.com/subject/zkxwbltx.html