PAT (Advanced Level) Practise 1111. Online Map (30) Dijstra单源最短路

《iOS之一起进大厂》系列-秒杀面试官的RunLoop底层原理面试题 Runloop是iOS面试中的必考题,不管大厂小厂都会问到。 这里包括了Runloop几乎所有的面试题,还有中肯完整的答案解释。 阅读详情

1111. Online Map (30)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2 <= N <= 500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N-1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not;length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:
10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5
Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5
Sample Input 2:
7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5
Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5


#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int maxn = 500 + 10, INF = 0x3f3f3f3f;
int N, M, s, e;
int dis[maxn][maxn], tim[maxn][maxn], cnt[maxn][maxn];
int pd[maxn], pt[maxn], dd[maxn], dt[maxn];
bool vis[maxn];

//dijstra(dis, tim, dd, pd);
//dijstra(tim, cnt, dt, pt);
void dijstra(int map1[][maxn], int map2[][maxn], int *d1, int *p) {
	int d2[maxn];
	for (int i = 0; i < N; i++) {
		d1[i] = INF;
		d2[i] = INF;
	}
	memset(vis, false, sizeof(vis));
	for (int i = 0; i < N; i++) {
		d1[i] = map1[s][i];
		d2[i] = map2[s][i];
	}
	d1[s] = 0;
	p[s] = s;
	vis[s] = true;
	for (int t = 1; t <= N - 1; t++) {
		int MIN = INF, min;
		for (int i = 0; i < N; i++) {
			if (!vis[i]) {
				if (d1[i] < MIN) {
					min = i;
					MIN = d1[i];
				}
			}
		}
		vis[min] = true; //不要忘记标记
		for (int i = 0; i < N; i++) {
			if (!vis[i]) {
				if (d1[min] + map1[min][i] < d1[i]) {
					d1[i] = d1[min] + map1[min][i];
					d2[i] = d2[min] + map2[min][i];
					p[i] = min;
				}
				else if (d1[min] + map1[min][i] == d1[i] && d2[min] + map2[min][i] < d2[i]) {
					d2[i] = d2[min] + map2[min][i];
					p[i] = min;
				}
			}
		}
	}
}

void output(int *a) {
	int out[maxn], len = 0;
	int j = e;
	while (j != s) {
		out[len++] = j;
		j = a[j];
	}
	out[len++] = j;
	for (int i = len - 1; i >= 0; i--) {
		if (i != 0) {
			printf("%d -> ", out[i]);
		}
		else {
			printf("%d\n", out[i]);
		}
	}
}

int main()
{
	scanf("%d%d", &N, &M);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (i != j)
				dis[i][j] = tim[i][j] = INF;
			else
				dis[i][j] = tim[i][j] = 0;
		}
	}
	for (int t = 0; t < M; t++) {
		int v1, v2, one, len, time;
		scanf("%d%d%d%d%d", &v1, &v2, &one, &len, &time);
		dis[v1][v2] = len;
		tim[v1][v2] = time;
		if (!one) {
			dis[v2][v1] = len;
			tim[v2][v1] = time;
		}
	}
	scanf("%d%d", &s, &e);

	for (int i = 0; i < N; i++) {
		pd[i] = s;
	}
	dijstra(dis, tim, dd, pd);

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cnt[i][j] = 1;
		}
	}

	for (int i = 0; i < N; i++) {
		pt[i] = s;
	}
	dijstra(tim, cnt, dt, pt);
	bool flag = true;
	for (int i = 0; i < N; i++) {
		if (pd[i] != pt[i]) {
			flag = false;
			break;
		}
	}
	if (flag) {
		printf("Distance = %d; Time = %d: ", dd[e], dt[e]);
		output(pd);
	}
	else {
		printf("Distance = %d: ", dd[e]);
		output(pd);
		printf("Time = %d: ", dt[e]);
		output(pt);
	}
	return 0;
}




ESP32-CAM 赋能 AI 视觉巡检小车:自主避障与目标追踪实现全解析 本文基于 ESP32-CAM 实现了低成本 AI 视觉巡检小车的核心功能,硬件成本<100 元,兼顾自主避障与目标追踪。算法升级:移植 YOLOv5nano 量化模型(INT8),提升目标检测精度(如识别巡检场景中的 “异常物体”“人员”);无线拓展:通过 MQTT 协议将巡检图像、距离数据上传到阿里云 / 腾讯云,实现远程监控;多传感器融合:增加温湿度(DHT11)、红外测温(MLX90614)模块,扩展巡检维度;路径规划:结合 SLAM 算法,实现自主建图与路径巡检。 阅读详情

相关推荐

【SAP报错】BK128&K5112

在ECC中使用CO1F进行Progressive confirmation时,依次遇到了如下的报错。

weixin_45228526的博客 793

PAT (Advanced Level) Practise 1111 Online Map (30)

1111. Online Map (30) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Input our current position and a destination, a

Occult 1139

从SRIOV到SIOV:PCIe虚拟化技术下一代革命

如果说SR-IOV是过去十年PCIe虚拟化的“标杆方案”,那SIOV就是为超大规模云数据中心、AI集群量身打造的“破局者”——它解决了SR-IOV难以逾越的扩展性瓶颈,同时平衡了性能、成本与生态兼容性。今天就从“为什么需要SIOV”到“SIOV到底强在哪”,再到“未来要解决什么问题”,做一次全方位深度拆解。

存储随笔 2585

PAT甲级 1111. Online Map (30)

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is g...

moffy_abo的博客 1383

mysql9.0windows安装

将这里的位置修改为你刚刚创建并复制data目录的地址即可,win11的系统使用vscode修改之后,会让你确认开启更高权限以进行保存,比较方便,window10不需要确认,修改后可以直接保存。:在修改之前,最好将下图文件进行备份,将原始的文件保存好一份,以防更改了配置之后,导致mysql服务报错,无法启动,备份好的话直接将有问题的配置文件给替换就可以,无需重新安装一次mySql。双击下载的msi文件进行安装。安装有问题的,可以直接进行卸载,新本的mySql会将注册表中的数据自动进行清除,还是很方便的。

m0_74824123的博客 6462

Doris Streamloader安装教程

Doris Streamloader 是一款用于将数据导入 Doris 数据库的专用客户端工具。本文介绍其在centos7环境下的安装过程

qq_44540985的博客 1097

PAT甲级 1111 Online Map(最短路径的应用)

题目链接:点击打开链接思路:写的比较繁琐,利用dijkstra算法分别去求最短路径和最快路径,再按照要求输出即可。还写了一个搜索的版本,由于剪枝函数不够优化,复杂度过高,最后一个测试点超时,27分。#include &lt;cstdio&gt; #include &lt;cstring&gt; #include &lt;iostream&gt; #include &lt;vector&gt; #i...

不忘初心 2060

深度解读:车辆诊断之DTC的状态位

搞车辆诊断测试的,离不开对DTC的状态位的测试DTC的状态位是8个bit,每个bit代表不同的意思,每个bit置1或者置0都要满足一定的条件初学者,很容易被这个搞得头很大今天我们就来详细拆解一下,扫除你心中的疑惑我们以欠压故障为例进行说明,假设欠压故障(DTC code为EE0468)发生的条件如下:1. 车辆模式(CarModSts1)处于Normal(0)或者Dyno(5)2. 当电源可用性不受限制时,即ElPowerLevel (有和PwrLvlElecSubtyp组成。

帮助100万人颠覆负面潜意识,实现人生蜕变,培养1万名潜意识开发导师 1万+

solution Of Pat 1111. Online Map (30)

1111. Online Map (30)Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is

风先生的日常专栏 2403

揭秘 Chat Template 如何驱动 AI Agent?以Qwen3为例详解LLM的聊天模板

bos_token: null (没有使用特殊的文档开始符)eos_token<|im_end|>(作为每个对话回合的结束符)pad_token: 对话回合开始<|im_end|>: 对话回合结束非特殊但关键的控制 Token (在词汇表中定义): 包裹模型生成的工具调用请求。: 包裹外部工具返回的执行结果。<think></think>: 包裹模型的内部思考或推理过程。

ljp1919的专栏 1365

深度学习中的编码器-解码器架构

解码的作用:转换为人类可理解的形式,解码器的任务是将编码器生成的中间表示(上下文向量)转换回人类可理解的输出形式(如翻译后的文本、生成的图像描述等)。解码器通过逐步生成输出序列,使得最终的输出符合人类的理解和需求。编码的作用:转换为机器可理解的形式,编码器将输入数据(如文本、图像等)转换为一种机器更容易处理和理解的形式,即上下文向量或编码。在深度学习中,编码器(Encoder)解码器(Decoder)是常见的神经网络架构,通常用于处理序列到序列(seq2seq)任务,如机器翻译、文本摘要和图像描述等。

xxk1323的博客 1099

pat1111 Online Map

题意:输入一个图,边权包括路径长度,要消耗的时间。输出最短路径,多条则输出耗时最短的,输出耗时最多的路径,多条则输出经过点最少的。 思路:spfa跑两下就好。 代码 #include &lt;iostream&gt; #include &lt;cstdio&gt; #include &lt;algorithm&gt; #include &lt;cmath&gt; #include &lt;...

cccsssxxx 993

SAP Workflow 工作流开发步骤总结

SAP WORKFLOW

qq_36952606的博客 5107

1111. Online Map (30)-PAT甲级真题(Dijkstra + DFS)

1111. Online Map (30)Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is...

柳婼 の blog 5535

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

PAT_A、1111. Online Map (30)

1111. Online Map (30)Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is

longtails的博客 1070

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

1111 Online Map(30 分)

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is g...

ysq96的博客 869

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

【PAT】1111. Online Map

考查点:最短路径,Dijkstra算法,DFS 思路和提交情况:本题直接DJ算法即可,第一次提交Dj没写对,主要是第一个循环找最小值时忘记判断vis,第二次没考虑相同路径,最费时间的是逻辑搞错了,题目中虽然要输出最短路中最快的但是在维护时间这个权值时不是用通过最快路径得到权值,而是在最短路径中同时更新的自己的权值,还有因为权值都是同一个数组所以调用两个dj方法顺序要按题目来。 #define

天行健,君子以自强不息 642

(PAT)1111. Online Map(迪杰斯特拉算法)

不用模板徒手码代码真TM刺激。。PAT,我有句MMP不知当讲不当讲。 迪杰斯特拉,一个非常不错的算法,老迪也是个牛人,哎呀,跑题了。。不懂DJ算法的话,参考:迪杰斯特拉算法 1111. Online Map (30) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判

路漫漫 始于足下 1467

1111. Online Map (30) 最短路径、DFS

时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two p

一叶知秋 628

PAT甲级1111 【Online Map】 (30)

补充:2018.8.25更新,写这种题一定要静心,否则小bug会不经意间不断出现,再写一遍代码行居然和上次一样,不过方法略有差别。 #include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;vector&gt; #include&lt;cstring&gt; using namespace std; const int maxn ...

gq__97的博客 369

PAT-A1111. 图-Dijkstra输出最短路径

Dijkstra查找单源最短路径,DFS输出路径

lilysara的博客 703

PAT甲1111 Online Map(30 分)

#include &lt;string.h&gt; #include &lt;stdio.h&gt; #include &lt;vector&gt; #include &lt;queue&gt; #include &lt;algorithm&gt; #include &lt;map&gt; #include &lt;string&gt; #include &lt;iostream&

一点点 421

PAT甲级1111 Online Map (30 分)【Dijkstra+dfs】

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is g...

Imagirl1的博客 476

PAT Advanced 1111. Online Map (30)

问题描述:1111. Online Map (30)时间限制300 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueInput our current position and a destination, an online map can recommend several paths. Now your job is to recommen...

kircher的博客 291

1111. Online Map (30)

1111

ZhangVeni的博客 846

PAT(A)1111 Online Map (30)(最短路)

题目大意: 给你n个点,m条路。 接下来m行,V1 V2 one-way length time one-way 表示是不是单向边,1是0否 求最短和最快的距离和时间 并输出路径。 如果路径不一样,则按一下格式输出 Distance = D: source -> v1 -> … -> destination Time = T: source -> w1 ->

No Program No Life 774
上一篇: hdu4278 Faulty Odometer 八进制转十进制
下一篇: hdu4287 Intelligent IME STLmap散列查找
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值