当前位置:网站首页>Dijkstra sequence (summer vacation daily question 5)
Dijkstra sequence (summer vacation daily question 5)
2022-07-25 07:58:00 【sweetheart7-7】
D i j k s t r a Dijkstra 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 E d s g e r W . D i j k s t r a Edsger W. Dijkstra EdsgerW.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 D i j k s t r a Dijkstra Dijkstra Algorithm , We can generate an ordered sequence of vertices step by step , We call it D i j k s t r a Dijkstra Dijkstra Sequence .
For a given graph , There may be more than one D i j k s t r a Dijkstra Dijkstra Sequence .
for example , { 5 , 1 , 3 , 4 , 2 } \{5,1,3,4,2\} { 5,1,3,4,2} and { 5 , 3 , 1 , 2 , 4 } \{5,3,1,2,4\} { 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 N N and M M M, Represents the number of points and edges in the graph .
Number of points 1 ∼ N 1∼N 1∼N.
Next M M M That's ok , Each line contains three integers a , b , c a,b,c a,b,c, Indication point a a a Sum point b b b There is an undirected edge between , The length is c c c.
The next line contains integers K K K, Indicates the number of sequences to be judged .
Next K K K That's ok , Each line contains a 1 ∼ N 1∼N 1∼N Permutation , Represents a given sequence .
Output format
common K K K That's ok , The first i i i Line output the K K K Judgment of sequences , If the sequence is D i j k s t r a Dijkstra Dijkstra Sequence is output Yes, Otherwise output No.
Data range
1 ≤ N ≤ 1000 , 1≤N≤1000, 1≤N≤1000,
1 ≤ M ≤ 1 0 5 , 1≤M≤10^5, 1≤M≤105,
1 ≤ a , b ≤ N , 1≤a,b≤N, 1≤a,b≤N,
1 ≤ c ≤ 100 , 1≤c≤100, 1≤c≤100,
1 ≤ K ≤ 100 , 1≤K≤100, 1≤K≤100,
Ensure that a given undirected graph is connected ,
Ensure no double edges and self rings .
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
Grasp the essence :dijkstra Sequence , The distance from each point in the series to the original point is gradually increasing ( Non decreasing sequence )
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1010;
int n, m;
int g[N][N], q[N], d[N];
bool st[N];
bool is_dijkstra(){
memset(d, 0x3f, sizeof d);
memset(st, 0, sizeof st);
d[q[0]] = 0;
for(int i = 0; i < n; i++){
int t = -1;
for(int j = 1; j <= n; j++)
if(!st[j] && (t == -1 || d[j] < d[t]))
t = j;
st[t] = true;
for(int j = 1; j <= n; j++)
d[j] = min(d[j], d[t] + g[t][j]);
}
int last = -1;
for(int i = 0; i < n; i++)
if(d[q[i]] < last) return false;
else last = d[q[i]];
return true;
}
int main(){
memset(g, 0x3f, sizeof g);
scanf("%d%d", &n, &m);
int a, b, w;
for(int i = 0; i < m; i++){
scanf("%d%d%d", &a, &b, &w);
g[a][b] = g[b][a] = min(g[a][b], w);
}
int k;
scanf("%d", &k);
while(k--){
for(int i = 0; i < n; i++)
scanf("%d", &q[i]);
if(is_dijkstra()) puts("Yes");
else puts("No");
}
return 0;
}
边栏推荐
- Teach you to use cann to convert photos into cartoon style
- 大佬秋招面经
- A review of nature: gender differences in anxiety and depression - circuits and mechanisms
- [dynamic programming] - Knapsack model
- Vs2019 C MFC installation
- [unity entry program] basic concept trigger
- [unity introduction program] basic concepts -2d rigid body 2D
- [ES6] function parameters, symbol data types, iterators and generators
- A fast method of data set enhancement for deep learning
- Oracle trigger creation
猜你喜欢

Use cyclegan to train self-made data sets, popular tutorials, and get started quickly

【Unity入门计划】制作我的第一个小游戏

Recommend 7 open source projects of yyds this week

机器学习入门详解(一):理解监督学习中的最大似然估计

Gather the wisdom of developers and consolidate the foundation of the database industry

深度学习之快速实现数据集增强的方法
![[unity entry plan] interface Introduction (1) -scene view](/img/88/dee292cb90cd740640018e7260107f.png)
[unity entry plan] interface Introduction (1) -scene view

【Unity入门计划】基本概念-2D刚体Rigidbody 2D

Google AI can't understand the comments of netizens, and the wrong meaning will be as high as 30%. Netizens: you don't understand my stem

A fast method of data set enhancement for deep learning
随机推荐
【软件测试】包装简历从这几点出发、提升通过率
用一个栈实现另一个栈的排序
[ffmpeg] MP4 to YUV
Redis client tool redis insight recommendation
P1048 [noip2005 popularization group t3] drug collection
P1086 [NOIP2004 普及组第二题] 花生采摘
app耗电量测试
【Unity入门计划】基本概念-预制件 Prefab
Cache design in Web services (error allowed, error not allowed)
P1046 [NOIP2005 普及组 T1] 陶陶摘苹果
Problems during nanodet training: modulenotfounderror: no module named 'nanodet' solution
Implement hot post | community project with timed tasks and cache
整数a按位取反(~)后的值为-(a+1)
webflux默认io线程数
Vs2019 C MFC installation
P1047 [NOIP2005 普及组 T2] 校门外的树
Today in history: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
[paper notes] next vit: next generation vision transformer for efficient deployment in real industry
[QNX hypervisor 2.2 user manual]9.3 CPU
[paper notes] effective CNN architecture design guided by visualization