#1072. Gas Station【最短路 + Dijkstra】

原题链接

Problem Description:

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

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

Problem Analysis:

题目给定了几个候选的加油站的位置,要求我们从中选择一个加油站的位置,满足如下要求:

  • 所有房屋都在该加油站的服务范围内
  • 距离加油站最近的房屋距离尽可能的远
  • 所有房屋到达加油站的距离尽可能的小

我们可以以每个加油站作为最短路的起点,依次做 Dijkstra,然后不断更新最短距离和总距离即可。

Code

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1020, INF = 0x3f3f3f3f;

int n, m, k, D;
int g[N][N];
int dist[N];
bool st[N];

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

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[t] > dist[j]))
                t = j;
        st[t] = true;
        for (int j = 1; j <= n + m; j ++ )
            dist[j] = min(dist[j], dist[t] + g[t][j]);
    }

    for (int i = 1; i <= n; i ++ )
        if (dist[i] > D)
        {
    
    
            mind = -INF;
            return;
        }

    mind = INF, sumd = 0;
    for (int i = 1; i <= n; i ++ )
    {
    
    
        mind = min(mind, dist[i]);
        sumd += dist[i];
    }
}

int main()
{
    
    
    cin >> n >> m >> k >> D;
    memset(g, 0x3f, sizeof g);

    while (k -- )
    {
    
    
        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; // minbd是距离加油站最近的房屋, sumd是所有房屋到达加油站的距离之和
    for (int i = n + 1; i <= n + m; i ++ )
    {
    
    
        int d1, d2; // mind, sumd
        dijkstra(i, d1, d2); 

        if (d1 > mind) res = i, mind = d1, sumd = d2;
        else if (d1 == mind && d2 < sumd) res = i, sumd = d2;
    }

    if (res == -1) puts("No Solution");
    else printf("G%d\n%.1lf %.1lf\n", res - n, (double)mind, (double)sumd / n + 1e-8);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/geraltofrivia123/article/details/121108153