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 (≤), 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 D​S​​, 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
作者: CHEN, Yue
单位: 浙江大学
时间限制: 200 ms
内存限制: 64 MB

题意:

给出一个图,其中有 N <= 103 个节点是居民房,M
<= 10 个节点是计划建造加油站的候选点。给出加油站所能服务的最远距离 D。要求计算出合适的位置建造加油站,满足如下优先级条件:

1.所有居民房必须在加油站的服务距离内。

2.所有居民房中距离加油站的最近的居民房与加油站之间的距离是最远的。(大概是安全方面的考虑,加油站要离居民区远一点)

3.所有房间距离加油站的最小距离的总和最小。(节约居民加油的总体成本)

4.同等条件下,序号越小的加油站优先。

题解:

实际上是求加油站到所有点的最短路径的问题,使用 Dijsktra 可以满足。

另外,需要考虑求最短路径的过程中是否要将其他加油站所构建的路径算入在内

思路我很快想到了,但是写的不够熟练,粗心挑了挺久的bug,后来最后的一个测试点没过,原因是输入的时候我用的的char,没有考虑两位数三位数,像G13,2000这种。

AC代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
int e[][];
int dis[][];//dijstra
int v[];
int n,m,k,ds;
string p;
struct node{
int id;
int mi;
int sum;
}ans[];
bool cmp(node x,node y){
if(x.mi==y.mi){
if(x.sum==y.sum){
return x.id<y.id;
}
return x.sum<y.sum;
}
return x.mi>y.mi;
}
void dijstra(int s){
for(int i=;i<=n+m;i++){
dis[s][i]=e[i][s+n];
}
memset(v,,sizeof(v));
v[s+n]=;
int kk=-;
int mm=inf;
for(int i=;i<=n+m;i++){
kk=-;
mm=inf;
for(int j=;j<=n+m;j++){
if(!v[j]&&dis[s][j]<mm){
mm=dis[s][j];
kk=j;
}
}
if(kk==-) break;
v[kk]=;
for(int j=;j<=n+m;j++){
if(v[j]!=&&dis[s][j]>dis[s][kk]+e[kk][j]){
dis[s][j]=dis[s][kk]+e[kk][j];
}
}
}
}
int main(){
cin>>n>>m>>k>>ds;
for(int i=;i<=n+m;i++){
for(int j=;j<=n+m;j++){
e[i][j]=inf;
if(i==j) e[i][j]=;
}
}
for(int i=;i<=k;i++){
int u=,v=,dd;
cin>>p;//输入不能用char,可能会出现G11,199这样的数字
if(p[]=='G'){
for(int j=;j<p.length();j++){
u=u*+p[j]-'';
}
u+=n;
}else{
for(int j=;j<p.length();j++){
u=u*+p[j]-'';
}
}
cin>>p;
if(p[]=='G'){
for(int j=;j<p.length();j++){
v=v*+p[j]-'';
}
v+=n;
}else{
for(int j=;j<p.length();j++){
v=v*+p[j]-'';
}
}
cin>>dd;
if(dd<e[u][v]){
e[u][v]=e[v][u]=dd;
}
}
int num=;//记录备选答案的个数
int f;//标记是否超出范围
for(int i=;i<=m;i++){
dijstra(i);
int min_d=inf;//最小距离
int sum_d=;//距离和
f=;
for(int j=;j<=n;j++){
if(dis[i][j]>ds){//超出范围不放入ans
f=;
break;
}
if(min_d>dis[i][j]){
min_d=dis[i][j];
}
sum_d+=dis[i][j];
}
if(f){
ans[++num].id=i;
ans[num].mi=min_d;
ans[num].sum=sum_d;
}
}
if(num==) cout<<"No Solution";
else{
sort(ans+,ans++num,cmp);
cout<<"G"<<ans[].id<<endl;
printf("%.1f %.1f",ans[].mi*1.0,ans[].sum*1.0/n);
}
return ;
}

PAT 甲级 1072 Gas Station (30 分)(dijstra)的更多相关文章

  1. pat 甲级 1072. Gas Station (30)

    1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...

  2. PAT甲级——1072 Gas Station

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  3. 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 ...

  4. 【PAT甲级】1072 Gas Station (30 分)(Dijkstra)

    题意: 输入四个正整数N,M,K,D(N<=1000,M<=10,K<=10000)分别表示房屋个数,加油站个数,路径条数和加油站最远服务距离,接着输入K行每行包括一条路的两条边和距 ...

  5. 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 ...

  6. PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)

    1030 Travel Plan (30 分)   A traveler's map gives the distances between cities along the highways, to ...

  7. 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 ...

  8. 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 ...

  9. 1072. Gas Station (30)

    先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...

随机推荐

  1. Eclipse中修改了项目,导入Tomcat中时,括号显示原来项目的名字

    Eclipse中Tomcat导入项目并且修改了项目名字,把项目添加到Tomcat上,发现在项目后面带了个括号里面显示原来项目的名字,并且在访问的时候也只能用原来的项目名访问,怎么办呢? 1.打开你的项 ...

  2. 【学习笔记】Baby Step Giant Step算法及其扩展

    1. 引入 Baby Step Giant Step算法(简称BSGS),用于求解形如\(a^x\equiv b\pmod p\)(\(a,b,p\in \mathbb{N}\))的同余方程,即著名的 ...

  3. Spring入门(三)——AOP

    1. AOP aspect object programming ,简单来说就是把重复的代码抽取出来,然后再需要用到的地方进行切入,这里讲解基于接口的注解实现 2. 了解 关注点:即重复的代码 切面: ...

  4. http://www.tldp.org/LDP/abs/abs-guide.txt.gz

    http://www.tldp.org/LDP/abs/abs-guide.txt.gz

  5. Linux下搭建iSCSI共享存储的方法 Linux-IO Target 方式 Debian9.5下实现

    iSCSI(internet SCSI)技术由IBM公司研究开发,是一个供硬件设备使用的.可以在IP协议的上层运行的SCSI指令集,这种指令集合可以实现在IP网络上运行SCSI协议,使其能够在诸如高速 ...

  6. Cogs 763. [USACO Open09] 数字的游戏(博弈)

    [USACO Open09] 数字的游戏 ★☆ 输入文件:cdgame.in 输出文件:cdgame.out 简单对比 时间限制:1 s 内存限制:128 MB Bessie正跟FJ玩一个数字游戏,她 ...

  7. 本地spark下保存rdd为文件

    写随笔大概也是做笔记记录下自己思考的意思吧,之前有些事情觉得做随笔还是比较有用的,mark一下一个有用的网址 关于rdd的操作,网上有很多很多的教程,当初全部顺一遍,除了对rdd这个类型有了点概念,剩 ...

  8. mac clion c/c++环境配置

    下载安装:https://www.cnblogs.com/sea-stream/p/11220036.html 切换语言:https://www.cnblogs.com/sea-stream/p/11 ...

  9. 实体类(VO,DO,DTO,PO)的划分《转载---》

    转载自:https://blog.csdn.net/u010722643/article/details/61201899 经常会接触到VO,DO,DTO的概念,本文从领域建模中的实体划分和项目中的实 ...

  10. 巧用DNSlog实现无回显注入【转载】

    原作者:afanti 原出处:https://www.cnblogs.com/afanti/p/8047530.html 0x00 简介 测试一些网站的时候,一些注入都是无回显的,我们可以写脚本来进行 ...