PAT_A1072#Gas Station
Source:
Description:
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 (≤), the total number of houses; M (≤), the total number of the candidate locations for the gas stations; K (≤), 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
G
1 toG
M.Then K lines follow, each describes a road in the format
P1 P2 Dist
where
P1
andP2
are the two ends of a road which can be either house numbers or gas station numbers, andDist
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
Keys:
Attention:
- 加油站可以作为中间结点
Code:
/*
Data: 2019-06-18 17:04:24
Problem: PAT_A1072#Gas Station
AC: 43:23 题目大意:
加油站选取的最佳位置,在服务范围内,离居民区的最短距离尽可能的远
如果最佳位置不唯一,挑选距离居民区平均距离最近,且编号最小的位置
输入:
第一行给出,房子数N,候选加油站数M,总路径数K,最大服务范围Ds
接下来K行,v1,v2,dist
输出:
最佳位置编号
最小距离,平均距离(一位小数)
没有则No Solution
*/
#include<cstdio>
#include<string>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e3+,INF=1e9;
int grap[M][M],vis[M],d[M];
int n,m; int Str(string s)
{
if(s[] == 'G')
{
s = s.substr();
return n+atoi(s.c_str());
}
else
return atoi(s.c_str());
} void Dijskra(int s)
{
fill(d,d+M,INF);
fill(vis,vis+M,);
d[s]=;
for(int i=; i<=n+m; i++)
{
int u=-, Min=INF;
for(int j=; j<=n+m; j++)
{
if(vis[j]== && d[j]<Min)
{
Min = d[j];
u = j;
}
}
if(u==-) return;
vis[u]=;
for(int v=; v<=n+m; v++)
if(vis[v]== && grap[u][v]!=INF)
if(d[u]+grap[u][v] < d[v])
d[v] = d[u]+grap[u][v];
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE fill(grap[],grap[]+M*M,INF); int k,v1,v2,Ds;
scanf("%d%d%d%d", &n,&m,&k,&Ds);
for(int i=; i<k; i++)
{
string index;
cin >> index;
v1 = Str(index);
cin >> index;
v2 = Str(index);
scanf("%d", &grap[v1][v2]);
grap[v2][v1]=grap[v1][v2];
}
int maxSum=INF,minDist=,id=-;
for(int i=n+; i<=n+m; i++)
{
Dijskra(i);
int sum=,dist=INF;
for(int j=; j<=n; j++)
{
if(d[j] > Ds)
{
dist=-;
break;
}
sum += d[j];
if(d[j] < dist)
dist=d[j];
}
if(dist==-)
continue;
if(dist > minDist){
minDist = dist;
maxSum = sum;
id = i;
}
else if(dist==minDist && sum<maxSum){
maxSum=sum;
id = i;
}
}
if(id == -)
printf("No Solution");
else
printf("G%d\n%.1f %.1f", id-n, (1.0)*minDist,(1.0)*maxSum/n); return ;
}
PAT_A1072#Gas Station的更多相关文章
- [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 ...
- Gas Station
Description: There are N gas stations along a circular route, where the amount of gas at station i i ...
- Gas Station [LeetCode]
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
随机推荐
- Mac下查看文件编码方式
一句话:file -I {filename}
- 王立平--Failed to pull selection
解决的方法:重新启动eclipse
- zoj2676 Network Wars(0-1分数规划,最大流模板)
Network Wars 07年胡伯涛的论文上的题:http://wenku.baidu.com/view/87ecda38376baf1ffc4fad25.html 代码: #include < ...
- 2014百度之星第二题Disk Schedule(双调欧几里得旅行商问题+DP)
Disk Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- 抽象类(Abstract)和接口的不同点、共同点(Interface)。
同样点: (1) 都能够被继承 (2) 都不能被实例化 (3) 都能够包括方法声明 (4) 派生类必须实现未实现的方法 区 别: (1) 抽象基类能够定义字段.属性.方法实现.接口仅仅能定义属性.索引 ...
- HDU 2110-Crisis of HDU(母函数)
Crisis of HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- oc40--类的启动过程
// // main.m // 类的启动过程 #import <Foundation/Foundation.h> #import "Person.h" #import ...
- Google Deepmind AI tries it hand at creating Hearthstone and Magic: The Gathering cards
http://www.techrepublic.com/article/google-deepmind-ai-tries-it-hand-at-creating-hearthstone-magic-t ...
- 【POJ 3076】 Sudoku
[题目链接] http://poj.org/problem?id=3076 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 [代码] #include <algorit ...
- 【POJ 1964】 City Game
[题目链接] http://poj.org/problem?id=1964 [算法] 记f[i]表示第i行最多向上延伸的行数 然后,对于每一行,我们用单调栈计算出这一行向上延伸的最大矩形面积,取最大值 ...