当前位置:网站首页>1163 Dijkstra Sequence
1163 Dijkstra Sequence
2022-06-30 07:59:00 【How far is it forever】
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
NoThe main idea of the topic :
Given a graph I want you to ask whether this graph is Dijkstra Sequence
Be careful :
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
AC Code :
/*
* @Author: Spare Lin
* @Project: AcWing2022
* @Date: 2022/6/28 18:44
* @Description: 4275. Dijkstra Sequence source :PAT Class a real topic 1163
*/
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1e3 + 7;
int n, m; // Points and edges
int dis[MAXN], g[MAXN][MAXN];
bool vis[MAXN];
int q[MAXN];// Input sequence
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 the current point has not been accessed and there is a point smaller than the current point
if (!vis[j] && dis[j] < dis[t]) {
return false;
}
}
vis[t] = true;
// Update current near point
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;
}
边栏推荐
- 奇迹MU服务器租用选择 真实好用 稳定不卡 还能防入侵
- 期末複習-PHP學習筆記5-PHP數組
- Multi whale capital: report on China's education intelligent hardware industry in 2022
- Line fitting (least square method)
- What management improvements can CRM bring to enterprises
- 架构实战营模块 5 作业
- Investment and financing analysis report of Supply Chain & logistics industry in 2021
- 342 maps covering exquisite knowledge, one of which is classic and pasted on the wall
- 深度学习——词汇表征
- Deep learning vocabulary representation
猜你喜欢

Bingbing learning notes: quick sorting

Examen final - notes d'apprentissage PHP 5 - Tableau PHP

Use of nested loops and output instances

Cross compile opencv3.4 download cross compile tool chain and compile (3)

Research Report on search business value in the era of big search in 2022

深度学习——GRU单元

冰冰学习笔记:快速排序

Cadence innovus physical implementation series (I) Lab 1 preliminary innovus

Want to change careers, but don't know what to do? This article is written for you who are confused

More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!
随机推荐
2022.01.20 [bug note] | qiime2: an error was encoded while running dada2 in R (return code 1)
Examen final - notes d'apprentissage PHP 3 - Déclaration de contrôle du processus PHP
期末複習-PHP學習筆記3-PHP流程控制語句
Research Report on search business value in the era of big search in 2022
November 19, 2021 [reading notes] a summary of common problems of sneakemake (Part 2)
December 13, 2021 [reading notes] | understanding of chain specific database building
Line fitting (least square method)
冰冰学习笔记:快速排序
2022 Research Report on China's intelligent fiscal and tax Market: accurate positioning, integration and diversity
Examen final - notes d'apprentissage PHP 5 - Tableau PHP
Basic theory of four elements and its application
MySQL加索引语句不加锁:ALGORITHM=INPLACE, LOCK=NONE
Go 数据类型篇之基本数据类型之间的转化
Introduction notes to pytorch deep learning (11) neural network pooling layer
National technology n32g45x series about timer timing cycle calculation
Deep learning -- feature point detection and target detection
Deep learning -- language model and sequence generation
架构实战营模块 5 作业
Want to ask, how to choose securities companies for stock speculation? Is it safe to open an account online?
Multi whale capital: report on China's education intelligent hardware industry in 2022