2016-PTA初赛-L3-1 天梯地图(dijkstra模板)

news/2025/11/26 1:30:47/文章来源:https://www.cnblogs.com/Yuhhhhh/p/19270589

image

思路

dijkstra模板题,不需要小根堆优化,这题的第二个样例:路径一样则合并输出,怎么判断路径一样?会想到C++的vector对==进行了重写(即每个对应位置的元素一样)。

AcCode:

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
int N, M;
//最后一维表示time或distance,graph存图,dist存最短距,lst存上个节点
int graph[510][510][2], dist[510][2], lst[510][2];
//两种路径
vector<int> path[2];
//dijkstra模板
int dj(int st, int be, int end){dist[be][st] = 0;bool vis[510];lst[be][st] = -1;memset(vis, false, sizeof(vis));for(int i = 0; i < N; i++){int cur, mn = 0x7fffffff;for(int i = 0; i < N; i++) if(dist[i][st] < mn && !vis[i]) mn = dist[i][st], cur = i; for(int i = 0; i < N; i++){if(graph[cur][i][st] && dist[cur][st] + graph[cur][i][st] < dist[i][st] && !vis[i]){dist[i][st] = dist[cur][st] + graph[cur][i][st];lst[i][st] = cur;}}vis[cur] = true;}// for(int i = 0; i < N; i++) cout << lst[i][st] << "-";// cout << endl;return dist[end][st];
}
//递归的获取路径
void getPath(int st, int end){if(end == -1) return;getPath(st, lst[end][st]);path[st].push_back(end);
}int main(){
//初始化dist数组为大值,并不知道time和lenth取值范围,题目压根没写,我猜的。memset(dist, 0x2f, sizeof(dist));cin >> N >> M;
//输入数据while(M--){int v1, v2, ow, len, time; cin >> v1 >> v2 >> ow >> len >> time;graph[v1][v2][0] = time;graph[v1][v2][1] = len;if(ow != 1) graph[v2][v1][0] = time, graph[v2][v1][1] = len;}
//调用函数输出答案即可int be, end; cin >> be >> end;int ans[2];for(int i = 0; i < 2; i++){ans[i] = dj(i, be, end);getPath(i, end);}if(path[1] != path[0]){cout << "Time = " << ans[0] << ": ";for(int i = 0; i < path[0].size(); i++){cout << path[0][i];if(i != path[0].size() - 1) cout << " => ";}cout << endl << "Distance = " << ans[1] << ": ";for(int i = 0; i < path[1].size(); i++){cout << path[1][i];if(i != path[1].size() - 1) cout << " => ";}}else{cout << "Time = " << ans[0] << "; " << "Distance = " << ans[1] << ": ";for(int i = 0; i < path[0].size(); i++){cout << path[0][i];if(i != path[0].size() - 1) cout << " => ";}}return 0;
}

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

KEYDIY Toyota 8A (BA) 4A All-Lost Adapter Cable: Simplify Key Replacement for Mechanics Car Owners

When All Keys Are Lost: The Toyota Key Programming Challenge For European and American automotive repair professionals and car owners, the scenario of losing all keys to a Toyota can be frustrating and…

PyCodeObject

2025.11.26 1.python是用C语言写成的,根据名字空间特性,以下代码经过python编译器编译后,一共得到()个PyCodeObject对象class A:pass def Fun():pass a = A() Fun()Python编译器在编译代码时,每个独立的作用域(…

python拷贝

2025.11.26 1.当我们使用下面的操作的时候,会产生浅拷贝的效果:使用切片 [:] 操作使用工厂函数(如list/dir/set)使用copy模块中的copy()函数 2.拷贝的特殊情况 对于非容器类型(如数字、字符串、和其他’原子’类…

KEYDIY KD ZB02-4 Universal Smart Remote Key 3+1 Buttons for BMW - 5pcs/lot

Solving BMW Smart Key Woes: The KEYDIY KD ZB02-4 Universal Smart Remote Key Problem: The High Cost of BMW Smart Key Replacements For European and American BMW owners, a lost or malfunctioning smart key…

Universal 3-Button Smart Remote Key for BMW - KEYDIY KD ZB02-3 (5pcs/lot)

Problem: The Frustration of BMW Smart Key Replacements For European and American BMW owners and mechanics, finding a reliable, affordable smart remote key can feel like a challenge. Dealerships often c…

列表,元组,字典,集合笔记

列表,元组,字典,集合笔记列表 定义:列表是处理一组有序项目的数据结构 格式:列表名 = [元素1,元素2,元素3,元素4....] 一个列表中的数据类型可以各不相同 列表相关操作 添加 append  整体添加 extend  分散…

sam3 (3)匹配mask - MKT

sam3 (3)匹配mask 1 -1平移视角差异 1-2对应的还有大小 1-3 旋转 2 残缺部分不全

Educational Codeforces Round 184 (Div. 2)

A. Alice and Bob根据题目要求,Bob要想得分最大化就是要使Bob能够得分的球尽可能得多。 通过 lower_bound 函数和 upper_bound(这是因为题目中说平局也就是数组中的元素与a一样大是使Alice得分,我们要避免与a相同)…

KEYDIY KD NB08-4 3+1 Button Universal Flip Remote Key for Volkswagen – 5pcs/lot

Solving Volkswagen Remote Key Woes: The KEYDIY KD NB08-4 Universal Flip Remote Key For European and American Volkswagen owners and automotive repair professionals, the struggle to find reliable, afford…

C++学习日志——蓝桥杯课程总结_基础篇/2025.11.26

C++课程学习记录——递归递归 概念: 函数直接或间接调用自身的过程 两个关键要素 1.基本情况(递归终止条件):递归函数中的一个条件,当满足该条件时递归终止,避免无限递归。[直接解决极小规模问题的方法] 2.递归表…

KEYDIY KD NB104 4-Button Universal Remote Key (5pcs) – Reliable Replacement for Euro/American Cars

The Remote Key Problem: Costly Delays and Compatibility Headaches For European and American automotive repair shops, nothing derails efficiency faster than tracking down OEM remote keys that cost custo…

在 linux 操作系统中,使用 vim 打开一个文本文件时,出现中文乱码的原因和解决方法

在 linux 操作系统中,使用 vim 打开一个文本文件时,出现中文乱码的原因和解决方法问题场景: 例如:使用 vim 打开一个文件后,中文是乱码乱码原因: 文件的编码格式 和 vim 用于解析文件内容的编码格式不一致,导致…

【实验报告】sglang,vllm,transformers 在强制串行推理场景下的表现

【实验报告】sglang,vllm,transformers 在强制串行推理场景下的表现我们现在考虑若干强制串行的需求。也就是说,必须推理完这个之后再推理下一个。调包范围是 transformers,vllm,sglang投机采样/不使用投机采样。 投…

实验3类和对象

实验任务1 源代码如下: button.hpp#pragma once#include <iostream> #include <string>class Button { public:Button(const std::string &label_);const std::string& get_label() const;void c…

what is A

ai. "Amor vincit omnia" ὁ θεὸς ἀγαπᾷ τὸν κόσμοv from ancient Greek to Roman, even in the ancient Chinese. not lo.

夺命雷公狗—好用的截图工具分享

夺命雷公狗—好用的截图工具分享今天在工作 由于环境没有网络 但是又非常想截图~ ~! 无意间发现了一款非常不错的截图工具 pixpin ~ ~! 真心好用 暴赞 ! 而且还可以截长图,下载地址我就不分享了~ ~! 文档地址:…

Windows给文件夹别名

Windows 和 macOS 采用了不同的技术来实现多语言显示,思路与 macOS 的 .localized 机制非常相似。macOS 的方式:.localized 文件夹 正如您所说,macOS 使用一种非常直观的文件系统层面的本地化方案:你创建一个名为 …

2025 完整 AI 模型核心用法速查表 - 智慧园区

一、通用大模型(全能顶尖)GPT-4o Ultra(OpenAI)最擅长:多模态实时交互(语音+图像+文本)、复杂Excel函数嵌套生成,错误率<1.5%,通用能力天花板。 Gemini 2.5 Pro(谷歌)最擅长:草图转可交互3D模型、百万tok…

实验 3

task 1 Button.hpp1 #pragma once2 3 #include<iostream>4 #include<string>5 6 class Button {7 public:8 Button(const std::string& label_);9 const std::string& get_label() const;…

实验03

实验一 button.hpp#pragma once #include<iostream> #include<string>class Button{ public:Button(const std::string &label_);const std::string &get_label() const;void click();private:std…