1072 Gas Station 30 ☆☆☆

github 地址:https://github.com/iofu728/PAT-A-by-iofu728 难度:☆☆☆ 关键词:Dijkstra

题目

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 (<= 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

大意

给出 n 个村子,m 个加油站,k 条路,求问哪个加油站里最近村落距离最大,且离最远村落的距离在可控范围内(<Dg).

思路

  1. 很明显是一个求最短距离的问题,归类到Dijkstra上。和一般Dijkstra不同的是,起点需要遍历 m 个。
  2. G[][],点坐标一开始想用 map 进行转换,后来一看编号有规律,不如用 n+i 来表示加油站。
  3. 其他就是裸Dijkstra,一次Dijkstra之后得到d[]数组,从而求出average,mindis,maxdis.
  4. 遍历 m 个加油站得到最短距离最大的加油站,判断满不满足条件,输出.

code

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;
const int INF = 0x3fffffff;
const int maxn = 1020;
int n, m, k, dg, ansid = -1;
double avar, mind, ansdis, ansavar;
int G[maxn][maxn], d[maxn];
bool vis[maxn];

void Dijkstra(int start) {
  fill(d, d + maxn, INF);
  fill(vis, vis + maxn, false);
  d[start] = 0;
  double sum = 0;
  mind = INF;
  for (int i = 1; i <= n + m; ++i) {
    int u = -1, MIN = INF;
    for (int j = 1; j <= n + m; ++j) {
      if (!vis[j] && d[j] < MIN) {
        u = j;
        MIN = d[j];
      }
    }
    if (u == -1)
      break;
    vis[u] = true;
    for (int v = 1; v <= n + m; ++v) {
      if (!vis[v] && G[u][v] != INF && d[u] + G[u][v] < d[v]) {
        d[v] = d[u] + G[u][v];
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    if (d[i] > dg) {
      mind = -1;
      return;
    }
    if (d[i] < mind) {
      mind = d[i];
    }
    if (d[i] != INF) {
      sum += d[i] * 1.0;
    }
  }
  avar = sum / n * 1.0;
  return;
}

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

int main() {
  cin >> n >> m >> k >> dg;
  getchar();
  fill(G[0], G[0] + maxn * maxn, INF);
  for (int i = 0; i < k; ++i) {
    int id1, id2;
    string str1, str2;
    cin >> str1 >> str2;
    id1 = stringnum(str1);
    id2 = stringnum(str2);
    cin >> G[id1][id2];
    G[id2][id1] = G[id1][id2];
    getchar();
  }
  for (int i = n + 1; i <= n + m; ++i) {
    Dijkstra(i);
    if (mind == -1)
      continue;
    if (mind > ansdis) {
      ansid = i;
      ansdis = mind;
      ansavar = avar;
    } else if (mind == ansdis && avar < ansavar) {
      ansid = i;
      ansavar = avar;
    }
  }
  if (ansid == -1) {
    cout << "No Solution\n";
  } else {
    printf("G%d\n%.1f %.1f", ansid - n, ansdis, ansavar);
  }
  return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
You can use this BibTex to reference this blog if you find it useful and want to quote it.