1072 Gas Station (30point(s)) - C语言 PAT 甲级

1072 Gas Station (30point(s))

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 D​S​​, 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:

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:

G1
2.0 3.3

Sample Input:

2 1 2 10
1 G1 9
2 G1 20

Sample Output:

No Solution

题目大意:

输入 N 个村庄,M 个加油站,K 条路径,DS 一个加油站服务的范围

从 M 个加油站中求一个站点,符合离居民区最远,并且服务区 DS 依旧覆盖居民区,若有多个,则输出距离所有居民区平均距离最小的,若还有多个,则输出编号最小的

设计思路:

Dijkstra 计算最短路径

  • 对 M 个加油站依次计算 Dijkstra 最短路径
  • 判断此加油站的服务范围覆盖,最近村庄最远,平均距离最小,编号最小
  • 输出最终编号
编译器:C (gcc)
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int c2i(char *a, int n)
{
        if (a[0] == 'G')
                return atoi(a + 1) + n;
        return
                atoi(a);
}

int e[1020][1020] = {0};
int dis[1020], visit[1020];

void dijkstra(int start, int n)
{
        int i, j, u, v, min;
        for (j = 0; j <= n; j++) {
                dis[j] = INT_MAX;
                visit[j] = 0;
        }
        dis[start] = 0;
        for (i = 0; i < n; i++) {
                u = -1;
                min = INT_MAX;
                for (j = 1; j <= n; j++) {
                        if (visit[j] == 0 && dis[j] < min) {
                                u = j;
                                min = dis[j];
                        }
                }
                if (u == -1)
                        break;
                visit[u] = 1;
                for (v = 1; v <= n; v++)
                        if (!visit[v] && e[u][v] && dis[u] + e[u][v] < dis[v])
                                dis[v] = dis[u] + e[u][v];
        }
}

int main(void)
{
        int n, m, k, ds, station = -1;
        double mindis = -1.0, avgdis = INT_MAX * 1.0, mindis_now, avgdis_now;
        char s1[5], s2[5];
        int i, j, u, v, flag;

        scanf("%d%d%d%d", &n, &m, &k, &ds);
        for (i = 0; i < k; i++) {
                scanf("%s%s%d", s1, s2, &j);
                u = c2i(s1, n);
                v = c2i(s2, n);
                e[u][v] = j;
                e[v][u] = j;
        }
        for (i = n + 1; i <= n + m; i++) {
                dijkstra(i, n + m);
                mindis_now = INT_MAX * 1.0;
                avgdis_now = 0.0;
                flag = 1;
                for (j = 1; j <= n && flag; j++) {
                        if (dis[j] <= ds) {
                                mindis_now = mindis_now < dis[j] * 1.0 ? mindis_now : dis[j] * 1.0;
                                avgdis_now += dis[j] * 1.0;
                        } else {
                                flag = 0;
                        }
                }
                avgdis_now /= n;
                if (flag && (mindis_now > mindis || (mindis_now == mindis && avgdis_now < avgdis))) {
                        station = i;
                        mindis = mindis_now;
                        avgdis = avgdis_now;
                }
        }
        if (station != -1)
                printf("G%d\n%.1lf %.1lf", station - n, mindis, avgdis);
        else
                printf("No Solution");
        return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章