当前位置:网站首页>4275. Dijkstra sequence
4275. Dijkstra sequence
2022-07-27 08:38:00 【Pursue people far away】
Dijkstra Algorithm is one of the most famous greedy algorithms .
It is used to solve the single source shortest path problem , That is, specify a specific source vertex , Find the shortest path from this vertex to all other vertices of a given graph .
It was developed by computer scientists Edsger W. Dijkstra On 1956 Conceived in and published three years later .
In this algorithm , We need to constantly maintain a set of vertices in the shortest path tree .
At each step , We find a vertex that is not yet in the set and has the smallest distance from the source vertex , And put it in the collection .
therefore , adopt Dijkstra Algorithm , We can generate an ordered sequence of vertices step by step , We call it Dijkstra Sequence .
For a given graph , There may be more than one Dijkstra Sequence .
for example ,{5,1,3,4,2} and {5,3,1,2,4} Are all given graphs Dijkstra Sequence .
Be careful , The first vertex in the sequence is the specified source vertex .
Your task is to check whether a given sequence is Dijkstra Sequence .
Input format
The first line contains two integers N and M, Represents the number of points and edges in the graph .
Number of points 1∼N.
Next M That's ok , Each line contains three integers a,b,c, Indication point a Sum point b There is an undirected edge between , The length is c.
The next line contains integers K, Indicates the number of sequences to be judged .
Next K That's ok , Each line contains a 1∼N Permutation , Represents a given sequence .
Output format
common K That's ok , The first i Line output the K Judgment of sequences , If the sequence is Dijkstra Sequence is output Yes, Otherwise output No.
Data range
1≤N≤1000,
1≤M≤105,
1≤a,b≤N,
1≤c≤100,
1≤K≤100,
Ensure that a given undirected graph is connected ,
Ensure no double edges and self rings ( The official website didn't mention , But after actual measurement , The official website data meets this condition ).
sample input :
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
sample output :
Yes
Yes
Yes
No
Code :
// Judge whether each step is the current minimum
#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;
}
边栏推荐
- Luogu super Mary game
- All in one 1319 - queue for water
- Solution of database migration error
- 4279. 笛卡尔树
- Eval and assert execute one sentence Trojan horse
- while Loop
- 缓存一致性与内存屏障
- Iterators and generators
- Zhongang Mining: the new energy industry is developing rapidly, and fluorine chemical products have a strong momentum
- UVM Introduction Experiment 1
猜你喜欢

众昂矿业:新能源行业快速发展,氟化工产品势头强劲

OSI seven layer model and tcp/ip four layer (TCP and UDP) (notes)
![[NPUCTF2020]ReadlezPHP 1](/img/d9/590446b45f917be3f077a9ea739c20.png)
[NPUCTF2020]ReadlezPHP 1

Minio 安装与使用

Day4 --- flask blueprint and rest ful

无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。

Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?

Solution to the program design of the sequence structure of one book (Chapter 1)

How to upload qiniu cloud

P7 Day1 get to know the flask framework
随机推荐
UVM Introduction Experiment 1
UVM入门实验1
[netding cup 2020 Qinglong group]areuserialz (buuctf)
面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
Connection failed during installation of ros2 [ip: 91.189.91.39 80]
海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态
User management - restrictions
海关总署:这类产品暂停进口
Query and association of flask to database
Fluent rendering mechanism - GPU thread rendering
Solution to the program design of the sequence structure of one book (Chapter 1)
Flask login implementation
Have a good laugh
Bandwidth and currency
4279. 笛卡尔树
Help send some recruitment. If you are interested, you can have a look
Background image related applications - full, adaptive
阿里云国际版回执消息简介与配置流程
众昂矿业:新能源行业快速发展,氟化工产品势头强劲
[NPUCTF2020]ReadlezPHP 1