【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

相关文章

代码随想录算法训练营:12/60

非科班学习算法day12 | LeetCode150:逆波兰表达式 ,Leetcode239: 滑动窗口最大值  目录 介绍 一、基础概念补充: 1.c++字符串转为数字 1. std::stoi, std::stol, std::stoll, std::stoul, std::stoull(最常用) 2. std::stringstream 3. std::atoi, std

人工智能机器学习算法总结神经网络算法(前向及反向传播)

1.定义,意义和优缺点 定义: 神经网络算法是一种模仿人类大脑神经元之间连接方式的机器学习算法。通过多层神经元的组合和激活函数的非线性转换,神经网络能够学习数据的特征和模式,实现对复杂数据的建模和预测。(我们可以借助人类的神经元模型来更好的帮助我们理解该算法的本质,不过这里需要说明的是,虽然名字是神经网络,并且结构等等也是借鉴了神经网络,但其原型以及算法本质上还和生物层面的神经网络运行原理存在

Python应用开发——30天学习Streamlit Python包进行APP的构建(9)

st.area_chart 显示区域图。 这是围绕 st.altair_chart 的语法糖。主要区别在于该命令使用数据自身的列和指数来计算图表的 Altair 规格。因此,在许多 "只需绘制此图 "的情况下,该命令更易于使用,但可定制性较差。 如果 st.area_chart 无法正确猜测数据规格,请尝试使用 st.altair_chart 指定所需的图表。 Function signa

大林 PID 算法

Dahlin PID算法是一种用于控制和调节系统的比例积分延迟算法。以下是一个简单的C语言实现示例: #include <stdio.h>// DALIN PID 结构体定义typedef struct {float SetPoint; // 设定点float Proportion; // 比例float Integral; // 积分float Derivative; // 微分flo

PAT-1039 到底买不买(20)(字符串的使用)

题目描述 小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如,YrR8RrY是小红想做的珠串;那么ppRYYGrrYBR2258可以

PAT-1028

题目描述 某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月6日,所以超过200岁的生日和未出生的生日都是不合理的,应该被过滤掉。 输入描述: 输入在第一行给出正整数N,取值在(0, 105];随后N行,每行给出1个人的姓名(由不超过5个

LeetCode 算法:二叉树的中序遍历 c++

原题链接🔗:二叉树的中序遍历 难度:简单⭐️ 题目 给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。 示例 1: 输入:root = [1,null,2,3] 输出:[1,3,2] 示例 2: 输入:root = [] 输出:[] 示例 3: 输入:root = [1] 输出:[1] 提示: 树中节点数目在范围 [0, 100] 内 -100 <= Node.

【Java算法】滑动窗口 下

​ ​    🔥个人主页: 中草药 🔥专栏:【算法工作坊】算法实战揭秘 🦌一.水果成篮 题目链接:904.水果成篮 ​ 算法原理 算法原理是使用“滑动窗口”(Sliding Window)策略,结合哈希表(Map)来高效地统计窗口内不同水果的种类数量。以下是详细分析: 初始化:创建一个空的哈希表 map 用来存储每种水果的数量,初始化左右指针 left

ROS2从入门到精通4-4:局部控制插件开发案例(以PID算法为例)

目录 0 专栏介绍1 控制插件编写模板1.1 构造控制插件类1.2 注册并导出插件1.3 编译与使用插件 2 基于PID的路径跟踪原理3 控制插件开发案例(PID算法)常见问题 0 专栏介绍 本专栏旨在通过对ROS2的系统学习,掌握ROS2底层基本分布式原理,并具有机器人建模和应用ROS2进行实际项目的开发和调试的工程能力。 🚀详情:《ROS2从入门到精通》 1 控制插

算法与数据结构面试宝典——回溯算法详解(C#,C++)

文章目录 1. 回溯算法的定义及应用场景2. 回溯算法的基本思想3. 递推关系式与回溯算法的建立4. 状态转移方法5. 边界条件与结束条件6. 算法的具体实现过程7. 回溯算法在C#,C++中的实际应用案例C#示例C++示例 8. 总结回溯算法的主要特点与应用价值 回溯算法是一种通过尝试各种可能的组合来找到所有解的算法。这种算法通常用于解决组合问题,如排列、组合、棋盘游