当前位置:网站首页>1163 Dijkstra Sequence
1163 Dijkstra Sequence
2022-06-30 07:26:00 【永远有多远.】
Dijkstra's algorithm is one of the very famous greedy algorithms.
It is used for solving the single source shortest path problem which gives the shortest paths from one particular source vertex to all the other vertices of the given graph. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.
In this algorithm, a set contains vertices included in shortest path tree is maintained. During each step, we find one vertex which is not yet included and has a minimum distance from the source, and collect it into the set. Hence step by step an ordered sequence of vertices, let's call it Dijkstra sequence, is generated by Dijkstra's algorithm.
On the other hand, for a given graph, there could be more than one Dijkstra sequence. For example, both { 5, 1, 3, 4, 2 } and { 5, 3, 1, 2, 4 } are Dijkstra sequences for the graph, where 5 is the source. Your job is to check whether a given sequence is Dijkstra sequence or not.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers Nv (≤103) and Ne (≤105), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to Nv.
Then Ne lines follow, each describes an edge by giving the indices of the vertices at the two ends, followed by a positive integer weight (≤100) of the edge. It is guaranteed that the given graph is connected.
Finally the number of queries, K, is given as a positive integer no larger than 100, followed by K lines of sequences, each contains a permutationof the Nv vertices. It is assumed that the first vertex is the source for each sequence.
All the inputs in a line are separated by a space.
Output Specification:
For each of the K sequences, print in a line Yes if it is a Dijkstra sequence, or No if not.
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题目大意:
给定一个图 要你求这个图是否是Dijkstra序列
注意:
在每一步中,我们找到一个尚未在集合内且与源顶点距离最小的顶点,并将其收于集合中
AC代码:
/*
* @Author: Spare Lin
* @Project: AcWing2022
* @Date: 2022/6/28 18:44
* @Description: 4275. Dijkstra序列 来源:PAT甲级真题1163
*/
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1e3 + 7;
int n, m; //点和边
int dis[MAXN], g[MAXN][MAXN];
bool vis[MAXN];
int q[MAXN];//输入的序列
bool Dijkstra() {
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[q[0]] = 0;
for (int i = 0; i < n; ++i) {
int t = q[i];
for (int j = 1; j <= n; ++j) {
//若当前点没被访问过且存在一个点比当前点更小
if (!vis[j] && dis[j] < dis[t]) {
return false;
}
}
vis[t] = true;
//更新当前近的点
for (int j = 1; j <= n; ++j) {
dis[j] = min(dis[j], dis[t] + g[t][j]);
}
}
return true;
}
int main() {
scanf("%d%d", &n, &m);
memset(g, 0x3f, sizeof(g));
while (m--) {
int x, y, val;
scanf_s("%d%d%d", &x, &y, &val);
g[x][y] = g[y][x] = val;
}
int k;
scanf("%d", &k);
while (k--) {
for (int i = 0; i < n; ++i) {
scanf("%d", &q[i]);
}
printf(Dijkstra() ? "Yes\n" : "No\n");
}
return 0;
}
边栏推荐
- right four steps of SEIF SLAM
- C. Fishingprince Plays With Array
- Wangbohua: development situation and challenges of photovoltaic industry
- Lodash filter collection using array of values
- Analysis of cross clock transmission in tinyriscv
- Solve the linear equation of a specified point and a specified direction
- 深度学习——GRU单元
- Introduction notes to pytorch deep learning (11) neural network pooling layer
- January 23, 2022 [reading notes] - bioinformatics and functional genomics (Chapter 6: multiple sequence alignment)
- Parameter calculation of deep learning convolution neural network
猜你喜欢

2022 retail industry strategy: three strategies for consumer goods gold digging (in depth)
![Cadence physical library lef file syntax learning [continuous update]](/img/5a/b42269d80c13779762a8da67ba6989.jpg)
Cadence physical library lef file syntax learning [continuous update]
![2021-10-27 [WGS] pacbio third generation methylation modification process](/img/a3/39d05e0daf4ea7eba95337b7a936b1.jpg)
2021-10-27 [WGS] pacbio third generation methylation modification process

String application -- string violent matching (implemented in C language)

Processes, jobs, and services

HelloWorld

Examen final - notes d'apprentissage PHP 5 - Tableau PHP
![November 16, 2021 [reading notes] - macro genome analysis process](/img/c4/4c74ff1b4049f5532c871eb00d5ae7.jpg)
November 16, 2021 [reading notes] - macro genome analysis process

24C02

Global digital industry strategy and policy observation in 2021 (China Academy of ICT)
随机推荐
Proteus catalog component names and Chinese English cross reference
深度学习——词汇表征
Line fitting (least square method)
深度学习——卷积的滑动窗口实现
Raspberry pie 4B Getting Started Guide
Implementation of double linked list in C language
Ad\dxp how to solve the problem of not knowing the schematic Library
期末复习-PHP学习笔记5-PHP数组
ACM. HJ48 从单向链表中删除指定值的节点 ●●
深度学习——网络中的网络以及1x1卷积
2022.01.20 [bug note] | qiime2: an error was encoded while running dada2 in R (return code 1)
Solve the linear equation of a specified point and a specified direction
期末复习-PHP学习笔记3-PHP流程控制语句
Program acceleration
期末复习-PHP学习笔记6-字符串处理
Given a fixed point and a straight line, find the normal equation of the straight line passing through the point
Introduction notes to pytorch deep learning (10) neural network convolution layer
Processes, jobs, and services
MCU essay
Basic knowledge points