A1072. 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 (<= 103), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 104), 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
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
const int INF = ;
int G[][], visit[], dst[];
int station = -, maxLen = -, sum = INF;
double average = ;
int N, M, K, Ds;
int str2num(char str[]){
int num = , P = ;
if(str[] == 'G'){
for(int i = strlen(str) - ; i > ; i--){
num += (str[i] - '') * P;
P *= ;
} return num + N;
}else{
for(int i = strlen(str) - ; i >= ; i--){
num += (str[i] - '') * P;
P *= ;
}
return num;
}
}
void dijkstra(int s){
fill(visit, visit + , );
fill(dst, dst + , INF);
dst[s] = ;
for(int i = ; i <= N + M; i++){
int u = -, minLen = INF;
for(int j = ; j <= N + M; j++){
if(visit[j] == && dst[j] < minLen){
minLen = dst[j];
u = j;
}
}
if(u == -)
return;
visit[u] = ;
for(int j = ; j <= N + M; j++){
if(visit[j] == && G[u][j] != INF){
if(G[u][j] + dst[u] < dst[j]){
dst[j] = G[u][j] + dst[u];
}
}
}
}
}
int main(){
scanf("%d%d%d%d", &N, &M, &K, &Ds);
char str1[], str2[];
int temp;
fill(G[], G[] + *, INF);
for(int i = ; i < K; i++){
scanf("%s %s %d", str1, str2, &temp);
int p1 = str2num(str1);
int p2 = str2num(str2);
G[p1][p2] = G[p2][p1] = temp;
}
for(int i = N + ; i <= N + M; i++){
dijkstra(i);
int minFind = INF, sumTemp = ;
for(int j = ; j <= N; j++){
sumTemp += dst[j];
if(dst[j] == INF || dst[j] > Ds){
minFind = INF;
break;
}
if(dst[j] < minFind){
minFind = dst[j];
}
}
if(minFind != INF){
if(minFind > maxLen){
maxLen = minFind;
sum = sumTemp;
station = i;
}else if(minFind == maxLen && sumTemp < sum){
maxLen = minFind;
sum = sumTemp;
station = i;
}
}
}
if(station == -)
printf("No Solution\n");
else{
double betw = maxLen * 1.0;
average = (double)sum / (double)N;
printf("G%d\n%.1f %.1f", station - N, betw, average);
}
cin >> N;
return ;
}
总结:
1、题意:有M个待选位置,选其中一个建造加油站。选择原则是该加油站应该使得距离它最近的房屋的距离最大。如果有多个待选方案,则选择使油站到所有房屋的平均距离最小。 即先对每一个待选位置做一次dijkstra,求出它到每一个房屋的最短路,在这之中选择最小的记为MINi,该位置到所有房屋的平均距离记为AVGi。 对所有的带选位置对应的MINi,选择一个最大的。如果有多个最大值,则选择到最小的AVGi的那个。
2、由于M个待选点虽然不是房屋且不计入最短路内,但它作为一个节点也有联通其它房屋的作用。所以在dijkstra时需要把它们算在内。但在找MINi与AVGi时,它们需要被排除。
3、计算均值且比较大小有误差,可以先用sum和代替,在最后输出时再算均值。
4、由于题目输入的数据有纯数字,也有G和数字。所以统一按照字符串读入再转为数字。对于普通房屋,编号1到N,加油站编号从N+1开始,即G1、G2的编号为N+1, N+2。
5、对于这类构造数据很麻烦的题,在调试时尽可能想最简单的测试数据以及边界数据。比如只有一个house和一个待选点。
A1072. Gas Station的更多相关文章
- [pat]A1072 Gas Station
这道题的结点编号是字符串类型,处理的过程很有意思,用getID将house和GasStation进行区分 #include<bits/stdc++.h> using namespace s ...
- PAT_A1072#Gas Station
Source: PAT A1072 Gas Station (30 分) Description: A gas station has to be built at such a location t ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 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 ...
- Leetcode 134 Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 【leetcode】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- [LeetCode] Gas Station
Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...
- 20. Candy && Gas Station
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- LeetCode——Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
随机推荐
- centOS7防火墙关闭失败问题
CentOS7命令: 查看防火墙状态:firewall-cmd --state 关闭防火墙:systemctl stop firewalld.service 禁止开机自启:systemctl disa ...
- png8、16、24、32位的区别
我们都知道一张图片可以保存为很多种不同的格式,比如bmp/png/jpeg/gif等等.这个是从文件格式的角度看,我们抛开文件格式,看图片本身,我们可以分为8位, 16位, 24位, 32位等. 单击 ...
- MCV 和 MTV框架基本信息
一 . MCV # web服务器开发最著名的MVC模式 M : model.py 就是和数据库打交道的, 创建表等操作 V : view 视图(视图函数,就是装HTML文件的) C : control ...
- 结巴(jieba)分词
一.介绍: jieba: “结巴”中文分词:做最好的 Python 中文分词组件 “Jieba” (Chinese for “to stutter”) Chinese text segmentatio ...
- java.util.concurrent.TimeoutException: Idle timeout expired: 300000/300000 ms
Request idle timed out at 123000 ms. That means there was no activity (read or write) for 123000 ms ...
- Java多线程4:Thread中的静态方法
一.Thread类中的静态方法 Thread类中的静态方法是通过Thread.方法名来调用的,那么问题来了,这个Thread指的是哪个Thread,是所在位置对应的那个Thread嘛?通过下面的例子可 ...
- 去掉dede织梦position当前位置最后一个箭头的方法
理论是,dede的当前位置标签{dedefield name='position'}结构是 首页 > 主栏目 > 子栏目 > ,这就说明,而箭头符号字段数据都是在后台设置后存储在数据 ...
- Python自动化测试之selenium从入门到精通
1. 安装selenium 首先确保python安装成功,输入python -V 在windows下使用pip安装selenium,详情如图所示: 在ubuntu下使用pip install sele ...
- django celery redis 定时任务
0.目的 在开发项目中,经常有一些操作时间比较长(生产环境中超过了nginx的timeout时间),或者是间隔一段时间就要执行的任务. 在这种情况下,使用celery就是一个很好的选择. cele ...
- tornado web框架简介
https://www.cnblogs.com/aylin/p/5702994.html