PAT 1072 Gas Station[图论][难]
1072 Gas Station (30)(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 (<= 10^3^), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 10^4^), 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
//看了好几遍,终于理解了题目的意思。并且搜索别的题解。
1.修建加油站,要求加油站能够服务所有的居民屋子,是有距离限制的。
2.要求距离加油站最近的屋子最远,出于安全的考虑。
3.如果这些要求满足,那么输出到所有居民屋子平均距离最短的。
4.同等条件下,输出编号最小的。
代码来自:https://www.liuchuo.net/archives/2376
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int inf = ;
int n, m, k, ds, station;
int e[][], dis[]; bool visit[];
int main() {
fill(e[], e[] + * , inf);
fill(dis, dis + , inf);
scanf("%d%d%d%d", &n, &m, &k, &ds);
for(int i = ; i < k; i++) {
int tempdis;
string s, t;
cin >> s >> t >> tempdis;
int a, b;
if(s[] == 'G') {
s = s.substr();
a = n + stoi(s);
} else {
a = stoi(s);
}
if(t[] == 'G') {
t = t.substr();
b = n + stoi(t);
} else {
b = stoi(t);
}
e[a][b] = e[b][a] = tempdis;
}
int ansid = -;
double ansdis = -, ansaver = inf;
for(int index = n + ; index <= n + m; index++) {
double mindis = inf, aver = ;
fill(dis, dis + , inf);
fill(visit, visit + , false);
dis[index] = ;
for(int i = ; i < n + m; i++) {
int u = -, minn = inf;
for(int j = ; j <= n + m; j++) {
if(visit[j] == false && dis[j] < minn) {
u = j;
minn = dis[j];
}
}
if(u == -) break;
visit[u] = true;
for(int v = ; v <= n + m; v++) {
if(visit[v] == false && dis[v] > dis[u] + e[u][v])
dis[v] = dis[u] + e[u][v];
}
}
for(int i = ; i <= n; i++) {
if(dis[i] > ds) {
mindis = -;
break;
}
if(dis[i] < mindis) mindis = dis[i];
//mindis是加油站到某个居民屋子的最短距离。
aver += 1.0 * dis[i];
}
if(mindis == -) continue;
aver = aver / n;
if(mindis > ansdis) {
ansid = index;
ansdis = mindis;
ansaver = aver;
} else if(mindis == ansdis && aver < ansaver) {
ansid = index;
ansaver = aver;
}
}
if(ansid == -)
printf("No Solution");
else
printf("G%d\n%.1f %.1f", ansid - n, ansdis, ansaver);
return ;
}
//大佬写的可真好,思路清晰,代码简洁!关键是将加油站接着存在了居民屋后面,省空间,而且好遍历,这个我肯定想不到。厉害。
PAT 1072 Gas Station[图论][难]的更多相关文章
- 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 ...
- 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 (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
A gas station has to be built at such a location that the minimum distance between the station and a ...
- PAT Advanced 1072 Gas Station (30) [Dijkstra算法]
题目 A gas station has to be built at such a location that the minimum distance between the station an ...
- 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 ...
随机推荐
- pace.js简介
Pace.js – 超赞的页面加载进度自动指示和 Ajax 导航效果 在页面中引入 Pace.js 和您所选择主题的 CSS 文件,就可以让你的页面拥有漂亮的加载进度和 Ajax 导航效果.不需要挂接 ...
- VIM 如何使用系统的剪切板
想要将系统剪贴板里的内容复制到 vi 编辑的文档中怎么办? 例如,在网页上复制了一段文字,想贴到本地的某个文件中. 使用 vi 打开本地文件,在 输入 模式下,按 Shift + Insert 详细可 ...
- FPM打包工具使用
author:headsen chen date: 2019-01-19 14:57:09 个人原创博客,转自请注明出处和作者,否则追究法律责任 1,安装依赖和语言包 yum -y install ...
- 服务器群秒级别文件同步(ssh+SHELL)
1.介绍 \ 2.业务服务器远程更新浏览服务器文件的脚本 #!/bin/bash operate=$ ip=$ conf_file="/var/www/html/test/ip_list&q ...
- Elasticsearch 索引、更新、删除文档
一.Elasticsearch 索引(新建)一个文档的命令: curl XPUT ' http://localhost:9200/test_es_order_index/test_es_order_t ...
- 彻底关闭window10 专业版 企业版 windows defender
按照上面图中的,关闭windows defender 设置为已启用,这样就可以彻底关闭 windows defender了
- SQL Fundamentals: 数据更新及事务处理(INSERT INTO,UPDATE,DELETE,事务,锁)
SQL Fundamentals || Oracle SQL语言 在SQL语句中,数据操作语言DML由两部分组成,查询(DQL).更新操作(增加,修改,删除). 增加数据(INSERT INTO) 数 ...
- 11.21 CSS学习-上午
font-family:设置文本的字体序列,应当多设置几个,作为后备机制,如果浏览器不支持第一种字体,它将尝试下一种字体.字体序列的名字超过一个字需要使用引号,多个字体序列用逗号分隔指明:{font- ...
- The Unique MST POJ - 1679 最小生成树判重
题意:求一个无向图的最小生成树,如果有多个最优解,输出"Not Unique!" 题解: 考虑kruskal碰到权值相同的边: 假设点3通过边(1,3)连入当前所维护的并查集s. ...
- Spark Streaming 在数据平台日志解析功能的应用
https://mp.weixin.qq.com/s/bGXhC9hvDj4lzK7wYYHGDg 目前,我们使用Filebeat监控日志产生的目录,收集产生的日志,打到logstash集群,接入ka ...