PAT甲级 - 1030 Travel Plan (30 分)

vcs覆盖率相关 选择要查看的覆盖率点,右键选择 show xxx tests; 阅读详情

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
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 迪杰斯特拉算法首先得到最短路径值和前驱节点(最短路径),相当于先算出第一标尺。

然后使用DFS计算第二标尺。

BUG整理

  1. 对于dis和cost初始化为INF,不是0!没有通路,则初始化为无限大
  2. struct的定义 struct{}; 分号一定要有
  3. 定义一个变量的时候,如果不主动赋值,一定要初始化
  4. Dijstra,输入是起点;DFS输入是终点(因为是pre[]存储,从后向前DFS)
  5. 多层嵌套循环时,不要都用i,造成干扰;找最小的数,不仅要小,而且要没访问过
  6. 无向图要赋值两次,有向图只需要复制一次
  7. if分支的时候,if...if是错的!!!会顺序执行。使用if..else或者if...else if,
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn = 510;
const int INF = 1E9;

struct node {
	int dis, cost;
	node(int _dis = INF, int _cost = INF) : dis(_dis), cost(_cost) {}//dis想要最小值,cost想要最小值,默认初始化为无限大!!!!不要为0!!
};
node G[maxn][maxn];
int vis[maxn] = { false };
int dis[maxn];
vector<int> pre[maxn];
vector<int> path;
vector<int> temp_path;
int N, M, S, D;
int minCost = INF;

int computeCost() {
	int now, pre, cost = 0;
	pre = temp_path[0];
	for (int i = 1; i < temp_path.size(); i++)
	{
		now = temp_path[i];
		cost += G[pre][now].cost;
		pre = now;
	}
	return cost;
}

void DFS(int front) {
	if (front == S) {//到达叶节点
		temp_path.push_back(front);
		int cost = computeCost();
		if (cost < minCost)
		{
			minCost = cost;
			path = temp_path;
		}
		temp_path.pop_back();
                return;
	}
	temp_path.push_back(front);
	for (int i = 0; i < pre[front].size(); i++) {
		DFS(pre[front][i]);
	}
	temp_path.pop_back();
}



void Dijstra(int s) {
	fill(dis, dis + maxn, INF);
	dis[s] = 0;
	for (int i = 0; i < N; i++) {
		int u = -1, MIN = INF;
		for (int j = 0; j < N; j++) { //i不要重用
			if (vis[j] == false && dis[j] < MIN) {
				u = j;
				MIN = dis[j];
			}
		}
		if (u == -1) return;
		vis[u] = true;//使用u为跳板来更新v的距离
		for (int v = 0; v < N; v++) {
			if (vis[v] == false && G[u][v].dis != INF) {
				if (dis[u] + G[u][v].dis < dis[v]) {
					dis[v] = dis[u] + G[u][v].dis;
					pre[v].clear();
					pre[v].push_back(u);
				}
				else if (dis[u] + G[u][v].dis == dis[v]) { //else if!!!!!
					pre[v].push_back(u);
				}
			}
		}
	}
}


int main() {
	cin >> N >> M >> S >> D;
	//node Q;
	//fill(G, G + maxn * maxn, Q);
	for (int i = 0; i < M; i++)
	{
		int m, n;
		cin >> m >> n;
		cin >> G[m][n].dis >> G[m][n].cost;
		G[n][m].dis = G[m][n].dis;
		G[n][m].cost = G[m][n].cost;
	}

	Dijstra(S);
	DFS(D);
	for (int i = path.size() - 1; i >= 0; i--) {
		cout << path[i] << " ";
	}
	cout << dis[D] << " " << minCost;
}

 

【PADS-开发指南】 馒头破戒大师 阅读详情

相关推荐

告别命令行恐惧!用nTopology可视化工具5分钟搞定三维Voronoi泡沫建模

本文介绍如何利用nTopology可视化工具快速生成三维Voronoi泡沫结构,无需编程即可完成复杂建模。Voronoi结构在轻量化设计、能量吸收和生物医学等领域具有重要应用,传统命令行方法门槛较高。通过nTopology的四步流程,用户可在5分钟内创建仿真就绪模型,大幅提升设计效率。

weixin_30764771的博客 427

PAT甲级1030 Travel Plan (30 分)题解

\quad这个题是一道最短路题,跟PAT甲级1003 Emergency (25 分)题解很像,即在求最短路的前提下,若有多条最短路,输出花费最少的一条。PAT最爱考这种题,因为花费最少满足最优子结构,我们可以直接在Dijistra中进行更新。程序如下: #include <iostream> #include <vector> #include <map> ...

qq_40438165的博客 1519

智能体在车联网中的应用:第9天 核心工具链与仿真世界:SUMO交通仿真入门——从安装到构建你的第一个虚拟十字路口

摘要 SUMO是一款开源微观交通仿真工具,为车联网和自动驾驶算法开发提供高效安全的测试环境。文章详细介绍了SUMO的核心概念、安装方法及实战应用。在Ubuntu或Windows系统安装SUMO后,可通过netedit工具构建十字路口路网,包括创建节点、连接边、设置交通信号灯等步骤。随后通过编写.rou.xml文件定义车辆类型、路线和车流,实现动态交通场景模拟。SUMO支持与外部程序交互,能复现真实场景、进行极端测试和算法验证,是车联网研究的理想数字沙盘。

IT深耕十余载,大道之简 1454

PAT甲级-1030 Travel Plan (30分)

点击链接PAT甲级-AC全解汇总 题目: 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 decid...

卷子的博客 325

Python驱动COMSOL自动化:从数据生成到DNN代理模型部署全流程解析

本文详细解析了如何利用Python驱动COMSOL实现全流程自动化,从参数化建模、批量仿真数据生成,到训练深度神经网络(DNN)代理模型并部署应用。该方案通过自动化闭环解放工程师生产力,并利用代理模型实现秒级性能预测,极大提升了多物理场仿真优化与设计探索的效率。

weixin_29041767的博客 212

从婴儿学步到工业级应用:VLA的‘隐式标定‘原理通俗图解

本文深入解析VLA(Vision-Language-Action)模型的'隐式标定'原理,通过类比婴儿学习机制,揭示其如何替代传统手眼标定技术。文章对比了工业机器人传统标定与VLA数据驱动方法的差异,并可视化神经网络注意力机制,展示其在空间关系编码上的突破性进展。最后探讨了VLA在工业应用中的优势与混合架构解决方案。

pz890123456的博客 324

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

论文阅读 AIED 2024 Coding with AI: How Are Tools Like ChatGPT Being Used by Students in Foundational Pro

该论文题为《Coding with AI: How Are Tools Like ChatGPT Being Used by Students in Foundational Programming Courses》,由美国犹他州立大学的 Aashish Ghimire 与 John Edwards 共同撰写,发表于人工智能与教育领域顶级会议。

CSPhD-winston的博客 309

PAT甲级 1030 Travel Plan (30分)

1030 Travel Plan (30分) 题目链接:PAT A 1030 题目大意:题目第一行输入给出顶点数,边数,起点和终点,随后几行给出每条边的距离和通过每条边的开销,要求输出最短路径所经过的点以及这条路径的总距离以及总开销。如果有多条同样距离的路径,则输出花销最少的那条路径~ 思路分析:很简单的一道dijkstra算法模板题,只要熟练掌握dijkstra算法就可以解出。有两种解法,一种...

weixin_43684054的博客 527

抖音无水印视频爬虫实战:从抓包分析到批量下载指定用户作品

抖音作为当前最受欢迎的短视频平台,拥有海量的优质内容。很多用户希望备份自己或他人的作品,但抖音官方并未提供批量下载功能,且直接下载的视频带有平台水印。本文将带领大家从零开始,通过抓包分析、设备模拟、请求签名等核心技术,实现一个完整的抖音无水印视频爬虫系统。本文仅供技术学习和个人备份使用,请勿将爬取的内容用于商业用途或侵犯他人版权,爬取频率请控制合理范围内。目录一、前言二、技术栈概览三、准备工作3.1 环境搭建3.2 抓包分析步骤3.3 关键API发现四、核心技术解密4.1 X-Gorgon签名算法。

2201_76125261的博客 846

使用Python爬虫抓取政府网站政务信息:公告、通知的自动化抓取与分析

通过结合Requests、BeautifulSoup、Selenium等技术,你能够高效地抓取政府网站的数据,并通过MySQL存储与Pandas分析这些数据。通过自动化手段抓取这些信息,不仅可以提高数据收集的效率,还能够实时获取最新的政务动态,为政策研究、舆情分析等提供宝贵数据。我们将通过Python的Requests、BeautifulSoup、Selenium等工具,结合最新的技术实现一个完整的爬虫系统,帮助你轻松抓取并分析政务信息。通过爬虫,我们可以自动化地从指定网站上抓取公告、新闻、通知等信息。

2201_76125261的博客 1907

PAT甲级1030 Travel Plan (30 分)

1030Travel 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...

Dexter 296

【Ranger】mac下Apache Ranger编译安装

a、编译过程中出现测试用例不通过和文件认证错误错误:带上参数-DskipTests=true -Drat.skip=true 重新编译;b、若编译过程中出现缺少jar包的情况,可能下载不了,需要手动自动添加;c、编译所需的其他工具如maven等安装配置略。采用这个会报错,因此我才用了下面一个命令。初始化设置Ranger Admin:执行。然后进入target目录。

九师兄 2107

PAT甲级1030 Travel Plan (30分)|C++实现

一、题目描述 原题链接 Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. No

weixin_42393947的博客 207

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

PAT甲级1030 Travel Plan (30分)

题解: 题目大意是告诉你每个城市间的距离和开销,让你求出从起点到终点的最短距离,然后如果距离相同的话要最小开销,最后输出这条最短的路径、最小距离和最小开销 思路: finalmin记录最后最短路,curpath记录当前路径,cost和len分别记录两两城市间的开销和距离,visit是访问标记 dfs从起点开始深搜遍历所有可能,到达终点后判断是否是最短距离 1.小于最短距离,更新数据 2.等于最短距...

Artorias的博客 190

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

PAT 甲级 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 b...

qq_39564678的博客 196

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

【PAT 甲级】A1030 Travel Plan (30分)

mmp,又一次因为笔误浪费了我一下午的时间!!!!!!!气死爹了 我说怎么一直段错误和内存超限轮番攻陷,原来是我赋值的地方写错了 真是无语。。 核心:Dijkstra #include <bits/stdc++.h> using namespace std; const int INF=0x3ffffff; int g[510][510],path[510],pre[510],cost...

weixin_42278063的博客 328

PAT 甲级 1030 Travel Plan (30分)

题目 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 a

qq_41001689的博客 122

1030 Travel Plan (30 分)--PAT甲级

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

laplacebh的博客 192

【PAT甲级】1030 Travel Plan (30分)

解题过程的小记录,如有错误欢迎指出。 难度:三星(Dijkstra+DFS模板题,题不难,奈何我手生orz) 小导航~题目分析注意点我的解题过程思路bug代码dalao的代码借鉴点 题目分析 给出各城市之间的距离和路过需要的花费,找出给定的起始点和终点的花费最少的最短路径,要求输出路径所经过的城市、最短距离和花费 注意点 采用邻接矩阵保存数据的话,距离和花费都要双重赋值,即G[u][v]=G[v][u] 我的解题过程 思路 先通过Dijkstra函数找出给定起点的单源最短路径,并在函数中用变长数组保存

SpicyChicken的自我修养 189

PAT 1030 Travel Plan (30 分) -甲级

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

qq_39071488的博客 190

PAT-甲级-1030 Travel Plan (30 分)

1030Travel 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...

valcanoshan的博客 243

【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费。求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费。 trick: 找了半小时bug终于发现是给dis数组赋初值时范围是1~N,这道题点是从0~N-1的,故初次只通过了第0组数据,初始化改一下边界即可AC。 代码: #define...

weixin_30919571的博客 196

1030 Travel Plan (30 分)-PAT甲级

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 destina

神笔码农.的博客 265

PAT甲级 -- 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 b...

猪の成长日记 221

PAT甲级刷题记录——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 b...

一只奋斗中的本科生 515
上一篇: PAT甲级 - 1003 Emergency (25 分) Dijstra和BF算法
下一篇: 字符串Hash
DedicateToAI
博客等级 码龄9年 6粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值