pat 甲级 1072. Gas Station (30)

1072. Gas Station (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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] = 0;que.push(P(0,s));while (!que.empty()) {P p = que.top(); que.pop();int v = p.second;if (d[v] < p.first)continue;for (int i = 0; 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[0] == 'G') {if (s.size() == 3)return m+n;//m最大为10,唯一的三位数else return s[1] - '0'+n;}else {return atoi(s.c_str());}
}
string recover(int id) {string s="G";s += '0' + id-n;return s;
}int main() {while (cin>>n>>m>>k>>d_max) {V = n + m+1;for (int i = 0; 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 = -1, max_avedist = -1; int id;for (int i = 1; i <= m;i++) {//对于每一个stationbool flag = 1;//判断当前情况是否可以int pos = n + i;dijkstra(pos);double tmp_ave = 0;int tmp_min = INF;for (int j = 1; j <= n; j++) {if (d[j] > d_max) {flag = 0;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 == -1)puts("No Solution");else {cout << recover(id) << endl;printf("%.1f %.1f\n",max_mindist,max_avedist);}}return 0;
}

 

转载于:https://www.cnblogs.com/ZefengYao/p/8556088.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/369427.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

骑士周游问题

骑士周游问题 问题&#xff1a;在一个 8*8 的棋盘上&#xff0c;马按照“日”字走&#xff0c;给定一个起点&#xff0c;打印出马不重复的走完棋盘64个格子的路径。 解答&#xff1a;递归 回溯 &#xff08;对于任一步&#xff0c;马能走的下一步有8个方向&#xff0c;但是需要…

那些容易遗忘的web前端问题

背景&#xff1a; 年底将至&#xff0c;本人这只才出门的前端菜鸟&#xff0c;终于有空闲的时间来整理一下最近投简历时出现的问题。有的是经常使用但是没有仔细留意造成的&#xff1b;有的是个人认为根本没人使用而忽略的。为了下次不出现这种错误&#xff0c;进行一下总结。…

使用IntelliJ IDEA的原因

介绍 我经常遇到一个问题&#xff0c;为什么我使用Intellij来支持另一个IDE&#xff08;在本例中为Eclipse&#xff09;。 大多数时候&#xff0c;我会通过演示IntelliJ的某些功能并展示一切的集成程度来回答这个问题。 这让我开始思考使用它的真正原因是什么。 这篇文章将试图…

linux光标美化包,使用 [ powerlevel10k ] 美化你的WSL (Linux)

使用 [ powerlevel10k ] 美化你的WSL (Linux)使用 [ powerlevel10k ] 美化你的WSL (Linux)前言关于linux终端的美化&#xff0c;网上的教程有很多&#xff0c;但对于国内的用户来说&#xff0c;效果往往是这样的&#xff1a;教程中通过以下命令安装 oh-my-zshsh -c "$(cur…

HashMap实现原理分析

1 HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储&#xff0c;但这两者基本上是两个极端。 数组 数组存储区间是连续的&#xff0c;占用内存严重&#xff0c;故空间复杂的很大。但数组的二分查找时间复杂度小&#xff0c;为O(1)&#xff1b;数组的特点是&#xf…

opencv3.2.0在vs2015下安装与配置

准备工作 VS2015OpenCV 3.2.0OpenCV配置环境变量&#xff0c;path下添加\opencv\build\x64\vc14\bin&#xff0c;新设置的环境变量需要重启才能使用测试工程 新建VC控制台空项目修改平台为x64&#xff0c;这一步先做源文件中加入main.cpp&#xff0c;测试代码&#xff1a;#incl…

CSS实现响应式布局(自动拆分几列)

1.css代码 <style type"text/css">.container{margin-top: 10px;}.outerDiv{float:left;width:100%;}/* 大于648像素一行两个div&#xff0c;innerDiv两个宽度为&#xff1a;(300 4 20)*2 */media screen and (min-width: 648px){.outerDiv {width: 50%}}.inne…

如何使用字节序列化双精度数组(二进制增量编码,用于低差单调浮点数据集)...

低延迟系统需要高性能的消息处理和传递。 由于在大多数情况下&#xff0c;数据必须通过有线传输或进行序列化才能保持持久性&#xff0c;因此编码和解码消息已成为处理管道的重要组成部分。 高性能数据编码的最佳结果通常涉及应用程序数据细节的知识。 本文介绍的技术是一个很好…

error

for(int i1;i<size;i) { if(ba[i]) { pos i1; break; } }输入&#xff1a; a{4,5,7,4,6,8},b4 输出&#xff1a; 位置是4&#xff08;错误&#xff0c;这儿应该是1&#xff0c;但程序未失败。&#xff09;改成&#xff1a;for(int i0;i<size;i) { if(ba[i]) { pos i1; …

c语言第一次作业,C语言培训班第一次作业 (1)

1、以下叙述中正确的是()(A)、用户所定义的标识符不允许使用关键字。(B)、分号是C语句之间的分隔符&#xff0c;不是语句的一部分。(C)、花括号“&#xff5b;&#xff5d;”只能作为函数体的定界符。(D)、构成C程序的基本单位是函数&#xff0c;所有函数都可以由用户命名。1、…

2.Python爬虫入门二之爬虫基础了解

1.什么是爬虫 爬虫&#xff0c;即网络爬虫&#xff0c;大家可以理解为在网络上爬行的一直蜘蛛&#xff0c;互联网就比作一张大网&#xff0c;而爬虫便是在这张网上爬来爬去的蜘蛛咯&#xff0c;如果它遇到资源&#xff0c;那么它就会抓取下来。想抓取什么&#xff1f;这个由你来…

对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)

本文属于本人原创&#xff0c;转载请注明出处&#xff1a;http://blog.csdn.net/xxd851116/archive/2009/06/25/4296866.aspx 【前面的话】 在网上经常看到有人对request.getSession(false)提出疑问&#xff0c;我第一次也很迷惑&#xff0c;看了一下J2EE1.3 API&#xff0c;看…

实现自定义的未来

上一次我们学习了java.util.concurrent.Future<T>背后的原理 。 我们还发现&#xff0c; Future<T>通常由库或框架返回。 但是没有什么可以阻止我们在有意义的情况下自行实现所有功能。 它不是特别复杂&#xff0c;可以显着改善您的设计。 我尽力为我们的示例选择有…

c语言中的两个百分号什么意思,百分号的用法,特别是在两个量词之间的用法,例如50%—70%和50—70%...-百分号-语文-彭都宰同学...

概述&#xff1a;本道作业题是彭都宰同学的课后练习&#xff0c;分享的知识点是百分号&#xff0c;指导老师为屠老师&#xff0c;涉及到的知识点涵盖&#xff1a;百分号的用法&#xff0c;特别是在两个量词之间的用法&#xff0c;例如50%—70%和50—70%...-百分号-语文&#xf…

Markdown 语法和 MWeb 写作使用说明

# Markdown 语法和 MWeb 写作使用说明 ## Markdown 的设计哲学 > Markdown 的目標是實現「易讀易寫」。> 不過最需要強調的便是它的可讀性。一份使用 Markdown 格式撰寫的文件應該可以直接以純文字發佈&#xff0c;並且看起來不會像是由許多標籤或是格式指令所構成。>…

微信小程序 引入公共页面的几种情况

1、不带参数 首先在pages文件夹中新建一个template文件夹&#xff0c;文件夹中新建一个template.wxml文件&#xff0c;代码如下 <!--template.wxml--> <template name"msgItem"><view><text>This is template.wxml文件&#xff0c;我是一个…

Python学习笔记----基础篇10----模块2

8&#xff09;json& pickle 用于序列化的两个模块 json&#xff0c;用于处理字符串和python数据类型间进行转换 pickle&#xff0c;用于python特有的类型和python的数据类型间进行站换 Json模块提供了四个功能&#xff1a;dumps、dump、loads、load pickle模块提供了四个功…

易语言自定义数据类型转c,一步一步跟我学易语言之自定义数据类型

自定义数据类型什么是“自定义数据类型”&#xff1f;顾名思义&#xff0c;就是用户可以随时在程序中自行定义新的数据类型。自定义数据类型时需要设置数据类型的名称及其成员。数据类型成员各属性的设置方法等同于变量设置时相应属性的设置方法。双击“程序”中的“自定义数据…

(第2部分,共3部分):有关性能调优,Java中的JVM,GC,Mechanical Sympathy等的文章和视频的摘要...

这是以前的文章&#xff08;第3部分&#xff0c;共1部分&#xff09;的继续&#xff1a;有关性能调优&#xff0c;Java中的JVM&#xff0c;GC&#xff0c;Mechanical Sympathy等的文章和视频的提要 。 事不宜迟&#xff0c;让我们开始使用我们的下一组博客和视频&#xff0c;印…

Redis初步整理

1&#xff0c;Redis 简介 Redis 是完全开源免费的&#xff0c;遵守BSD协议&#xff0c;是一个高性能的key-value数据库。 Redis 与其他 key - value 缓存产品有以下三个特点&#xff1a; Redis支持数据的持久化&#xff0c;可以将内存中的数据保持在磁盘中&#xff0c;重启的时…