当前位置:网站首页>Dijkstra序列(暑假每日一题 5)
Dijkstra序列(暑假每日一题 5)
2022-07-25 07:54:00 【sweetheart7-7】
D i j k s t r a Dijkstra Dijkstra 算法是非常著名的贪心算法之一。
它用于解决单源最短路径问题,即指定一个特定源顶点,求该顶点到给定图的所有其他顶点的最短路径。
它由计算机科学家 E d s g e r W . D i j k s t r a Edsger W. Dijkstra EdsgerW.Dijkstra 于 1956 年构思并在三年后出版。
在该算法中,我们需要不断维护一个包含最短路径树中顶点的集合。
在每一步中,我们找到一个尚未在集合内且与源顶点距离最小的顶点,并将其收于集合中。
因此,通过 D i j k s t r a Dijkstra Dijkstra 算法,我们可以逐步生成一个有序的顶点序列,我们称之为 D i j k s t r a Dijkstra Dijkstra 序列。
对于一个给定的图,可能有多个 D i j k s t r a Dijkstra Dijkstra 序列。
例如, { 5 , 1 , 3 , 4 , 2 } \{5,1,3,4,2\} { 5,1,3,4,2} 和 { 5 , 3 , 1 , 2 , 4 } \{5,3,1,2,4\} { 5,3,1,2,4} 都是给定图的 Dijkstra 序列。
注意,序列中的第一个顶点即为指定的特定源顶点。
你的任务是检查给定的序列是否是 Dijkstra 序列。
输入格式
第一行包含两个整数 N N N 和 M M M,表示图中点和边的数量。
点的编号 1 ∼ N 1∼N 1∼N。
接下来 M M M 行,每行包含三个整数 a , b , c a,b,c a,b,c,表示点 a a a 和点 b b b 之间存在一条无向边,长度为 c c c。
再一行包含整数 K K K,表示需要判断的序列个数。
接下来 K K K 行,每行包含一个 1 ∼ N 1∼N 1∼N 的排列,表示一个给定序列。
输出格式
共 K K K 行,第 i i i 行输出第 K K K 个序列的判断,如果序列是 D i j k s t r a Dijkstra Dijkstra 序列则输出 Yes,否则输出 No。
数据范围
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,
保证给定无向图是连通图,
保证无重边和自环。
输入样例:
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
抓住本质:dijkstra 序列,系列中依次每个点距离原来的距离是逐渐递增的(不递减序列)
#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;
}
边栏推荐
- The value of integer a after bitwise negation (~) is - (a+1)
- 由两个栈组成的队列
- Introduction and principle explanation of 30 common sensor modules in IOT embedded devices
- A review of nature: gender differences in anxiety and depression - circuits and mechanisms
- 全新8.6版本SEO快排系统(可源码级搭建)
- Use of toolbar
- How to do a good job in safety development?
- Polling, interrupt, DMA and channel
- Using one stack to sort another stack
- 【论文笔记】Progressive Layered Extraction (PLE): A Novel Multi-Task Learning (MTL) Model for Personalized
猜你喜欢

【Unity入门计划】基本概念-预制件 Prefab

How does MTK change the boot logo?

第十七届振兴杯计算机程序设计员(云计算平台运维与开发)决赛

Introduction to machine learning (I): understanding maximum likelihood estimation in supervised learning
![[paper notes] effective CNN architecture design guided by visualization](/img/aa/aeeac3f970eac7f110987c523602c8.png)
[paper notes] effective CNN architecture design guided by visualization

【软件测试】包装简历从这几点出发、提升通过率

CSDN custom T-shirts are waiting for you to get, and the benefits of new programmer are coming!

Completely replace the redis+ database architecture, and JD 618 is stable!

Eval and assert one sentence Trojan horse analysis

Today in history: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
随机推荐
设计一个有getMin功能的栈
Introduction and principle explanation of 30 common sensor modules in IOT embedded devices
Gather the wisdom of developers and consolidate the foundation of the database industry
yolov7 网络架构深度解析
P1047 [noip2005 popularization group t2] tree outside the school gate
A queue of two stacks
Summer Challenge harmonyos - slider slider for custom components
How to do a good job in safety development?
Introduction to Manhattan distance
What has become a difficult problem for most people to change careers, so why do many people study software testing?
轮询、中断、DMA和通道
UNIPRO multi terminal deployment to meet customers' diversified needs
About gbase automatically closing the connection
batchnorm 和layernorm的区别
Huawei wireless device sta black and white list configuration command
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
If Debian infringes the rust trademark, will it be exempted by compromising and renaming?
Supplementary notes on Relevant Issues of complete model group
大佬秋招面经
2-6. Automatic acquisition