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

题意:

  给出m个候选的加油站建造地点,要求从中选取一个,使其距离住宅区的距离尽可能的远。如果存在相等的情况,则输出距离平均值最小的那个。

思路:

  对每一个加油站运用Dijkstra算法求出该加油站到达其他结点的最小距离。然后在最小距离中寻找最大值。

Code:

 1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 const int inf = 999999999;
6 int grap[1020][1020];
7 int visited[1020], dist[1020];
8
9 int main() {
10 int n, m, k, ds;
11 cin >> n >> m >> k >> ds;
12 fill(grap[0], grap[0] + 1020 * 1020, inf);
13 for (int i = 0; i < 1020; ++i) grap[i][i] = 0;
14 for (int i = 0; i < k; ++i) {
15 string p1, p2;
16 int d;
17 cin >> p1 >> p2 >> d;
18 int v1, v2;
19 if (p1[0] == 'G') {
20 v1 = stoi(p1.substr(1)) + n;
21 } else {
22 v1 = stoi(p1);
23 }
24 if (p2[0] == 'G') {
25 v2 = stoi(p2.substr(1)) + n;
26 } else {
27 v2 = stoi(p2);
28 }
29 grap[v1][v2] = grap[v2][v1] = d;
30 grap[v1][v2] = grap[v2][v1] = min(d, grap[v1][v2]);
31 }
32 int ansid = -1;
33 double ansdist = -1, ansaver = inf;
34 for (int i = n + 1; i <= n + m; ++i) {
35 double aver = 0, mindist = inf;
36 fill(visited, visited + 1020, 0);
37 fill(dist, dist + 1020, inf);
38 dist[i] = 0;
39 for (int j = 0; j < n + m; ++j) {
40 int u = -1, minn = inf;
41 for (int k = 1; k <= n + m; ++k) {
42 if (visited[k] == 0 && dist[k] < minn) {
43 u = k;
44 minn = dist[k];
45 }
46 }
47 if (u == -1) break;
48 visited[u] = 1;
49 for (int k = 1; k <= n + m; ++k) {
50 if (visited[k] == 0 && dist[k] > dist[u] + grap[u][k])
51 dist[k] = dist[u] + grap[u][k];
52 }
53 }
54 for (int j = 1; j <= n; ++j) {
55 if (dist[j] > ds) {
56 mindist = -1;
57 break;
58 }
59 if (dist[j] < mindist) mindist = dist[j];
60 aver += 1.0 * dist[j];
61 }
62 if (mindist == -1) continue;
63 aver = aver / n;
64 if (mindist > ansdist) {
65 ansdist = mindist;
66 ansaver = aver;
67 ansid = i;
68 } else if (mindist == ansdist && aver < ansaver) {
69 ansaver = aver;
70 ansid = i;
71 }
72 }
73
74 if (ansid == -1)
75 printf("No Solution\n");
76 else
77 printf("G%d\n%.1f %.1f\n", ansid - n, ansdist, ansaver);
78
79 return 0;
80 }

参考:

  https://www.liuchuo.net/archives/2376

1072 Gas Station的更多相关文章

  1. PAT 1072 Gas Station[图论][难]

    1072 Gas Station (30)(30 分) A gas station has to be built at such a location that the minimum distan ...

  2. pat 甲级 1072. Gas Station (30)

    1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...

  3. 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise

    题目信息 1072. Gas Station (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A gas station has to be built at s ...

  4. PAT 甲级 1072 Gas Station (30 分)(dijstra)

    1072 Gas Station (30 分)   A gas station has to be built at such a location that the minimum distance ...

  5. PAT 1072. Gas Station (30)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  6. 1072. Gas Station (30)

    先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...

  7. 1072. Gas Station (30) 多源最短路

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  8. 1072 Gas Station (30)(30 分)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  9. PAT 1072. Gas Station

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  10. PAT甲级——1072 Gas Station

    A gas station has to be built at such a location that the minimum distance between the station and a ...

随机推荐

  1. 卧槽,好强大的魔法,竟能让Python支持方法重载

    1. 你真的了解方法重载吗? 方法重载是面向对象中一个非常重要的概念,在类中包含了成员方法和构造方法.如果类中存在多个同名,且参数(个数和类型)不同的成员方法或构造方法,那么这些成员方法或构造方法就被 ...

  2. Django的ORM封装接口详细

    [前言]Django的orm操作本质上会根据对接的数据库引擎,翻译成对应的sql语句:所有使用Django开发的项目无需关心程序底层使用的是MySQL.Oracle.sqlite....,如果数据库迁 ...

  3. 检查字符串是否包含另一串字符串(c++)

    在c++中检查字符串是否包含另一串字符串,这个本来是我做过的一个算法题,不过最近刚好有个需求让我想到了这个题,就在此记录一下! 使用std::string::findfunction string s ...

  4. 146. LRU 缓存机制 + 哈希表 + 自定义双向链表

    146. LRU 缓存机制 LeetCode-146 题目描述 题解分析 java代码 package com.walegarrett.interview; /** * @Author WaleGar ...

  5. POJ-1502(基本dijikstra算法)

    MPI Maelstrom POJ-1502 这题是求最短路,但是因为一开始看错题目,导致我去使用prime算法求最小生成树 题意是指一台机器发出信息后,还可以向其他的机器发送信息,所以不能使用pri ...

  6. C语言之三字棋的简单实现及扩展

    C语言之三字棋的简单实现及扩展 在我们学习完数组之后,我们完全可以利用数组相关知识来写一个微小型的游戏,比如说今天所说的--三子棋. 大纲: 文件组成 实现   完整代码展示   扩展 即: 一.文件 ...

  7. Java 在PPT中添加文本水印的简易方法(单一/平铺水印)

    [前言] 在PPT幻灯片中,可通过添加形状的方式,来实现类似水印的效果,可添加单一文本水印效果,即在幻灯片中心位置水印以单个文本字样显示,但通过一定方法也可以添加多行(平铺)文本水印效果,即在幻灯片中 ...

  8. yolo训练数据集

    最近了解了下yolov3的训练数据集部分,总结了以下操作步骤:(基于pytorch框架,请预先装好pytorch的相关组件) 1.下载ImageLabel软件对图片进行兴趣区域标记,每张图片对应一个x ...

  9. MySQL入门(5)——运算符

    MySQL入门(5)--运算符 算术运算符 MySQL支持的算数运算符包括加.减.乘.除.求余. 符号 作用 + 加法运算 - 减法运算 * 乘法运算 / 除法运算 % 求余运算 DIV 除法运算,返 ...

  10. win8 下删除服务

    1.右键 我的电脑-管理-服务和应用程序-服务,找到想要删除的服务,双击后复制服务名称. 2.管理员身份运行cmd 在命令框中输入 sc delete "secbizsrv" 就删 ...