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[图论][难]的更多相关文章

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

  2. PAT 1072. Gas Station

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

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

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

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

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

  6. PAT甲级——1072 Gas Station

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

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

  8. 1072. Gas Station (30)

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

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

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

随机推荐

  1. 三、K3 WISE 开发插件《K3 WISE开发手册》

    1.VB插件工程的命名.命名空间和生成的DLL命名要一致,否则导致注册不成功! 2.主控台的查询分析工具,添加sql直接报表,代码用到临时表,提示“在对应所需名称或序数的集合中未找到项目” 解决:在代 ...

  2. 【Eclipse】Ubuntu 下菜单栏失效了,怎么办?(已解决)

    如果你的 Ubuntu 的版本是 13.10 , 且你又安装了 Eclipse , 你就会发现 Eclipse 的菜单不起作用了. 就是点击 File , Edit ... 这些菜单,不会显示子菜单了 ...

  3. jQuery属性操作(二)

    挂载到$上的几个属性操作方法分析,发现属性操作用到了sizzle封装的方法 attr: function( elem, name, value ) {        var hooks, ret,   ...

  4. OGG遇到相关问题汇总

    OGG初始化加载数据时遇到的问题 1.target端拒绝source端访问 2016-12-13 14:31:03 INFO OGG-00963 Oracle GoldenGate Manager f ...

  5. 用CornerStone配置SVN,HTTP及svn简单使用说明

    转载 http://my.oschina.net/joanfen/blog/194491 一.下载地址 CornerStoneV2.6:http://pan.baidu.com/s/1qWEsEbM密 ...

  6. Centos 安装yum,安装ansible

    今天使用centos安装ansible,发现域名默认安装是未注册的.提示: This system is not registered to Red Hat Subscription Manageme ...

  7. Spark2 SQL configuration参数配置

    查看当前环境SQL参数的配置 spark.sql("SET -v") key value spark.sql.hive.version 1.2.1 spark.sql.source ...

  8. [分布式系统学习]阅读笔记 Distributed systems for fun and profit 之一 基本概念

    因为工作的原因,最近打算看一些分布式学习的资料.其中这个http://book.mixu.net/distsys/就是一篇非常适合分布式入门的介绍. 这个短小的材料有下面5个小的章节,图文并茂,也没有 ...

  9. 设置ubuntu默认中文字符

    一. Ubuntu默认的中文字符编码 Ubuntu默认的中文字符编码为zh_CN.UTF-8,这个可以在 /etc/environment中看到:sudo gedit /etc/environment ...

  10. zabbix监控日志文件

    环境: 操作系统:centos 6.8  ,zabbix软件版本:zabbix 3.0.1 前提条件:zabbix客户端已经配置了主动模式,如何配置主动模式,请参考此文 监控日志keys 首先要了解k ...