当前位置:网站首页>4275. Dijkstra序列
4275. Dijkstra序列
2022-07-27 08:37:00 【追寻远方的人】
Dijkstra 算法是非常著名的贪心算法之一。
它用于解决单源最短路径问题,即指定一个特定源顶点,求该顶点到给定图的所有其他顶点的最短路径。
它由计算机科学家 Edsger W. Dijkstra 于 1956 年构思并在三年后出版。
在该算法中,我们需要不断维护一个包含最短路径树中顶点的集合。
在每一步中,我们找到一个尚未在集合内且与源顶点距离最小的顶点,并将其收于集合中。
因此,通过 Dijkstra 算法,我们可以逐步生成一个有序的顶点序列,我们称之为 Dijkstra 序列。
对于一个给定的图,可能有多个 Dijkstra 序列。
例如,{5,1,3,4,2} 和 {5,3,1,2,4} 都是给定图的 Dijkstra 序列。
注意,序列中的第一个顶点即为指定的特定源顶点。
你的任务是检查给定的序列是否是 Dijkstra 序列。
输入格式
第一行包含两个整数 N 和 M,表示图中点和边的数量。
点的编号 1∼N。
接下来 M 行,每行包含三个整数 a,b,c,表示点 a 和点 b 之间存在一条无向边,长度为 c。
再一行包含整数 K,表示需要判断的序列个数。
接下来 K 行,每行包含一个 1∼N 的排列,表示一个给定序列。
输出格式
共 K 行,第 i 行输出第 K 个序列的判断,如果序列是 Dijkstra 序列则输出 Yes,否则输出 No。
数据范围
1≤N≤1000,
1≤M≤105,
1≤a,b≤N,
1≤c≤100,
1≤K≤100,
保证给定无向图是连通图,
保证无重边和自环(官网没提,但是经实测,官网数据符合此条件)。
输入样例:
5 7
1 2 2
1 5 1
2 3 1
2 4 1
2 5 2
3 5 1
3 4 1
4
5 1 3 4 2
5 3 1 2 4
2 3 4 5 1
3 2 1 5 4
输出样例:
Yes
Yes
Yes
No
代码:
// 判断每一步是否是当前最小即可
#include <bits/stdc++.h>
using namespace std;
const int N = 1010, INF = 0x3f3f3f3f;
int n, m;
int dist[N], g[N][N];
bool st[N];
int q[N];
bool dijkstra()
{
memset(st, 0, sizeof st);
memset(dist, 0x3f, sizeof dist);
dist[q[0]] = 0;
for (int i = 0; i < n; i++)
{
int t = q[i];
for (int j = 1; j <= n; j++)
if (!st[j] && dist[j] < dist[t])
return false;
st[t] = true;
for (int j = 1; j <= n; j++)
{
if (!st[j] && dist[j] > dist[t] + g[t][j])
dist[j] = dist[t] + g[t][j];
}
}
return true;
}
int main()
{
scanf("%d%d", &n, &m);
memset(g, 0x3f, sizeof g);
while (m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
g[a][b] = g[b][a] = c;
}
int k;
scanf("%d", &k);
while (k--)
{
for (int i = 0; i < n; i++)
scanf("%d", &q[i]);
if (dijkstra())
puts("Yes");
else
puts("No");
}
return 0;
}
边栏推荐
- I drew a Gu ailing with characters!
- 【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发1/100
- Redis configuration file download
- Implementation of adding function of background user management display
- ERP production operation control Huaxia
- MCDF顶层验证方案
- Flutter 渲染机制——GPU线程渲染
- 无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
- sql_ Mode strict mode (ansi/traditional/strict_trans_tables)
- Minio installation and use
猜你喜欢

"PHP Basics" use of integer data

Installation and use of beef XSS

情人节,我用字符画出了一个对象!

Realization of backstage brand management function

After downloading URL loader and specifying the size of the image with limit, the image will not be displayed
![Connection failed during installation of ros2 [ip: 91.189.91.39 80]](/img/7f/92b7d44cddc03c58364d8d3f19198a.png)
Connection failed during installation of ros2 [ip: 91.189.91.39 80]

Explain cache consistency and memory barrier

第2章 前台数据展现

MCDF top level verification scheme

Process control - Branch
随机推荐
【渗透测试工具分享】【dnslog服务器搭建指导】
sql_ Mode strict mode (ansi/traditional/strict_trans_tables)
Make a game by yourself with pyGame 01
[netding cup 2020 rosefinch group]nmap 1 two solutions
说透缓存一致性与内存屏障
[geek challenge 2019] finalsql 1
Vcenter7.0 managing esxi7.0 hosts
All in one 1319 - queue for water
ROS2安装时出现Connection failed [IP: 91.189.91.39 80]
What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?
[MRCTF2020]PYWebsite 1
CMD command and NPM command
Set set
OSI seven layer model and tcp/ip four layer (TCP and UDP) (notes)
Process control - Branch
我用字符画出了一个谷爱凌!
"PHP Basics" use of integer data
[BJDCTF2020]EasySearch 1
Software test interview questions (key points)
Use of "PHP Basics" delimiters