题目

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 (<= 103), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 10^4), the number of roads connecting the houses and the gas stations; and DS, 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

题意

加油站选址,有m个位置可供选择,取离城市中最近的住房距离最远的位置,若有多个满足条件的位置,取到所有住房的平均距离最短的位置

题目分析

已知图的n个顶点、边、边权,提供另外m个顶点位置,寻找到这n个顶点最近顶点的距离最短的位置,若有多个,取到n个顶点平均距离最短的位置,打印该位置距n个顶点最近的点的距离,和到n个顶点的平均距离,精确到小数点后1位

解题思路

1 dijkstra求出每个供选择顶点到n个顶点的最近距离mindis,取所有最近距离中距离最远的位置,并记录最小距离和平均距离

易错点

1 每次个供选择的顶点位置dijkstra计算最短距离时,需要重置已访问标记数组

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
const int maxn=1020, INF=999999999;
int n,m,k,ds,g[maxn][maxn];
int dist[maxn],col[maxn];
void dijkstra(int u) {
fill(dist,dist+1020,INF);
fill(col,col+1020,0); //每次统计需要重置col已访问标记数组
dist[u]=0;
for(int i=1; i<=n+m; i++) {
int min=-1,mind=INF;
for(int j=1; j<=n+m; j++) {
if(col[j]==0&&dist[j]<mind) {
mind=dist[j];
min = j;
}
}
if(min==-1)break;
col[min]=1;
for(int j=1; j<=n+m; j++) {
if(g[min][j]==0||col[j]==1)continue;
if(dist[j]>dist[min]+g[min][j]) { //==说明路径相同但是顶点更多,则平均值更小 ||dist[j]==dist[min]+g[min][j]
dist[j]=dist[min]+g[min][j];
}
}
}
}
int main(int argc,char * argv[]) {
scanf("%d %d %d %d",&n,&m,&k,&ds);
string a,b;
int ia,ib,d;
for(int i=0; i<k; i++) {
cin>>a>>b>>d;
if(a[0]=='G')ia=n+stoi(a.substr(1));
else ia=stoi(a);
if(b[0]=='G')ib=n+stoi(b.substr(1));
else ib=stoi(b);
g[ia][ib]=g[ib][ia]=d; //边权
}
// 求每个位置的最短路径
int ansid=-1; // dist[n]中最小值对应的顶点--离加油站最近的顶点
double ansdis=-1,ansavgd=INF; // dist[n]中最小值 --- 距加油站最近的顶点的距离
for(int i=1; i<=m; i++) {
dijkstra(n+i);
//求平均值
double avgd=0,mindis=INF; // avgd 边权平均值; mindis加油站到所有顶点距离的最小值
for(int j=1; j<=n; j++) {
if(dist[j]>ds) {
mindis=-1;
break;
}
if(dist[j]<mindis)mindis=dist[j]; //取所有位置中离城市中最近的住宅最远的加油站位置
avgd+=(1.0*dist[j]); //记录边权
}
if(mindis==-1)continue;
avgd=avgd/n; //距离平均值
if(mindis>ansdis) {
//取离加油站最近城市最远的位置 若相等 取编号最小者
//更新路径信息
ansid=i;
ansdis=mindis;
ansavgd=avgd;
} else if(mindis==ansdis&&avgd<ansavgd) {
ansid=i;
ansavgd=avgd;
}
}
// 打印
if(ansid == -1)
printf("No Solution");
else
printf("G%d\n%.1f %.1f", ansid, ansdis, ansavgd);
return 0;
}

PAT Advanced 1072 Gas Station (30) [Dijkstra算法]的更多相关文章

  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 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]

    题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...

  4. PAT甲题题解-1072. Gas Station (30)-dijkstra最短路

    题意:从m个加油站里面选取1个站点,使得其离住宅的最近距离mindis尽可能地远,并且离所有住宅的距离都在服务范围ds之内.如果有很多相同mindis的加油站,输出距所有住宅平均距离最小的那个.如果平 ...

  5. PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]

    题目 Input our current position and a destination, an online map can recommend several paths. Now your ...

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

  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)

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

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

随机推荐

  1. 1_02_MSSQL课程_T_SQL语句入门

    1.->全名:结构化查询语言(Structured Query Language)    关系数据库管理系统的标准语言. ->SQL主要分为三种语言:DML\DDL\DCL DDL(数据定 ...

  2. 「NOIP2011」观光公交

    传送门 Luogu 解题思路 有点麻烦,幸好 \(O(n^2)\) 能过... 贪心地想一想,我们如果要用加速器,肯定是要选择车上人数最多的时段加速. 但是我们就会面临这样的情况: 加速了,带来了增益 ...

  3. 「NOI2009」二叉查找树

    传送门 Luogu 解题思路 看一眼题面,显然这是一颗 treap ,考虑到这棵 treap 的中序遍历总是不变的,所以我们就先把所有点按照数据值排序,求出 treap 的中序遍历,然后还可以观察到, ...

  4. 2. 引用计数法(Reference Counting)

    1960年,George E. Collins 在论文中发布了引用计数的GC算法. 引用计数法意如了一个概念,那就是"计数器",计数器表示的是对象的人气指数, 也就是有多少程序引用 ...

  5. jsp中获取attribute

    value="<%=request.getSession().getAttribute("username")%>"

  6. 第一个Vue.js案例

    第一个Vue.js案例 使用Vue有如下几步 引入文件头 加入数据输出框 创建Vue对象,定义数据 案例: <!DOCTYPE html> <html lang="en&q ...

  7. 七 联系人与客户多对一配置&联系人列表&分页查询联系人

    联系人管理: 联系人实体类: package com.mycrm.domain; /** * 联系人的实体 * @author jt *CREATE TABLE `cst_linkman` ( `lk ...

  8. Linux分发版本的试用及选择工具

    https://www.forbes.com/sites/jasonevangelho/2019/06/15/how-to-test-drive-200-linux-distributions-wit ...

  9. Springboot注解使用总结

    使用Spring boot已经有段时间了,但是对很多注解的使用经常会遇到模糊甚至不解的地方,这次有时间便总结一下. 注解(Annotation)概念 注解是Java5开始对元数据的支持,注解与注释是有 ...

  10. Java基础学习总结(一)——Java开发学习介绍

    Java平台: 1.J2SE java开发平台标准版 2.J2EE java开发费平台企业版 Java程序需要在虚拟机上才可以运行,换言之只要有虚拟机的系统都可以运行java程序.不同的系统上要安装对 ...