当前位置:网站首页>Dijkstra sequence (day 66)
Dijkstra sequence (day 66)
2022-07-19 16:46:00 【Zhangxueheng】
List of articles
1: subject
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
2: Realization
#include <iostream>
#include <cstring>
#include <algorithm>
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(dist, 0x3f, sizeof dist);
memset(st, 0, sizeof st);
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 ++ )
dist[j] = min(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;
}
边栏推荐
- 【Unity3D】UGUI之布局组件
- Nacos Client常用配置
- Alibaba cloud (OSS) file upload and deletion
- 梅科尔工作室-DjangoWeb 应用框架+MySQL数据库第四次培训
- 【VScode输出为乱码】解决方法
- Why do many people still work when they know that they don't earn money?
- RT thread training learning and experience (II)
- OS知识点简介(二)
- Notes d'apprentissage de niveau C1 certifiées csdn - web Advanced
- [unity3d] dropdown of ugui
猜你喜欢
软件设计师:12-案例分析例题
CF591A Wizards‘ Duel
TCP 通信流程
【mysql篇-进阶篇】存储引擎
UVA10341 Solve It
Ribbon负载均衡策略
Gesture Recognition Dataset: Jester 数据集解压
阿里云(OSS)文件上传和删除
Jinlv environment listed on Shenzhen Stock Exchange: market value of 5billion Yu Xiaoxia family has a strong color
RT thread training learning and experience (II)
随机推荐
YoloV7:基于自己训练的模型如何导出正确的ONNX
Nacos Client常用配置
Rsync data synchronization script
[QNX Hypervisor 2.2用户手册]8.3 Guest触发的退出
codeforces每日5题(均1500)-第十八天
TCP 通信流程
【VScode输出为乱码】解决方法
mysql函数汇总之条件判断函数
Torch dist distributed data aggregation
项目报错“BeanInitializationException: com.xxxxx.xx.dao.data.Dao can‘t get a sessionFactory“
CANoe:.vmodule文件是什么
Solutions to the different sizes of PDF pages after PDF merging
51world 如何修改渲染视频/窗口的大小 ?
面试快速复习(二):交叉熵为什么有用
Word文档中封面的下划线如何精确对齐
epoll的ET工作模式和LT工作模式
软件设计师:12-案例分析例题
数据湖(十三):Spark与Iceberg整合DDL操作
Nacos client common configuration
字符集7-10