当前位置:网站首页>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;
}
边栏推荐
- Science: listening to music can really relieve pain. Chinese scientists reveal the neural mechanism behind it
- Changes were made to tables that cannot be recreated or the prevent saving changes that require the table to be recreated option was enabled
- YUV player
- [unity introduction plan] interface Introduction (2) -games view & hierarchy & Project & Inspector
- 深度学习之快速实现数据集增强的方法
- P1048 [noip2005 popularization group t3] drug collection
- Configuring WAPI certificate security policy for Huawei wireless devices
- Quickly build a centralized logging platform through elk
- 全新8.6版本SEO快排系统(可源码级搭建)
- Talk about programmers learning English again
猜你喜欢

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

Eval and assert one sentence Trojan horse analysis

Teach you to use cann to convert photos into cartoon style

Network file storage system (II) practical operation of Minio distributed file system

【Unity入门计划】界面介绍(2)-Games视图&Hierarchy&Project&Inspector
![[unity entry plan] interface Introduction (1) -scene view](/img/88/dee292cb90cd740640018e7260107f.png)
[unity entry plan] interface Introduction (1) -scene view
![[wechat applet] global style, local style, global configuration](/img/8e/c6241ab0f28e3f468dbfa923b91d20.png)
[wechat applet] global style, local style, global configuration

batchnorm 和layernorm的区别

A fast method of data set enhancement for deep learning

Competition path design of beacon group
随机推荐
Eval and assert one sentence Trojan horse analysis
Completely replace the redis+ database architecture, and JD 618 is stable!
Introduction to Manhattan distance
Growth path - InfoQ video experience notes [easy to understand]
P1048 [NOIP2005 普及组 T3] 采药
Is the yield of financial products high or low?
pom容易忽略的问题
Network file storage system (III) practice of fastdfs distributed file system
How to do a good job in safety development?
Cache design in Web services (error allowed, error not allowed)
2-6.自动化采集
Oracle19 adopts automatic memory management. The AWR report shows that the settings of SGA and PGA are too small?
The two Nobel Prize winners became the chief scientist of the sky high price Baijiu of "taishanglaojun holding a dream"
A queue of two stacks
[unity entry program] make my first little game
大佬秋招面经
oracle 触发器创建
【Unity入门计划】基本概念-触发器 Trigger
YUV player
Leetcode (Sword finger offer) - 04. search in two-dimensional array