【PAT】1072. Gas Station (30)【dijkstra算法】

2024-04-12 06:18

本文主要是介绍【PAT】1072. Gas Station (30)【dijkstra算法】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述

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 FORMAT

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.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括4个正整数:N(<=10^3),房子的总数;M(<=10),加油站可能选择的位置数;K,(<=10^4),连接房子和加油站的道路数;和Ds,加油站的最远服务距离。假设所有房子都被编号为1-N,所有可能的选址位置被编号为G1到GM。
接着K行每行按照以下格式描述一条路:
P1 P2 Dist
P1和P2代表道路两端,可能是民居或是加油站位置;Distinct代表路的整数距离。

OUTPUT FORMAT

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

翻译:对于每组输入数据,输出一行最佳位置的编号。第二行输出民居最近距离和到所有民居的平均距离。一行内数字之间必须用空格隔开,并且保留1位小数。如果结果不存在,则输出“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模板题,主要就是模拟题目说明,把Gi转换成数字,我是让Gi在前面,民居在后面。注意这道题的条件为先取最近民居距离最远的,否则取平均距离最短的,否则取编号最小的,真的一不注意就会看错。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#define INF 99999999
using namespace std;
int N,M,K,Dot,Ds;
double Min=0.0,Sum=INF,Ave=0.0;
int Des=0; 
struct Edge{int length,to;Edge(int l,int t):length(l),to(t){}bool operator<(const Edge &a)const{return length>a.length;}
};
vector<Edge> v[1020];
priority_queue<Edge> q;
int Judge(char s[]){int length=strlen(s),num=0;for(int i=0;i<length;i++){if(s[i]=='G')continue;if(s[i]>='0'&&s[i]<='9'){num=num*10+s[i]-'0';    }}if(s[0]!='G')num+=M;return num;
}
int d[1020];
void dijkstra(int s){for(int i=0;i<=Dot;i++)d[i]=INF;d[s]=0;q.push(Edge(0,s));double sum=0,Lmin=INF;while(!q.empty()){Edge temp=q.top();q.pop();int t=temp.to;if(d[t]<temp.length)continue;if(t>M&&Lmin>temp.length)Lmin=temp.length;for(int i=0;i<v[t].size();i++){Edge e=v[t][i];if(d[e.to]>d[t]+e.length){d[e.to]=d[t]+e.length; q.push(Edge(d[e.to],e.to));}}}for(int i=M+1;i<=Dot;i++){if(d[i]>Ds)return ;else sum+=d[i];}if(Min<Lmin||(Min==Lmin&&Sum>sum)){Sum=sum;Ave=Sum*1.0/N;Des=s;Min=Lmin;}
}
int main(){scanf("%d%d%d%d",&N,&M,&K,&Ds);char a[5],b[5];int l;for(int i=0;i<K;i++){scanf("\n%s%s%d",a,b,&l);int n1=Judge(a);int n2=Judge(b);v[n1].push_back(Edge(l,n2));v[n2].push_back(Edge(l,n1)); }Dot=N+M;for(int i=1;i<=M;i++){dijkstra(i);}if(Des==0)printf("No Solution\n");else printf("G%d\n%.1lf %.1lf\n",Des,Min,Ave);return 0;
}

这篇关于【PAT】1072. Gas Station (30)【dijkstra算法】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/896330

相关文章

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

shell脚本自动删除30天以前的文件(最新推荐)

《shell脚本自动删除30天以前的文件(最新推荐)》该文章介绍了如何使用Shell脚本自动删除指定目录下30天以前的文件,并通过crontab设置定时任务,此外,还提供了如何使用Shell脚本删除E... 目录shell脚本自动删除30天以前的文件linux按照日期定时删除elasticsearch索引s

TP-Link PDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务

《TP-LinkPDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务》近期,路由器制造巨头普联(TP-Link)在用户群体中引发了一系列重要变动,上个月,公司发出了一则通知,明确要求所... 路由器厂商普联(TP-Link)上个月发布公告要求所有用户必须完成实名认证后才能继续使用普联提供的 D

Python中的随机森林算法与实战

《Python中的随机森林算法与实战》本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房... 目录1、随机森林算法概述2、随机森林的原理3、实现步骤4、分类案例:使用随机森林预测鸢尾花品种4.1

mysql-8.0.30压缩包版安装和配置MySQL环境过程

《mysql-8.0.30压缩包版安装和配置MySQL环境过程》该文章介绍了如何在Windows系统中下载、安装和配置MySQL数据库,包括下载地址、解压文件、创建和配置my.ini文件、设置环境变量... 目录压缩包安装配置下载配置环境变量下载和初始化总结压缩包安装配置下载下载地址:https://d