当前位置:网站首页>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;
}
边栏推荐
- 交叉熵计算公式
- Introduction and principle explanation of 30 common sensor modules in IOT embedded devices
- [paper notes] next vit: next generation vision transformer for efficient deployment in real industry
- app耗电量测试
- Huawei wireless device sta black and white list configuration command
- [unity introduction program] basic concepts GameObject & components
- webflux默认io线程数
- Delete in elasticserach_ by_ What is the mechanism of query?
- Practical operation: elegant downtime under large-scale micro service architecture
- P1047 [noip2005 popularization group t2] tree outside the school gate
猜你喜欢

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

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

全新8.6版本SEO快排系统(可源码级搭建)

深度学习训练和测试时出现问题:error: the following arguments are required: --dataroot,解决:训练文件的配置方法和测试文件的配置方法

Polling, interrupt, DMA and channel

Teach you to use cann to convert photos into cartoon style

Recommend 7 open source projects of yyds this week

People who lose weight should cry: it's no good not eating food, because your brain will be inflamed
![[unity entry program] basic concept trigger](/img/16/cd0f8ae579627fc095935195136729.png)
[unity entry program] basic concept trigger

Oracle trigger creation
随机推荐
Supplementary notes on Relevant Issues of complete model group
P1046 [NOIP2005 普及组 T1] 陶陶摘苹果
如何仅用递归函数和栈操作逆序一个栈
Nailing the latest version, how to clear the login phone number history data
[pytorch] the most common function of view
Using one stack to sort another stack
转行学什么成为了一大部分人的难题,那么为什么很多人学习软件测试呢?
交叉熵计算公式
[paper notes] effective CNN architecture design guided by visualization
[QNX hypervisor 2.2 user manual]9.3 CPU
Network file storage system (II) practical operation of Minio distributed file system
Configuring WAPI certificate security policy for Huawei wireless devices
Changes were made to tables that cannot be recreated or the prevent saving changes that require the table to be recreated option was enabled
redis客户端工具redis-insight推荐
Is the yield of financial products high or low?
用一个栈实现另一个栈的排序
【Unity入门计划】基本概念-触发器 Trigger
Oracle trigger creation
【微信小程序】全局样式、局部样式、全局配置
P1048 [NOIP2005 普及组 T3] 采药