1072 Gas Station (30 分)(最短路径Dj)

题目描述:

1072 Gas Station (30 分)
 

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 (≤), the total number of houses; M (≤), the total number of the candidate locations for the gas stations; K (≤), 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

思路:把可以做加油站的地方与村庄都当作图中的点,具体就是Gi的位置为n+i,然后对每一个可以按加油站的位置求单源最短路,我用的Dj,然后判断取距离最小,如果存在到村庄的最短路超过限制,该位置就不能选
AC代码:
#include<iostream>
#include<string.h>
#include<cmath>
#include<set>
#include<map>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
#include<algorithm>
#include<climits>
using namespace std;
typedef long long ll;
inline int read() {
    int sum = 0, f = 1;
    char p = getchar();
    for (; !isdigit(p); p = getchar()) if (p == '-')f = -1;
    for (; isdigit(p); p = getchar())  sum = sum * 10 + p - 48;
    return sum * f;
}
const int maxn = 1011;
const int maxm = 20015;
struct edge {
    int t, nxt,w;
}e[maxm];
int hd[maxn], tot;
void add(int f, int t,int w) {
    e[++tot].t = t;
    e[tot].w = w;
    e[tot].nxt = hd[f];
    hd[f] = tot;
}
int n, m, k, md;
int getpos() {
    int sum = 0, f = 0;
    char p = getchar();
    for (; !isdigit(p); p = getchar()) { if (p == 'G')f = 1; }
    for (; isdigit(p); p = getchar())sum = sum * 10 + p - 48;
    if (f)sum += n;
    return sum;
}
struct node {
    int d, id;
    bool operator<(node b)const {
        return d > b.d;
    }
};
int dis[maxn];
bool vis[maxn];
bool f;
ll sum;
int midis;
void Dj(int st) {
    memset(dis, 0x3f3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    dis[st] = 0;
    priority_queue<node>Q;
    Q.push({ 0,st });
    while (!Q.empty()) {
        node t = Q.top(); Q.pop();
        if (vis[t.id])continue;
        vis[t.id] = 1;
        if (t.id <= n) {
            if (dis[t.id] > md) {
                f = 0; return;
            }
            else {
                midis = min(midis, dis[t.id]);
                sum += dis[t.id];
            }
        }
        for (int i = hd[t.id]; i; i = e[i].nxt) {
            int v = e[i].t,w=e[i].w;
            if (!vis[v]&&dis[t.id] + w < dis[v]) {
                dis[v] = dis[t.id] + w;
                Q.push({ dis[v],v });
            }
        }
    }
}
int main() {
    //freopen("test.txt", "r", stdin);
    n = read(), m = read(), k = read(), md = read();
    for (int i = 1; i <= k; i++) {
        int a = getpos(), b = getpos(), d = read();
        add(a, b, d);
        add(b, a, d);
    }
    ll res, ans, resdis=-1;
    for (int i = n + 1; i <= n + m; i++) {
        f = 1; sum = 0; midis = INT_MAX;
        Dj(i);
        if (f) {
            if (midis > resdis) { res = sum; ans = i; resdis = midis; }
            else if (midis == resdis && res > sum) {
                res = sum, ans = i;
            }
        }
    }
    if (resdis == -1) {
        printf("No Solution"); return 0;
    }
    printf("G%d\n", ans - n);
    printf("%.1lf %.1lf\n", double(resdis), double(res) / n);
    return 0;
}

 

 
posted @ 2021-03-08 13:18  cono奇犽哒  阅读(74)  评论(0)    收藏  举报