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

即对每个加油站都是用Dijkstra计算最小距离
 #include <iostream>
#include <vector>
#include <string>
#define inf 999999999
using namespace std;
int N, M, K, Ds, indexG = -;
double minG = -, avgG;
int city[][];
int main()
{
cin >> N >> M >> K >> Ds;
fill(city[], city[] + * , inf);
for (int i = ; i < K; ++i)
{
string s1, s2;
int a = , b = , dis;
cin >> s1 >> s2 >> dis;
if (s1[] == 'G')
{
a += N;
s1.erase(, );
}
if (s2[] == 'G')
{
b += N;
s2.erase(, );
}
a += stoi(s1);
b += stoi(s2);
city[a][b] = city[b][a] = dis;
}
//Dijsktra
for (int k = N+; k <= N+M; ++k)//每个加油站都是用一次dij
{
int temp[];
fill(temp, temp + , inf);
bool visit[];
fill(visit, visit + , false);
temp[k] = ;
for (int i = ; i <= N+M; ++i)
{
int v = -, minDis = inf;
for (int j = ; j <= N+M; ++j)
{
if (visit[j] == false && minDis > temp[j])
{
v = j;
minDis = temp[j];
}
}
if (v == -)break;
visit[v] = true;
for (int u = ; u <= N+M; ++u)
{
if (visit[u] == false && city[v][u] != inf)
{
if (temp[u] > temp[v] + city[v][u])
temp[u] = temp[v] + city[v][u];
}
}
}
int flag = , minD = inf;
double avgD = 0.0;
for (int i = ; i <= N; ++i)
{
minD = minD < temp[i] ? minD : temp[i];
avgD += (double)temp[i];
if (temp[i] > Ds)
{
flag = ;
break;
}
}
avgD /= N;
if (flag == && minG <= minD)
{
if ((minG < minD) || (minG == minD && avgD < avgG))
{
minG = minD;
indexG = k;
avgG = avgD;
}
else if (minG == minD && avgD == avgG)
indexG = indexG < k ? indexG : k;
}
}
if (indexG == -)
cout << "No Solution" << endl;
else
printf("G%d\n%.1f %.1f\n", indexG - N, minG, avgG);
return ;
}

PAT甲级——1072 Gas Station的更多相关文章

  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 (30 分)(dijstra)

    1072 Gas Station (30 分)   A gas station has to be built at such a location that the minimum distance ...

  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[图论][难]

    1072 Gas Station (30)(30 分) A gas station has to be built at such a location that the minimum distan ...

  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 1072. Gas Station (30)

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

  7. PAT 1072. Gas Station

    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)

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

  9. 1072. Gas Station (30) 多源最短路

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

随机推荐

  1. Spring Cloud高级视频

    Spring Cloud高级视频 第一章 微服务架构概述 第二章 开始使用Spring Cloud实战微服务 第三章 服务提供者与服务消费者 第四章 服务发现与服务注册 第五章 使用Hystrix保护 ...

  2. css 写一个向右的箭头

    经常写移动端页面时会用到向右的箭头,之前都是用图片,突然想到用css应该也能写,于是一搜还确实有 width: 7px; height: 7px; border-top: 2px solid #fff ...

  3. window.onload=function(){};

    window.onload=function(){}; 只要页面加载完毕,这个事件才会触发 扩展事件--页面关闭后才触发的事件 window.onunload=function(){}; 扩展事件-- ...

  4. Android开发 自定义View_白色圆型涟漪动画View

    代码: import android.animation.ValueAnimator; import android.content.Context; import android.graphics. ...

  5. [JZOJ1900] 【2010集训队出题】矩阵

    题目 题目大意 题目化简一下,就变成: 构造一个\(01\)数列\(A\),使得\(D=\sum A_iA_jB_{i,j}-\sum A_iC_i\)最大. 问这个最大的\(D\)是多少. 正解 其 ...

  6. jquery选择器中中>和空格的区别

    空格:$('parent childchild')表示获取parent下的所有的childchild节点 大于号:$('parent > childchild')表示获取parent下的所有下一 ...

  7. Oracle大数据查询优化

    1.对于像状态之类的列,不是很多的,就可以加位图索引,对于唯一的列,就加唯一索引,其余的创建普通索引. 2.尽量不要使用select * 这样的查询,指定需要查询的列. 3.使用hits  selec ...

  8. 重装系统后配置原有的mysql

    1.重装系统后配置原有的mysql 2.修改 my.ini [修改 basedir:MySQL当前所在路径 datadir  数据存放路径] [mysqld] # 设置3306端口 port= # 设 ...

  9. H5 回到顶部按钮

    //这里我用的是svg,所以代码老长了,可以把svg部分换成自己的图片什么的$(function(){ var obj = $('<div style="width:1.3rem;po ...

  10. c# sleep 例子

    using System; using System.Threading; public class arr { public static void Main() { //int[] arr; // ...