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的更多相关文章

  1. [pat]A1072 Gas Station

    这道题的结点编号是字符串类型,处理的过程很有意思,用getID将house和GasStation进行区分 #include<bits/stdc++.h> using namespace s ...

  2. PAT_A1072#Gas Station

    Source: PAT A1072 Gas Station (30 分) Description: A gas station has to be built at such a location t ...

  3. [LeetCode] Gas Station 加油站问题

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

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

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

  6. 【leetcode】Gas Station

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

  7. [LeetCode] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

  8. 20. Candy && Gas Station

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  9. LeetCode——Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

随机推荐

  1. (C/C++)区别:数组与指针,指针与引用

    1.数组跟指针的区别 数组要么在静态存储区被创建(如全局数组),要么在栈上被创建.数组名对应着(而不是指向)一块内存,其地址与容量在生命期内保持不变,只有数组的内容可以改变. 指针可以随时指向任意类型 ...

  2. Kettle中表输出字段和字段选择

    表输出: 字段选择: 注:字段选择可以输出匹配后的选中列,表输出则输出匹配后的所有列.

  3. github上测试服出现bug,如何回滚并获得合并之前的分支

    使用场景: 当我们提交了一个pr,但是该pr合并之后,经过在测试测试有问题,需要回滚.这个时候主master代码将会被回滚到提交你的pr之前的代码.而你的pr由于已经被合并过了,所以无法继续提交. 这 ...

  4. IWMS后台上传文章,嵌入音频文件代码

    <object width="260" height="69" classid="clsid:6bf52a52-394a-11d3-b153-0 ...

  5. Yii的数值比较验证器

    该验证器比对两个特定输入值之间的关系 是否与 operator 属性所指定的相同. compareAttribute:用于与原属性相比对的属性名称. 当该验证器被用于验证某目标属性时, 该属性会默认为 ...

  6. (转载)C#使用MemoryStream类读写内存

    MemoryStream和BufferedStream都派生自基类Stream,因此它们有很多共同的属性和方法,但是每一个类都有自己独特的用法.这两个类都是实现对内存进行数据读写的功能,而不是对持久性 ...

  7. k8s(一) kubeadm简单集群初始化

    写给想入门kubernetes的同学们 # 系统版本 [root@master ~]# cat /etc/os-release NAME="CentOS Linux" VERSIO ...

  8. Asp.Net Core 输出 Word

    In one of the ASP.NET Core projects we did in the last year, we created an OutputFormatter to provid ...

  9. PHP——生成唯一序列号UUID

    <?php function uuid($uid = '') { $chars = md5(uniqid(mt_rand(), true)); $uuid = substr($chars, 0, ...

  10. 洛谷P1395 会议 题解

    $题目$ 为什么这个题会有图论的标签啊,虽然图论也包括找树的重心,可是这很容易让人联想到最短路,但不得不说,这是一个典型的找树的重心模板题. 树的重心是什么? 找到一个点,其所有的子树中最大的子树节点 ...