当前位置:网站首页>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;
}
边栏推荐
- After installing mysql, docker entered the container and found that he could not log in to MySQL
- Background image related applications - full, adaptive
- Flink1.15 source code reading Flink clients client execution process (reading is boring)
- Forced login, seven cattle cloud upload pictures
- Blueprint class view method
- Use of flask
- [MRCTF2020]PYWebsite 1
- How to upload qiniu cloud
- Chapter 2 foreground data display
- [netding cup 2020 rosefinch group]nmap 1 two solutions
猜你喜欢

regular expression

百人参与,openGauss开源社区这群人都在讨论什么?

Flask project configuration

openGauss之TryMe初体验

One book 1201 Fibonacci sequence

Attack and defense World Lottery

Apache SSI remote command execution vulnerability

Risk control and application of informatization project

Hundreds of people participated. What are these people talking about in the opengauss open source community?

Element display mode: block level, inline, inline block, nesting specification, display mode conversion
随机推荐
我用字符画出了一个谷爱凌!
regular expression
Have a good laugh
借生态力量,openGauss突破性能瓶颈
如何在qsim查看软件对象的实例?
Luogu Taotao picks apples
I drew a Gu ailing with characters!
Pass parameters and returned responses of flask
Day4 --- flask blueprint and rest ful
General Administration of Customs: the import of such products is suspended
All in one 1319 - queue for water
好吃难吃饱七分为宜;好喝难喝醉三分为佳
Vcenter7.0 installation of ibm3650m4 physical machine
pytorch_demo1
Login to homepage function implementation
Risk control and application of informatization project
Implementation of registration function
Day5 - Flame restful request response and Sqlalchemy Foundation
How to view instances of software objects in QSIM?
Cache consistency and memory barrier