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 ...
随机推荐
- 让我们加密吧Let's encrypt
前言 如今的互联网越来越不安全,我们每个人的信息以及隐私不断被暴露,地下黑产盛行,经常收到垃圾短信,经常被陌生人的电话骚扰.我们的个人信息是怎么泄漏的呢?各种爬虫无时无刻不在互联网爬取着信息,各种嗅探 ...
- EarthWarrior3D游戏ios源代码
这是一款不错的ios源代码源代码,EarthWarrior3D游戏源代码. 而且游戏源码支持多平台. 适用于cocos v2.1.0.0版本号 源代码下载: http://code.662p.com/ ...
- C++组合通信
#include <iostream> #include<vector> #include<string> using namespace std; class A ...
- codeforces round #420 div2
A:暴力枚举 模拟 #include<bits/stdc++.h> using namespace std; ; int n; int a[N][N]; int main() { scan ...
- 此文章介绍vue-cli脚手架config目录下index.js配置文件
此配置文件是用来定义开发环境和生产环境中所需要的参数 关于注释 当涉及到较复杂的解释我将通过标识的方式(如(1))将解释写到单独的注释模块,请自行查看 上代码 // see http://vuejs- ...
- Js:弹窗剧中
js变量设置 var iWidth = $(window).width() * 0.9; var iHeight = $(window).height() * 0.9; - iHeight) / ; ...
- 基于Myeclipse+Axis2的WebService开发实录
最近开始学习了下在Myeclipse开发工具下基于WebSerivce的开发,下面将相关相关关键信息予以记录 Myeclipse的安装,本文以Myeclipse2014-blue为开发环境,相关配置执 ...
- Elasticsearch之更新(全部更新和局部更新)
前面的基础, Elasticsearch之curl创建索引库 Elasticsearch之curl创建索引 Elasticsearch之curl创建索引库和索引时注意事项 Elasticsearch之 ...
- sql 改字段名
sp_rename '[zErpMini].[dbo].[STK_Stock].Isextension','IsExtened'
- Android 侦听应用(Package)变化的方法侦听广播
应用的状态变化,包括安装.卸载.更新,是android系统上重要的事件.如何侦听到?有两种方法,一是通过侦听广播,一是实现PackageMonitor. 侦听广播 当Package状态发生变化时, ...