1072 Gas Station
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
题意:
给出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的更多相关文章
- PAT 1072 Gas Station[图论][难]
1072 Gas Station (30)(30 分) A gas station has to be built at such a location that the minimum distan ...
- pat 甲级 1072. Gas Station (30)
1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...
- 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 ...
- 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 ...
- 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 ...
- 1072. Gas Station (30)
先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...
- 1072. Gas Station (30) 多源最短路
A gas station has to be built at such a location that the minimum distance between the station and a ...
- 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 ...
- PAT 1072. Gas Station
A gas station has to be built at such a location that the minimum distance between the station and a ...
- PAT甲级——1072 Gas Station
A gas station has to be built at such a location that the minimum distance between the station and a ...
随机推荐
- wxWidgets源码分析(6) - 窗口关闭过程
目录 窗口关闭过程 调用流程 关闭文档 删除视图 删除文档对象 关闭Frame App清理 多文档窗口的关闭 多文档父窗口关闭 多文档子窗口关闭 窗口的正式删除 窗口关闭过程总结 如何手工删除view ...
- 关于 JMeter 5.4.1 的一点记录
APACHE JMeter table { border: 0; border-collapse: collapse; background-color: rgba(255, 245, 218, 1) ...
- 剑指 Offer 17. 打印从1到最大的n位数
剑指 Offer 17. 打印从1到最大的n位数 Offer 17 题目解析: 暴力解法 package com.walegarrett.offer; /** * @Author WaleGarret ...
- javascript处理HTML的Encode(转码)和解码(Decode)
HTML的Encode(转码)和解码(Decode)在平时的开发中也是经常要处理的,在这里总结了使用javascript处理HTML的Encode(转码)和解码(Decode)的常用方式 一.用浏览器 ...
- Java 基础加强 02
基础加强·反射 和 枚举 类的加载概述和加载时机 * A:类的加载概述 * 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载.连接.初始化来实现对这个类的初始化 * 加载 * 就是指 ...
- 【知识点】 C++寄存器优化
作者:李春港 出处:https://www.cnblogs.com/lcgbk/p/14502076.html 目录 一.前言 二.代码实例 三.volatile作用 一.前言 在c++中什么情况下, ...
- 对于如何从SM2的pfx证书文件中解析公钥和私钥,并从二次加密的密文中解密
首先呢,由于我的域名之前处理点问题,然后备案第二个网站时候,第一个网站没法访问,所以备案没过,阿里云告诉我要删除一个网站的备案,但是他没告诉我要删除主体,所以我的备案主体成了空壳主体,要传真或者发快递 ...
- MyBatis(九):MyBatis类型处理器(TypeHandler)详解
TypeHandler简介 TypeHandler,顾名思义类型转换器,就是将数据库中的类型与Java中的类型进行相互转换的处理器. MyBatis 在设置预处理语句(PreparedStatemen ...
- YoloV3 记录
常用于目标检测,因为最近要从目标分类问题转到目标检测中去. tensoflow.Keras(大公司一般都用这个).pytorch(本次学习)------------------主要框架 程序设计模块规 ...
- java例题_18 乒乓球比赛(对手问题)
1 /*18 [程序 18 乒乓球赛] 2 题目:两个乒乓球队进行比赛,各出三人.甲队为 a,b,c 三人,乙队为 x,y,z 三人.已抽签决定比赛名单. 3 有人向队员打听比赛的名单.a说他不和 x ...