1030 Travel Plan (30 point(s))

本文介绍了一道PAT竞赛题目“旅行计划”的解决方案,该题要求找出从起点到终点的最短路径,并在路径不唯一时选择成本最低的路径。文章详细解析了Dijkstra算法在解决此类问题中的应用,包括如何回溯路径、定义构造函数以及实现关键步骤。

1030 Travel Plan (30 point(s))

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

思路和1087 All Roads Lead to Rome基本一致,属于PAT常见题型。考点:Dijkstra算法灵活应用。

注意点:

1. DFS回溯路径的方法,深刻理解递归。

2. 注意构造函数定义。

#include<iostream>
#include<vector>
#include<cstring>
#define MAX 0x3f3f3f3f
using namespace std;
int N,M,S,D;
const int LEN = 505;
struct Edge{
	int next;int weight;int cost;//下一个结点、距离、花销 
	Edge(int n,int w,int c):next(n),weight(w),cost(c){	}
};
vector<Edge>graph [LEN];
int dis[LEN];//记录距离 
int sumCost[LEN];//当前总花销 
int last[LEN];//记录上一个城市 
bool visit[LEN]={false};
void dfs(int x){
	if(x==S){
		cout<<x;return;
	} 
	dfs(last[x]);
	cout<<" "<<x;
}
int main(void){
	cin>>N>>M>>S>>D;
	int a,b,w,c;
	for(int i=0;i<M;i++){
		cin>>a>>b>>w>>c;
		graph[a].push_back(Edge(b,w,c));
		graph[b].push_back(Edge(a,w,c));
	}
	memset(dis,-1,sizeof(dis));
	
	int newP = S;
	visit[newP] = true;
	dis[newP] = 0;
	while(!visit[D]){
		for(int i=0;i<graph[newP].size();i++){
			int v = graph[newP][i].next;
			int w = graph[newP][i].weight;
			int c = graph[newP][i].cost;
			if(visit[v]) continue;
			if(dis[v]==-1||dis[newP]+w<dis[v]){//不可达或者有更近的路径 
				dis[v] = dis[newP]+w;//更新距离 
				sumCost[v] = sumCost[newP]+c;//更新消耗
				last[v] = newP;//更新前驱城市 
			}
			else if(dis[newP]+w==dis[v]&&sumCost[newP]+c<sumCost[v]){//距离相等但是有更小的消耗 
				sumCost[v] = sumCost[newP]+c;//更新消耗
				last[v] = newP;//更新前驱城市 				
			} 
		}
		int min = MAX;
		for(int i=0;i<N;i++){
			if(dis[i]==-1) continue;
			if(visit[i]) continue;
			if(dis[i]<min){
				min = dis[i];
				newP = i;
			}
		} 
		visit[newP] = true;
	}
	dfs(D);
	cout<<" "<<dis[D]<<" "<<sumCost[D]<<endl;
	return 0;
}

 

<launch> <!-- size of map, change the size in x, y, z according to your application --> <arg name="map_size_x" value="40.0"/> <arg name="map_size_y" value="20.0"/> <arg name="map_size_z" value=" 5.0"/> <!-- topic of your odometry such as VIO or LIO --> <arg name="odom_topic" value="/state_ukf/odom" /> <!-- main algorithm params --> <include file="$(find plan_manage)/launch/kino_algorithm.xml"> <arg name="map_size_x_" value="$(arg map_size_x)"/> <arg name="map_size_y_" value="$(arg map_size_y)"/> <arg name="map_size_z_" value="$(arg map_size_z)"/> <arg name="odometry_topic" value="$(arg odom_topic)"/> <!-- camera pose: transform of camera frame in the world frame --> <!-- depth topic: depth image, 640x480 by default --> <!-- don't set cloud_topic if you already set these ones! --> <arg name="camera_pose_topic" value="/pcl_render_node/camera_pose"/> <arg name="depth_topic" value="/pcl_render_node/depth"/> <!-- topic of point cloud measurement, such as from LIDAR --> <!-- don't set camera pose and depth, if you already set this one! --> <arg name="cloud_topic" value="/pcl_render_node/cloud"/> <!-- intrinsic params of the depth camera --> <arg name="cx" value="321.04638671875"/> <arg name="cy" value="243.44969177246094"/> <arg name="fx" value="387.229248046875"/> <arg name="fy" value="387.229248046875"/> <!-- maximum velocity and acceleration the drone will reach --> <arg name="max_vel" value="3.0" /> <arg name="max_acc" value="2.0" /> <!-- 1: use 2D Nav Goal to select goal --> <!-- 2: use global waypoints below --> <arg name="flight_type" value="1" /> <!-- global waypoints --> <!-- If flight_type is set to 2, the drone will travel these waypoints one by one --> <arg name="point_num" value="2" /> <arg name="point0_x" value="19.0" /> <arg name="point0_y" value="0.0" /> <arg name="point0_z" value="1.0" /> <!-- set more waypoints if you need --> <arg name="point1_x" value="-19.0" /> <arg name="point1_y" value="0.0" /> <arg name="point1_z" value="1.0" /> <arg name="point2_x" value="0.0" /> <arg name="point2_y" value="19.0" /> <arg name="point2_z" value="1.0" /> </include> <!-- trajectory server --> <node pkg="plan_manage" name="traj_server" type="traj_server" output="screen"> <remap from="/position_cmd" to="planning/pos_cmd"/> <remap from="/odom_world" to="$(arg odom_topic)"/> <param name="traj_server/time_forward" value="1.5" type="double"/> </node> <node pkg="waypoint_generator" name="waypoint_generator" type="waypoint_generator" output="screen"> <remap from="~odom" to="$(arg odom_topic)"/> <remap from="~goal" to="/move_base_simple/goal"/> <remap from="~traj_start_trigger" to="/traj_start_trigger" /> <param name="waypoint_type" value="manual-lonely-waypoint"/> </node> <!-- use simulator --> <include file="$(find plan_manage)/launch/simulator.xml"> <arg name="map_size_x_" value="$(arg map_size_x)"/> <arg name="map_size_y_" value="$(arg map_size_y)"/> <arg name="map_size_z_" value="$(arg map_size_z)"/> <arg name="c_num" value="10"/> <arg name="p_num" value="150"/> <arg name="odometry_topic" value="$(arg odom_topic)" /> </include> </launch>详细解释一下并说明和上个文件的不同
最新发布
08-30
这段 `.launch` 文件与上一个文件在整体结构和功能上非常相似,但存在一些**细节上的差异**,主要体现在**参数设置、注释说明、以及部配置的值**上。下面是**详细解释**以及**与上个文件的不同之处**。 --- ### 🔍 **整体功能概述** 该 `.launch` 文件用于启动一个基于ROS的无人机路径规划系统。它包括以下几个主要部: 1. **地图尺寸设置** 2. **里程计话题配置** 3. **主算法参数配置(包含在 `kino_algorithm.xml`)** 4. **轨迹服务器节点** 5. **航路点生成器节点** 6. **模拟器配置(包含在 `simulator.xml`)** --- ## 📌 逐段解析 ### 1. 地图尺寸参数 ```xml <arg name="map_size_x" value="40.0"/> <arg name="map_size_y" value="20.0"/> <arg name="map_size_z" value=" 5.0"/> ``` - 定义了地图的三维范围。 - 与上个文件基本一致,只是 `map_size_z` 的值前面多了一个空格(不影响功能)。 ### 2. 里程计话题 ```xml <arg name="odom_topic" value="/state_ukf/odom" /> ``` - 使用的是 UKF 估计的里程计数据。 - 与上个文件完全一致。 ### 3. 主算法参数(kino_algorithm.xml) ```xml <include file="$(find plan_manage)/launch/kino_algorithm.xml"> ``` - 引入主路径规划算法的参数配置。 - 与上个文件相比: - 注释略有不同,强调了不能同时设置 `cloud_topic` 和 `camera_pose_topic` / `depth_topic`。 - 参数值基本一致,如相机内参、最大速度、加速度等。 ### 4. 轨迹服务器节点 ```xml <node pkg="plan_manage" name="traj_server" type="traj_server" output="screen"> <remap from="/position_cmd" to="planning/pos_cmd"/> <remap from="/odom_world" to="$(arg odom_topic)"/> <param name="traj_server/time_forward" value="1.5" type="double"/> </node> ``` - 功能一致,参数一致。 ### 5. 航路点生成器节点 ```xml <node pkg="waypoint_generator" name="waypoint_generator" type="waypoint_generator" output="screen"> <remap from="~odom" to="$(arg odom_topic)"/> <remap from="~goal" to="/move_base_simple/goal"/> <remap from="~traj_start_trigger" to="/traj_start_trigger" /> <param name="waypoint_type" value="manual-lonely-waypoint"/> </node> ``` - 功能一致,参数一致。 ### 6. 模拟器配置 ```xml <include file="$(find plan_manage)/launch/simulator.xml"> <arg name="map_size_x_" value="$(arg map_size_x)"/> <arg name="map_size_y_" value="$(arg map_size_y)"/> <arg name="map_size_z_" value="$(arg map_size_z)"/> <arg name="c_num" value="10"/> <arg name="p_num" value="150"/> <arg name="odometry_topic" value="$(arg odom_topic)" /> </include> ``` - 与上个文件相比: - `c_num`(障碍物数量)和 `p_num`(点云点数)保持一致。 - 没有明显功能差异。 --- ## 🔁 **与上个文件的不同点总结** | 比较项 | 当前文件 | 上一个文件 | 说明 | |--------|----------|------------|------| | `map_size_z` 值 | `" 5.0"`(前面有空格) | `"5.0"` | 空格不影响功能 | | 注释说明 | 更加详细地说明了不能同时设置 `cloud_topic` 和相机参数 | 说明较少 | 当前文件更具指导性 | | 参数值 | 完全一致 | 完全一致 | 包括相机内参、最大速度、加速度等 | | 航路点数量 | `point_num=2` | `point_num=2` | 一致 | | 航路点坐标 | 一致 | 一致 | point0~point2 坐标相同 | | 子模块文件 | 引用了相同的 `kino_algorithm.xml` 和 `simulator.xml` | 同上 | 模块化结构一致 | --- ## ✅ 总结 当前 `.launch` 文件与上一个文件在**功能结构和参数配置上基本一致**,主要差异体现在: - 注释更详细,对用户有更强的指导性; - `map_size_z` 的值多了空格,不影响实际运行; - 没有新增功能模块或参数。 这说明两个文件可能是同一项目的**不同版本或配置变体**,当前文件更注重**可读性和配置说明**。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值