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. 洛谷——P2871 [USACO07DEC]手链Charm Bracelet

    https://www.luogu.org/problem/show?pid=2871 题目描述 Bessie has gone to the mall's jewelry store and spi ...

  2. 洛谷——P1031 均分纸牌

    https://www.luogu.org/problem/show?pid=1031#sub 题目描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以 ...

  3. 解决MYSQL的You can't specify target table 'xxxxxxxxxx' for update in FROM clause

    出现这个问题的MYSQL的SQL语句形如: DELETE FROM xxxxa WHERE EXISTS (SELECT * FROM xxxx1 WHERE xxxxa.xxid=123) 解决方法 ...

  4. EasyUI 在textbox里面输入数据敲回车后查询和普通在textbox输入数据敲回车的区别

    EasyUI实现回车键触发事件 $('#id').textbox('textbox').keydown(function (e) { if (e.keyCode == 13) { alert('ent ...

  5. c++实现数据结构1.顺序表

    头文件seqlist.h #ifndef _SEQLIST_H_ #define _SEQLIST_H_ #include<iostream> using namespace std; t ...

  6. python初码

    第一次用python写代码,有一些不习惯,比如if.else.for.def后要加:.假设换一个编译工具可能会好点,否则仅仅能每次执行的时候查看错误信息.它的优点也正是这里不须要每条语句输入;.不须要 ...

  7. Ambari-部署常见问题

    重新启动ambari-server端后调用install.start API后返回200 导致该问题的解决办法是server在启动后没有收到agent的心跳即没有与agent建立连接,在此时进行API ...

  8. 重学C++ (十一) OOP面向对象编程(2)

    转换与继承 本节主要须要区分的是: 基类和派生类的转换: 引用(指针)的转换和对象的转换. 1.每一个派生类对象包括一个基类部分.因此.能够像使用基类对象一样在派生类对象上执行操作. 基于这一点,能够 ...

  9. 应用程序 /dev/rtc 编程 获取时间 2011-12-13 01:01:06【转】

    本文转载自:http://blog.chinaunix.net/uid-16785183-id-3040310.html 分类: 原文地址:应用程序 /dev/rtc 编程 获取时间 作者:yuwei ...

  10. SQL数据库问题 解释一下下面的代码 sql 存储过程学习

    SQL数据库问题 解释一下下面的代码 2008-08-13 11:30wssqyl2000 | 分类:数据库DB | 浏览1154次 use mastergocreate proc killspid( ...