当前位置:网站首页>PAT 甲级 A1072 Gas Station
PAT 甲级 A1072 Gas Station
2022-08-04 16:02:00 【柘木木】
Author CHEN, Yue
Organization 浙江大学
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 (≤104), 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题意:
有n个居民楼,m个加油站,k条边(是无向图),DS是服务距离,然后要找出在所有加油站中,找出一个加油站,该加油站离居民楼尽可能地远(相对其他加油站),但是该距离又是和居民楼的最短距离设作dis,如果有多个加油站的dis相同,就比较平均距离,平均路径是该加油站到所有居民楼的最短路径之和的平均值,选最小的平均距离的加油站。
思路:
所有加油站使用dijstra算法找到居民楼的最短距离,找到最短距离中的最短距离,和其他的加油站的最短距离中的最短距离比较,找出最短距离中的最短距离的最长距离,这个就是dis,如果相等就比较平均距离。
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1020;
const int INF = 1 << 30 - 1;
int G[maxn][maxn];
int d[maxn];
bool vis[maxn] = {false};
int n, m, k, DS;
void Dijkstra(int st) {
fill(d, d + maxn, INF);
memset(vis, false, sizeof(vis));
d[st] = 0;
for(int i = 0; i < n + m; i++) {//单源最短路径,总顶点数要明确;
int u = -1, MIN = INF;
for(int j = 1; j <= n + m; j++) {
if(vis[j] == false && d[j] < MIN) {
u = j;
MIN = d[j];
}
}
if(u == -1) return ;
vis[u] = true;
for(int v = 1; v <= n + m; v++) {
if(vis[v] == false && G[u][v] != INF) {
if(d[u] + G[u][v] < d[v]) {
d[v] = d[u] + G[u][v];
}
}
}
}
}
int getID(char s[]) {//给顶点编号;
int len = strlen(s);
int id = 0;
int i = 0;
//字符串转数字;
while(i < len) {
if(s[i] != 'G') {
id = id * 10 + (s[i] - '0');
}
i++;
}
if(s[0] == 'G') return n + id;
else {
return id;
}
}
int main () {
scanf("%d %d %d %d", &n, &m, &k, &DS);
//居民楼个数,加油站个数,无向图的边数,服务范围;
fill(G[0], G[0] + maxn * maxn, INF);
for(int i = 0; i < k; i++) {
char city1[5], city2[5];
int w;
scanf("%s %s %d", &city1, &city2, &w);
int u = getID(city1);
int v = getID(city2);
G[u][v] = G[v][u] = w;
}//建图;
double ansDis = -1, ansAvg = INF;//1,相对加油站中,离居民楼最远 2.相对居民楼,加油站和居民楼最近
int ansID = -1;//赋值判断是否有解;
for(int i = n + 1; i <= n + m; i++) {//遍历所有加油站;
Dijkstra(i);
double mindis = INF;//单源最短距离;
double avg = 0;
//遍历该加油站的最短距离;
for(int j = 1; j <= n; j++) {//只需要遍历到居民楼的距离;
if(d[j] > DS) {//超过服务距离,该服务站不合格;
mindis = -1;
break;
}
if(d[j] < mindis) {
mindis = d[j];
}
avg += 1.0 * d[j] / n;
}//找出最短距离和平均距离;
if(mindis == -1) continue;
if(mindis > ansDis) {
ansDis = mindis;
ansID = i;
ansAvg = avg;
}else if(mindis == ansDis && avg < ansAvg) {
ansID = i;
ansAvg = avg;
}
}
if(ansID == -1) {
printf("No Solution");
}else {
printf("G%d\n", ansID - n);
printf("%.1f %.1f", ansDis, ansAvg);
}
return 0;
}注意:
测试点1,哪里写的是3.3,其实输出的是3.2也是对的(平均距离是3.25,保留一位就是3.2)
边栏推荐
- 面试官:多个线程执行完毕后,才执行另一个线程,该怎么做?
- seaborn
- 农产品期货开户哪家好??
- 饿了么智能头盔专利获授权 进一步提升骑手安全保障
- Mysql Explain
- 云存储硬核技术内幕——小结(上)
- Projector reached the party benefits 】 【 beginners entry - brightness projection and curtain selection - from entry to the master
- 寻找消失的类名
- 字节API鉴权方法
- NFT blind box mining system dapp development NFT chain game construction
猜你喜欢
随机推荐
DocuWare平台——用于文档管理的内容服务和工作流自动化的平台(上)
第三章 Scala运算符
seaborn
皕杰报表配置文件report_config.xml里都配置了什么?
For循环控制
Analysis of Http-Sumggling Cache Vulnerability
Summary of some pytorch knowledge points that have been updated for a long time
UWP 转换 IBuffer 和其他类型
攻防视角下,初创企业安全实战经验分享
dot net double 数组转 float 数组
在Markdown文件中快速插入本地图片
Many merchants mall system function and dismantling 24 - ping the strength distribution of members
贝叶斯优化核极限学习机KELM用于回归预测
保证通信的机制有哪些
Crawler Xiaobai Notes (yesterday's supplement to pay attention to parsing data)
Xi'an Zongheng Information × JNPF: Adapt to the characteristics of Chinese enterprises, fully integrate the cost management and control system
LeetCode·每日一题·1403.非递增顺序的最小子序列·贪心
实战:10 种实现延迟任务的方法,附代码!
DMS 有接口获取每个实例下的数据库列表吗
不需要服务器,教你仅用30行代码搞定实时健康码识别









