PAT (Advanced Level) Practice 1072 Gas Station (30 min) the shortest path []

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 ( 1 0 3 ) N (≤10^3) , the total number of houses; M ( 10 ) M (≤10) , the total number of the candidate locations for the gas stations; K ( 1 0 4 ) K (≤10^4) , the number of roads connecting the houses and the gas stations; and D S D_S , the maximum service range of the gas station. It is hence assumed that all the houses are numbered from 1 to N N , and all the candidate locations are numbered from 1 1 to M M .

Then K 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

The meaning of problems

M candidate position selected from a position, this position does not exceed the scope of services of the gas station distance from all the houses, and the shortest distance to all residential maximum. If the answer is not unique, output to the average distance of all residential minimal, if still not unique, then output the smallest number.

Thinking

First find these m distance to each residential location, to determine whether there is more than range of residential services. If not, then calculate the distance to all homes in the shortest possible to update the answer.

Code

#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

const int MAX_V = 1100;
const int INF = -1;

int n, m, k, ds;

struct node {
    int v, weight;

    bool operator>(const node &e) const {
        return weight > e.weight;
    }
};

bool vis[MAX_V];
int dist[MAX_V];
vector<node> graph[MAX_V];

void dijkstra(int s) {
    fill_n(vis, MAX_V, false);
    fill_n(dist, MAX_V, INF);

    dist[s] = 0;

    priority_queue<node, vector<node>, greater<node>> pq;
    pq.push({s, 0});

    while (not pq.empty()) {
        node top = pq.top();
        pq.pop();

        int u = top.v, uDist = top.weight;

        vis[u] = true;

        if (dist[u] < uDist)
            continue;
        for (auto &e : graph[u]) {
            int v = e.v, uvWeight = e.weight;

            if (not vis[v] and (dist[v] == INF or dist[v] > dist[u] + uvWeight)) {
                dist[v] = dist[u] + uvWeight;
                pq.push({v, dist[v]});
            }
        }
    }
}

int getId(string &s) {
    if (s[0] == 'G')
        return stoi(s.substr(1)) + n;
    else
        return stoi(s);
}

int main() {
    scanf("%d%d%d%d", &n, &m, &k, &ds);

    for (int i = 0; i < k; ++i) {
        int d, p1, p2;
        string sp1, sp2;
        cin >> sp1 >> sp2 >> d;

        p1 = getId(sp1);
        p2 = getId(sp2);

        graph[p1].push_back({p2, d});
        graph[p2].push_back({p1, d});
    }

    int minSum = INF, minDist = 0, gasId = INF;

    for (int i = 1; i <= m; ++i) {
        dijkstra(n + i); // 计算这个加油站到所有住宅的最短路径

        bool ok = true;

        for (int j = 1; j <= n; ++j) { // 判断能否覆盖所有住宅
            if (dist[j] > ds) {
                ok = false;
                break;
            }
        }

        if (ok) {
            int sum = 0;
            int minD = INF;

            for (int j = 1; j <= n; ++j) { // 计算到所有住宅的距离之和和其中的最短距离
                sum += dist[j];
                if (minD == INF or minD > dist[j])
                    minD = dist[j];
            }

            if (minDist < minD or (minDist == minD and minSum > sum)) { // 更新最短距离最大的加油站位置
                minSum = sum;
                gasId = n + i;
                minDist = minD;
            }
        }
    }

    if (gasId != INF)
        printf("G%d\n%.1f %.1f", gasId - n, double(minDist), double(minSum) / n);
    else
        printf("No Solution");
}
发布了184 篇原创文章 · 获赞 19 · 访问量 2万+

Guess you like

Origin blog.csdn.net/Exupery_/article/details/104225947