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. PHP array_diff()

    定义和用法 array_diff() 函数返回两个数组的差集数组.返回的数组的元素都取自被比较的数组(既第一个数组). 在返回的数组中,键名保持不变. 语法 array_diff(array1,arr ...

  2. BZOJ 1605 [Usaco2008 Open]Crisis on the Farm 牧场危机 DP

    题意:链接 方法: DP 解析: 第一眼搜索题,复杂度不同意dfs,并且牛的数量太多不能bfs,迭代更不可能,A*不会估价.可能记忆化? 等等记忆化我还搜个毛线- 直接改成DP就好了. 状态非常好想非 ...

  3. Nginx配置httpsserver

    配置HTTPS主机.必须在server配置块中打开SSL协议,还须要指定服务器端证书和密钥文件的位置: server { listen 443;  #要加密的域名 server_name www.te ...

  4. Git版本号控制:Git分支处理

    http://blog.csdn.net/pipisorry/article/details/46958699分支的意义创建分支能够避免提交代码后对主分支的影响,同一时候也使你有了相对独立的开发环境. ...

  5. Fisher 线性判别

    Multiplying both sides of this result by wT and adding w0, and making use of y(x)=wTx+w0 and  y(xΓ)= ...

  6. ISLR学习笔记

    目录 C1 Introduction to Statistical Learning 1.1Statistical Learning介绍: 1.1.1 估计 \(f\) 的目的:prediction和 ...

  7. kafka参数在线修改

    当kafka集群单个节点出现磁盘满了,需要清理历史topic数据:方法如下 1): 停掉kafka进程,将kafka的server.properties中的log.retention.hours=1/ ...

  8. 【寒假集训系列DAY.1】

    Problem A. String Master(master.c/cpp/pas) 题目描述 所谓最长公共子串,比如串 A:“abcde”,串 B:“jcdkl”,则它们的最长公共子串为串 “cd” ...

  9. A - Next Round

    Problem description "Contestant who earns a score equal to or greater than the k-th place finis ...

  10. 总结java基础

    第一章总结: 1.java的是sun公司(现甲骨文有限公司)于1995年推出的高级编程语言,java技术可以应用在几乎所有类型和规模的设备上,小到计算机芯片.蜂窝电话,大到超级计算机,无所不在. 2. ...