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. USACO The Clocks

    操作间没有次序关系,同一个操作最多重复3次... 可以直接暴力... The Clocks IOI'94 - Day 2 Consider nine clocks arranged in a 3x3 ...

  2. 设计模式初探-桥接(Bridge)模式

    桥接(Bridge)模式,又称Handle/Body模式,属于对象结构型模式.用于将抽象部分与它的实现部分分离,使它们都可以独立地变化.比如常见的电脑窗口界面,不同的操作系统其窗口界面绘制的原理肯定不 ...

  3. vue案例 - v-model实现自定义样式の多选与单选

    接,上文:https://www.cnblogs.com/padding1015/p/9265985.html 这两天在玩mpvue,但是下午如果对着文档大眼瞪小眼的话,肯定会睡着的. 想起昨晚的fl ...

  4. Android Studio 3.1.2 版本包下载

    Android Studio 3.1.2 bug 修复版已发布,本次更新修复了一些错误,并改进了某些场景下 lint 审查的速度.详细的修复内容请查看 Android Studio 3.1.2 的发布 ...

  5. python---使用md5加密

    python中使用md5进行加密字符串: __author__ = 'Administrator' #-*- coding: utf-8 -*- import hashlib aa = ' #需要加密 ...

  6. Python 核心编程

    第3章 Python 基础 1.语句和语法: 注释(#): 继续换句话说跨行(\):有两种例外情况一个语句不使用反斜线也可以跨行.在使用闭合操作符时,单一语句可以跨多行,如小括号.中括号,花括号等,另 ...

  7. 美团店铺评价语言处理以及文本分类(logistic regression)

    美团店铺评价语言处理以及分类(LogisticRegression) 第一篇 数据清洗与分析部分 第二篇 可视化部分, 第三篇 朴素贝叶斯文本分类 本文是该系列的第四篇 主要讨论逻辑回归分类算法的参数 ...

  8. mvc 实现超时弹窗后跳转

    为了实现保持登录状态,可以用cookie来解决这一问题 假设过期时间为30分钟,校验发生在服务器,借助过滤器,可以这样写 public class PowerFilter : AuthorizeAtt ...

  9. 统计Java项目的代码行数

    Java项目谈论行数多少有点无聊,但是有的时候就想看看一个开源的代码的量级,用Shell命令统计再合适不过了 去掉空行和注释: find . -name "*.java" |xar ...

  10. GitHub 终端加速最佳实践

    终端加速 GitHub 方法的前置条件, 一是购买了加速服务或者租用 VPS 搭建加速服务, 二是系统是 macOS, 三是终端是 iTerm, 四是 Shell 是 zsh. 终端加速 GitHub ...