pat 甲级 1072. Gas Station (30)
1072. Gas Station (30)
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 题意:最短路,找出一个建立加油站的合适地方。现在有几个备选的地方。按照如下规则筛选:
1:加油站与所有住宅区的的最小距离越大越好。
2:加油站与所有住宅区的平均距离越小越好。
3:挑选编号数值最小的加油站。
AC 代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 1000+20
typedef long long ll;
int n,m,k,d_max;
struct edge {
int to, cost;
edge() {}
edge(int to ,int cost):to(to),cost(cost) {}
};
vector<edge>G[N_MAX];
struct P {
int first, second;
P() {}
P(int first,int second):first(first),second(second) {}
bool operator < (const P&b)const {
return first > b.first;
}
};
int d[N_MAX];
int V;
void dijkstra(int s) {
priority_queue<P>que;
fill(d, d + V, INF);
d[s] = ;
que.push(P(,s));
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if (d[v] < p.first)continue;
for (int i = ; i < G[v].size();i++) {
edge e = G[v][i];
if (d[e.to]>d[v]+e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
} int translation(string s) {
if (s[] == 'G') {
if (s.size() == )return m+n;//m最大为10,唯一的三位数
else return s[] - ''+n;
}
else {
return atoi(s.c_str());
}
}
string recover(int id) {
string s="G";
s += '' + id-n;
return s;
} int main() {
while (cin>>n>>m>>k>>d_max) {
V = n + m+;
for (int i = ; i < k;i++) {
string from, to; int cost;
cin >> from >> to >> cost;
G[translation(from)].push_back(edge(translation(to), cost));
G[translation(to)].push_back(edge(translation(from), cost));
}
double max_mindist = -, max_avedist = -; int id;
for (int i = ; i <= m;i++) {//对于每一个station
bool flag = ;//判断当前情况是否可以
int pos = n + i;
dijkstra(pos);
double tmp_ave = ;int tmp_min = INF;
for (int j = ; j <= n; j++) {
if (d[j] > d_max) {
flag = ;
break;
}
tmp_min = min(d[j], tmp_min);
tmp_ave += d[j];
}
if (!flag)continue;
tmp_ave /= (double)n;
if (tmp_min > max_mindist) {
max_mindist=tmp_min;
max_avedist = tmp_ave;
id = pos;
}
else if (tmp_min == max_mindist&&tmp_ave < max_avedist) {
max_avedist=tmp_ave;
id = pos;
}
else if (tmp_min == max_mindist&&tmp_ave == max_avedist&& id>pos) {
id = pos;
}
}
if (max_mindist == -)puts("No Solution");
else {
cout << recover(id) << endl;
printf("%.1f %.1f\n",max_mindist,max_avedist);
}
}
return ;
}
pat 甲级 1072. Gas Station (30)的更多相关文章
- PAT 甲级 1072 Gas Station (30 分)(dijstra)
1072 Gas Station (30 分) A gas station has to be built at such a location that the minimum distance ...
- PAT甲级——1072 Gas Station
A gas station has to be built at such a location that the minimum distance between the station and a ...
- PAT Advanced 1072 Gas Station (30) [Dijkstra算法]
题目 A gas station has to be built at such a location that the minimum distance between the station an ...
- 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise
题目信息 1072. Gas Station (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A gas station has to be built at s ...
- 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 ...
- 1072. Gas Station (30)
先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...
- 1072. Gas Station (30) 多源最短路
A gas station has to be built at such a location that the minimum distance between the station and a ...
- 1072 Gas Station (30)(30 分)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- 【PAT甲级】1072 Gas Station (30 分)(Dijkstra)
题意: 输入四个正整数N,M,K,D(N<=1000,M<=10,K<=10000)分别表示房屋个数,加油站个数,路径条数和加油站最远服务距离,接着输入K行每行包括一条路的两条边和距 ...
随机推荐
- iOS多播Delegate类——GCDMulticastDelegate用法小结
iOS中通常的delegate模式只能有一个被委托的对象,这样当需要有多个被委托的对象时,实现起来就略为麻烦,在开源库XMPPFramework中提供了一个GCDMulticastDelegate类, ...
- React 服务端渲染最佳解决方案
最近在开发一个服务端渲染工具,通过一篇小文大致介绍下服务端渲染,和服务端渲染的方式方法.在此文后面有两中服务端渲染方式的构思,根据你对服务端渲染的利弊权衡,你会选择哪一种服务端渲染方式呢? 什么是服务 ...
- 十、Shell 函数
Shell 函数 linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action ...
- ubuntu crontab设置定时任务
ubuntu 设置定时任务 crontab -l #查看详情crontab -e #设置定时任务 * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用* ...
- 18.VUE学习之-v-for操作对象与数值
一组数组时的循环 二组数组时的循环 另外可以v for 20 可以直接操作数字 <!DOCTYPE html> <html lang="en"> <h ...
- CentOS6.7下的软件安装
一.JDK安装及其环境变量的配置 **创建一个专门安装软件的文件夹:mkdir /root/apps **解压安装包:tar -zxvf jdk-7u45-linux-x64.tar.gz -C /r ...
- selenium中webdriver跳转新页面后定位置新页面的两种方式
刚刚在写Python爬虫的时候用到了selenium , 在跳转新页面时发现无法定位新页面 , 查找不到新页面的元素 一番查询后得到了解决方法 , 便记录下来备忘 , 也与大家分享 # 页面跳转代码. ...
- B1056 组合数的和 (15分)
B1056 组合数的和 (15分) 给定 N 个非 0 的个位数字,用其中任意 2 个数字都可以组合成 1 个 2 位的数字.要求所有可能组合出来的 2 位数字的和.例如给定2.5.8,则可以组合出: ...
- 8 django 里面的API
1.什么是API? 2.在djang里面写API 3.API实战效果 1.移动端的网页 4.restframework :老师方法 (0)安装 Django REST framework 是一个强大且 ...
- 第八届蓝桥杯C/C++ B组省赛----分巧克力
分巧克力 问题描述 儿童节那天有K位小朋友到小明家做客.小明拿出了珍藏的巧克力招待小朋友们. 小明一共有N块巧克力,其中第i块是Hi x Wi的方格组成的长方形. 为了公平起见,小明需要从这 N 块巧 ...