pat a1030 Travel Plan 【Dijkstra求最短路径】

2-6-1-1 QNX编程入门之进程和线程(一) 文章围绕进程和线程展开介绍。先是将进程类比为房子,线程类比为居住者,阐述其基本概念,如进程占用内存,线程可共享内存。接着讲解同步相关内容,介绍了互斥,线程通过互斥锁来控制对特定资源的访问,像使用浴室一样。还提到优先级,线程按优先级和等待时长来决定获取资源顺序。此外,说明了信号量,以厨房为例,区分计数为 1 和大于 1 的信号情况,且指出信号量可当互斥锁用,但互斥锁是特殊用途信号量,二者存在区别,最后提及后续会介绍其他同步机制。 阅读详情

 题目:

https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392

https://www.nowcoder.com/pat/1/submission-detail/64261014

一、问题描述

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output:

0 2 3 3 40

有N个城市(编号为0~N-1)、M条道路(无向边),并给出M条道路的距离属性与花费属性。

现在给定起点S与终点D,求从起点到终点的最短路径、最短距离及花费。

注意:如果有多条最短路径,则选择花费最小的那条。 

二、算法实现

法1:Dijkstra

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxv=510;
const int inf=1000000000;

int n,m,st,ed,G[maxv][maxv],cost[maxv][maxv];
int d[maxv],c[maxv],pre[maxv];
bool vis[maxv]={false};

void Dijkstra(int s) 
{
	fill(d,d+maxv,inf);
	fill(c,c+maxv,inf);
	for(int i=0;i<n;i++) pre[i]=i;
	d[s]=0;
	c[s]=0;
	for(int i=0;i<n;i++)
	{
		int u=-1;
		int min=inf;
		for(int j=0;j<n;j++)
		{
			if(vis[j]==false&&d[j]<min)
			{
				u=j;
				min=d[j];				
			}
		}
		if(u==-1) return;
		vis[u]=true;
		for(int v=0;v<n;v++)
		{
			if(vis[v]==false&&G[u][v]!=inf)
			{
				if(d[u]+G[u][v]<d[v])
				{
					d[v]=d[u]+G[u][v];
					c[v]=c[u]+cost[u][v];
					pre[v]=u;
				}
				else if(d[u]+G[u][v]==d[v])
				{
					if(c[u]+cost[u][v]<c[v])
					{
						c[v]=c[u]+cost[u][v];
						pre[v]=u;
					}
				}
			}
		}		 
	}	
}

void DFS(int v)
{
	if(v==st)
	{
		printf("%d ",v);
		return;
	}
	DFS(pre[v]);
	printf("%d ",v);
}

int main()
{
	scanf("%d%d%d%d",&n,&m,&st,&ed);
	int u,v;
	fill(G[0],G[0]+maxv*maxv,inf);
	for(int i=0;i<m;i++)
	{
		scanf("%d%d",&u,&v);
		scanf("%d%d",&G[u][v],&cost[u][v]);
		G[v][u]=G[u][v];
		cost[v][u]=cost[u][v];
	}
	Dijkstra(st);
	DFS(ed);
	printf("%d %d\n",d[ed],c[ed]);
	return 0;
}

法2:Dijkstra+DFS

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

const int maxv=510;
const int inf=1000000000;

int n,m,st,ed,G[maxv][maxv],cost[maxv][maxv];
int d[maxv],minCost=inf;
bool vis[maxv]={false};
vector<int> pre[maxv];
vector<int> tempPath,path;

void Dijkstra(int s)
{
	fill(d,d+maxv,inf);
	d[s]=0;
	for(int i=0;i<n;i++)
	{
		int u=-1;
		int min=inf;
		for(int j=0;j<n;j++)
		{
			if(vis[j]==false&&d[j]<min)
			{
				u=j;
				min=d[j];
			}
		}
		if(u==-1) return;
		vis[u]=true;
		for(int v=0;v<n;v++)
		{
			if(vis[v]==false&&G[u][v]!=inf)
			{
				if(d[u]+G[u][v]<d[v])
				{
					d[v]=d[u]+G[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}
				else if(d[u]+G[u][v]==d[v])
				{
					pre[v].push_back(u);
				}
			}
		}
	}
} 

void DFS(int v)
{
	if(v==st)
	{
		tempPath.push_back(v);
		int tempCost=0;
		for(int i=tempPath.size()-1;i>0;i--)
		{
			int id=tempPath[i];
			int idNext=tempPath[i-1];
			tempCost+=cost[id][idNext]; 
		}
		if(tempCost<minCost)
		{
			minCost=tempCost;
			path=tempPath;
		}
		tempPath.pop_back();
		return;
	}
	tempPath.push_back(v);
	for(int i=0;i<pre[v].size();i++)
	{
		DFS(pre[v][i]);
	}
	tempPath.pop_back();
}

int main()
{
	scanf("%d%d%d%d",&n,&m,&st,&ed);
	int u,v;
	fill(G[0],G[0]+maxv*maxv,inf);
	//fill(cost[0],cost[0]+maxv*maxv,inf);
	for(int i=0;i<m;i++)
	{
		scanf("%d%d",&u,&v);
		scanf("%d%d",&G[u][v],&cost[u][v]);
		G[v][u]=G[u][v];
		cost[v][u]=cost[u][v];
	}
	Dijkstra(st);
	DFS(ed);
	for(int i=(path.size()-1);i>=0;i--)
	{
		printf("%d ",path[i]);
	}
	printf("%d %d\n",d[ed],minCost);
	return 0;	
}

 

EtherCAT主站SOEM -- 1 -- SOEM之ethercattype.h文件分析 “ethercattype.h” 文件用于支持 EtherCAT 网络通信,提供了各种类型定义和宏,以便开发者更轻松地开发 EtherCAT 相关应用程序。这些定义包括数据类型、命令类型、状态类型和错误类型,以支持 EtherCAT 通信。根据具体应用需求,还可以调整一些超时和帧长度设置。这些定义用于处理函数调用的结果,如 EC_NOFRAME(没有帧返回)、EC_ERROR(一般错误)、EC_TIMEOUT(请求超时)等。这些定义用于处理 EtherCAT 命令,例如 EC_CMD_APRD(自动递增读取 阅读详情

相关推荐

在Pycharm中配置Pyqt5工具(2023年新版教程)

在Pycharm中配置Pyqt5相关的三个工具:QtDesigner、PyUIC、PYRCC

网络技术和Python 2万+

PAT:图的最短路径

1003 求无向图中最短路径的条数和最大权重和思路:在dj算法的基础上,还要增加两个数组pathNum[]和sum[]。当更新最短路径时,if(dis[j]>(dis[u]+e[u][j])){ dis[j]=(dis[u]+e[u][j]);//正常更新最短路径 pathNum[j]=pathNum[u];//pathNum更新 sum[j]=sum[u]+num[j];/

jinzhao1993的博客 756

【计算机视觉学习03】全景图像拼接

文章目录一、基本原理1.1 图像拼接的几何原理1.2 直线,圆,复杂曲线方程拟合1.3 RANSAC算法1.4 APAP算法1.5 寻找最佳拼接缝(Seam Finding)1.5.1 最大流问题1.5.2 最大流问题的增广链算法1.5.3 最大流最小割定理二、 图像拼接具体实现2.1 代码2.2 运行结果 一、基本原理 将两幅或多幅具有重叠区域的图像,通过特征匹配将具有相同的特征点(SIFT特征点)的图像拼接在一起,将来自多个不同视角拍摄的图像变换到同一视角下,拼接成一张宽视野图像。 图像拼接基础流程

一个正在努力学习的iter 7790

最短路径 pat 1003

#include <iostream> //1003 using namespace std; const int N = 510; const int inf = 1e9; int n,m,st,ed; int vis[N]={0}; int G[N][N] = {0}; int w[N],weight[N]; int d[N]; int num[N]; void dij(int ...

BuGengFeng的博客 258

告别QQ截图!用C# Winform + 百度AI,自己动手做一个免费的文字提取小工具

本文详细介绍了如何利用C# Winform和百度AI OCR接口开发一款免费的文字提取工具,替代QQ截图功能。通过智能截图模块、OCR服务集成和主界面功能整合,实现高效准确的文字识别,适用于日常办公和学习场景。工具支持图像预处理、异步处理和错误日志记录,提升用户体验和识别准确率。

weixin_30566063的博客 380

发那科机器人抓手与物料搬运阀的配置与优化实践

本文详细介绍了发那科机器人抓手与物料搬运阀的配置与优化实践,包括基础配置指南、阀岛与输入模块的详细设置、测试程序编写与验证技巧,以及常见问题排查与性能优化建议。通过实际案例和经验分享,帮助技术人员快速掌握关键配置步骤,提升物料搬运系统的稳定性和效率。

weixin_33738555的博客 386

PAT-关于最短路径-Dijkstra+(DFS)

求单源或全局最短路径(无负边)-> 用Dijkstra: #include <iostream> const int inf = 999999999; cosnt int maxn = 500; int main(){ int e[maxn][maxn],dis[maxn]; fill(e[0],e[0]+maxn*maxn,inf); fill(...

valcanoshan的博客 508

海康威视工业相机网络配置实战:从MVS软件操作到IP地址规划

本文详细介绍了海康威视工业相机的网络配置实战,从MVS软件操作到IP地址规划。重点讲解了IP设置的重要性、Windows系统下的网络配置步骤、避免IP冲突的技巧,以及MVS软件中的相机IP配置方法。帮助用户快速掌握工业相机的网络连接技术,确保稳定高效的图像采集。

weixin_30296405的博客 486

PAT A1030:Travel Plan之Dijkstra求解

题目描述 1030 Travel Plan (30分) A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city

lilian的博客 201

YOLOv12中的A2C2f模块详解,涉及区域注意力模块(A2)和 R-ELAN 结构详解

区域注意力模块(Area Attention, A2)的设计出发点是为了解决传统注意力机制在计算复杂度和内存访问效率上的问题。传统的自注意力机制(Self-Attention)具有二次方的计算复杂度(O(n²)),尤其是在处理高分辨率图像时,计算成本非常高。此外,注意力机制的内存访问模式不如卷积神经网络高效,导致在实际应用中难以满足实时性要求。区域注意力模块通过将特征图划分为多个区域,并在每个区域内进行注意力计算,从而显著降低了计算复杂度。

Limiiiing的博客 5515

FPGA+CUYSB3014实现USB3.0功能

CYUSB3014是赛普拉斯在近几年推出的新一代USB3.0的外设控制器,可以解决USB2.0带宽限制,或者单独开发USB协议和驱动的难题。赛普拉斯将CYUSB3014简称为EZ-USB FX3,具有高度的灵活特性,开发人员只需要下载FX3的固件库,就能使用USB3.0的功能。目前在一些电子产品中,使用主控器加PHY芯片最流行的方式是用FPGA+FX3这种搭配来实现USB3.0接口。......

D_Katter的博客 7069

PAT 1030 Dijkstra算法 +DFS

1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to d...

alwaysik的博客 251

【1】Python交叉编译到OpenHarmony标准系统运行(arm32位)

本文介绍如何Python语言如何在OpenHarmony标准系统运行,包括5.0r和4.1r以及4.0r,和未来版本的OpenHarmony版本上。 Python语言在OpenHarmony上使用,需要将Python解释器CPython移植到OpenHarmony标准系统。通过交叉编译的方式。

weixin_58069108的博客 930

PAT甲级 1030 Travel Plan

PAT甲级 1030 Travel Plan 题目链接 A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city

旺崽的博客 2495

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

【PAT 甲级】1030 Travel Plan

堆优化的Dijkstra算法

AmiaYukiAchami的博客 770

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

【PAT】A1030 Travel Plan【Dijkstra算法】

A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path b...

elegant coder 739

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

PAT A 1030 Travel Plan

摘要:题目要求为旅行者寻找起点到终点的最短路径,若路径不唯一则选择花费最低的。使用Dijkstra算法,在计算最短距离的同时维护最小花费。代码实现中,通过邻接矩阵存储城市间距离和花费,使用优先队列优化路径选择,并记录前驱节点以输出完整路径。输入样例为4个城市和5条高速公路,输出为最短路径0→2→3,总距离3,总花费40。

whdehcy的博客 516

算法笔记---【PAT A1030】Travel Plan

题目链接:【PAT A1030】Travel Plan 题目描述 A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a trave...

、思考致富的博客 346

PAT A1030 Travel Plan (30分)

PAT A1030 Travel Plan (30分) 题目 原题链接 题目大意 给出城市之间的连接关系,并给出每条边的权重,计算输出从起点到终点最短路径与最少消费 大体思路 本题是典型的dijkstra+DFS模板题目,掌握了该模板此题没有什么难度 代码 #include<stdio.h> #include<algorithm> #include<vector&gt...

qq_41706836的博客 215

【PAT 1030】Travel Plan 最短路径Dijkstra

1030. Travel Plan (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the distances between c

天堂人间 1667

[C++] PAT 1030 Travel Plan (30分)

Sample Input: 4 5 0 3 0 1 1 20 1 3 2 30 0 3 4 10 0 2 2 20 2 3 1 20 Sample Output: 0 2 3 3 40 题解: 相对于普通的Dijsktra,将parent[]数组,变为了vector<int> pre[],这样就可以根据目标节点,递归遍历其所有的最短路径,最终找到价格最小的那条路径 #include <iostream> #include <queue> #include &lt.

快乐de约定的博客 2171

PAT Advanced 1030 Travel Plan(图,最短路径,Dijkstra算法)

链接:PAT Advanced 1030 A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to deci...

Plus Ultra! 248

【PAT算法之路】 -- 最短路径 1030 Travel Plan (30 分) C++解法

最短路径几乎是PAT出现频率最高的题型之一,一般都需要70-100行代码(C++),但是不用慌,因为这个代码有40行以上都是靠背下来的(都是套路)。 我个人记的是Dijkstra+DFS,这种方法感觉比较通用,而且容易记住。 我们用一个例子来看看吧,就比如: 1030 Travel Plan (30 分) 1030 Travel Plan (30 分) A traveler’s map gives...

qq_40515692的博客 515

Day57: [PAT甲级] 1030 Travel Plan (30分)

Day57: [PAT甲级] 1030 Travel Plan (30分)(双权Dij) 题源: 来自PAT真题库: https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 代码: dirty code凑合看吧 #include<iostream> #include<string...

qq2215459786的博客 1738

PAT(Advanced)1030 Travel Plan Dijkstra最短路 C++实现

PAT(Advanced)甲级1030 Travel Plan Dijkstra最短路 C++实现 相关内容 PAT(Advanced)1003 Emergency (Dijkstra最短路) (邻接矩阵)C++实现 DijkstraSSSP(单源最短路径求解)(迪克斯特拉算法堆优化)(Java实现)(邻接矩阵)(优先级队列)(堆优化) Dijkstra最短路(迪克斯特拉算法)(C++实现)(邻接矩阵) PAT(Advanced)1087 All Roads Lead to Rome Dijkstra最短路

qq_44486439的博客 542

[PAT-A 1030]Travel Plan

考察最短路径 Dijkstra算法 题目大意: 有N个城市,M条道路,并给出M条道路的距离属性以及花费属性(边权) 给定起点S与终点D,求起点到终点的最短路径,最短距离以及花费 如果有多条最短路径,选择花费最小的那条 思路: 考虑Dijkstra+DFS的方法[算法笔记]最短路径 Dijkstra算法 第一标尺即为最短路径 计算第二标尺,即边权最小的值,遍历所有的边,累加所有的边权,取最小的那一...

RegulusF的博客 264
上一篇: Dijkstra+DFS
下一篇: Bellman-Ford算法求最短路径
Estrellas_
博客等级 码龄9年 273粉丝 233原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值