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. hadoop的缺点

    Hadoop的限制 Hadoop只能执行批量处理,并且只以顺序方式访问数据.这意味着必须搜索整个数据集,即使是最简单的搜索工作.

  2. SQL Server2012中如何通过bak文件还原SQL Server2012数据库

    1 登陆完数据库后,不要新建数据库,直接点击“数据库”然后右击"还原数据库". 2 在"源"选项中选择"设备". 3 选择相应的bak文件并 ...

  3. 如何在MAC上运行exe程序

    1. 首先下载并运行CrossOver 运行CrossOver需要收费,试用期为14天,运行CrossOver 2. 选择exe应用程序,新建容器,安装exe程序 3.安装成功后,运行exe应用程序启 ...

  4. python数学第八天【协方差】

  5. memcach 命令行

    1. cmd上登录memcache # telnet 127.0.0.1 11211   2. 列出所有items stats items     3. 通过itemid获取key 接下来基于列出的i ...

  6. web浏览器兼容问题

    1.居中问题 div中,ie默认居中,而ff(firefox)默认是向左对齐.解决办法:margin 0 auto 2.高度问题 如果有两个div排列或者嵌套,如果第一个div设置了高度,而内容超出d ...

  7. MVP, MVC, MVVM, 傻傻分不清楚~

    1 简介 英文原文:MVC vs. MVP vs. MVVM 三者的目的都是分离关注,使得UI更容易变换(从Winform变为Webform),使得UI更容易进行单元测试. 2 MVC/MVP 2.1 ...

  8. How to enable flash on Chromium

    sudo apt install chromium-browser pepperflashplugin-nonfree

  9. caffe2学习

    https://www.jianshu.com/p/50bf3bd4e3d0 知乎专栏 https://zhuanlan.zhihu.com/kingbob

  10. hdu-3294(最长回文子串)

    题意:给你一个字符和一个字符串让你求出最长回文子串并且输出来,答案需要根据给出的字符转换一下,就是将给出的字符认定为a,然后依次向后推: 解题思路:manacher模板+一些处理 代码: #inclu ...