Source:

PAT A1072 Gas Station (30 分)

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

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的更多相关文章

  1. [LeetCode] Gas Station 加油站问题

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

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

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

  4. 【leetcode】Gas Station

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

  5. [LeetCode] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

  6. 20. Candy && Gas Station

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  7. LeetCode——Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  8. Gas Station

    Description: There are N gas stations along a circular route, where the amount of gas at station i i ...

  9. Gas Station [LeetCode]

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

随机推荐

  1. N天学习一个linux命令之ps

    ps命令 用途 显示系统进程信息 用法 ps [options] 常用选项 选项有三种风格,这里是指Unix风格 (Unix,BSD,GNU LONG OPTIONS) 简单刷选类 -A, -e 显示 ...

  2. 怎样在BIOS中设置RAID?

    随着价格的下降和相应主板的支持.眼下SATA硬盘已经逐渐成为主流. 但因为受芯片组和操作系统的影响.不少用户对SATA硬盘的使用及安装系统掌握不足,今天小编就给大家介绍一下SATA硬盘的日常应用技巧. ...

  3. 我的Android进阶之旅------&gt;Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. leetcode第一刷_Convert Sorted Array to Binary Search Tree

    晕.竟然另一样的一道题.换成sorted array的话.找到中间位置更加方便了. TreeNode *sortTree(vector<int> &num, int start, ...

  5. redis client 2.0.0 pipeline 的list的rpop bug

    描写叙述: redis client 2.0.0 pipeline 的list的rpop 存在严重bug,rpop list的时候,假设list已经为空的时候,rpop出来的Response依旧不为n ...

  6. Android和H5交互-基础篇

    hybrid App开发也不是什么新鲜事了,其中native和h5之间的交互则是必不可少的.Android中是如何和H5交互的? 1.webView加载页面 我们都知道在Android中是通过webV ...

  7. C++ Web 编程(菜鸟教程)

    C++ Web 编程(菜鸟教程) C++ Web 编程 什么是 CGI? 公共网关接口(CGI),是一套标准,定义了信息是如何在 Web 服务器和客户端脚本之间进行交换的. CGI 规范目前是由 NC ...

  8. C++_homework_EraseComment

    顾名思义就是删除程序中的注释,不清楚fsm的机制,完全是自己的思路做. 开头先读取一个字符确定是否到文件结尾,如果读取成功,是换行的话就换行,并继续读取,不是的话,用putback放回缓冲区,并整行读 ...

  9. insufficient space

  10. leetcode数组相关

    目录 4寻找两个有序数组的中位数 11盛最多水的容器,42接雨水 15三数之和,16最接近的三数之和,18四数之和 26/80删除排序数组中的重复项, 27移除元素 31下一个排列 53最大子序和 5 ...