7-4 Dijkstra Sequence (30 分)

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 N v (≤10 3 ) Nv(≤103) and N e (≤10 5 ) Ne(≤105) , which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to N v  Nv

Then N e  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 N v  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

【声明】

  由于此题还未上PAT官网题库,故没有测试集,仅仅是通过了样例,若发现错误,感谢留言指正。

Solution:

  这道题不难,首先针对每个咨询,都对其出发点进行Dijkstra,然后判断询问的数组顺序是不是从小距离到大距离的,是的话那么就是Dijstra了,否则不是。

  当然也可以在Dijstra的过程中直接判断,即每次选中的中间节点的最短距离是不是满足给出数组的那个节点,是的话继续。否则直接false

  

 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m, k, v[][] = { }, path[], check[];
bool Dijkstra(int x)
{
vector<bool>visit(n + , false);
fill(path, path + n + , INT32_MAX);
path[x] = ;
for (int t = ; t <= n; ++t)
{
int index = -, minDis = INT32_MAX;
for (int i = ; i <= n; ++i)
{
if (visit[i] == false && minDis > path[i])
{
minDis = path[i];
index = i;
}
}
if (path[check[t]] == minDis)index = check[t];//按照给出数组的顺序选择中间节点
else return false;
visit[index] = true;
for (int u = ; u <= n; ++u)
if (visit[u] == false && v[index][u] > )
if (path[u] > path[index] + v[index][u])
path[u] = path[index] + v[index][u];
}
return true;
}
int main()
{
cin >> n >> m;
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
v[a][b] = v[b][a] = c;
}
cin >> k;
while (k--)
{
for (int i = ; i <= n; ++i)
cin >> check[i];
if (Dijkstra(check[]))
printf("Yes\n");
else
printf("No\n");
}
return ;
}

PAT甲级【2019年9月考题】——A1164 DijkstraSequence【30】的更多相关文章

  1. 2019年3月29日至30日深圳共创力《成功的产品经理DNA》在深圳公开课成功举办

    2019年3月29至30日,在深圳南山区中南海滨大酒店10楼行政厅,由深圳市共创力企业管理咨询有限公司举办的<成功的产品经理DNA>公开课成功举办,此次公开课由深圳市共创力咨询资深讲师冯老 ...

  2. PAT甲级【2019年3月考题】——A1157 Anniversary【25】

    Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebrat ...

  3. PAT甲级【2019年9月考题】——A1163 PostfixExpression【25】

    7-3 Postfix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspon ...

  4. PAT甲级【2019年9月考题】——A1162 MergingLinkedLists【25】

    7-2 Merging Linked Lists (25 分) Given two singly linked lists L 1 =a 1 →a 2 →...→a n−1 →a n  L1=a1→a ...

  5. PAT甲级【2019年9月考题】——A1160 Forever【20】

    7-1 Forever (20 分) "Forever number" is a positive integer A with K digits, satisfying the ...

  6. PAT甲级【2019年3月考题】——A1159 Structure_of_a_BinaryTree【30】

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】

    Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting ...

  8. PAT甲级【2019年3月考题】——A1156 SexyPrimes【20】

    Sexy primes are pairs of primes of the form (p, p+6), so-named since “sex” is the Latin word for “si ...

  9. PAT甲级2019冬季考试题解

    A Good In C纯模拟题,用string数组读入数据,注意单词数量的判断 #include<bits/stdc++.h> using namespace std; ; ][]; in ...

随机推荐

  1. C# string.Join 的使用

    原文:https://www.cnblogs.com/wangjunguang/p/11122145.html string.Join分为以下五类,用法都有讲解. HashSet<string& ...

  2. 一、.Net Core 分块上传文件

    一..Net Core 分块上传文件 一.前端实现 @* For more information on enabling MVC for empty projects, visit http://g ...

  3. keepalived的配置文件

    ! Configuration File for keepalived global_defs { notification_email { # acassen@firewall.loc # fail ...

  4. 基于firebird的数据转存

    功能:使用于相同的表从一个数据库转存到另一数据库: 方式:直连fdb并加载django,引用django的model完成: 原因:1.select * from *** 返回的数有很多None,直接i ...

  5. php函数漏洞

    1.ereg — 正则表达式匹配 此函数遇 %00 截断. <?php $a = $_GET['pwd']; var_dump(ereg ("^[0-9]+$", $a)); ...

  6. 如何使您的Wifi路由器更安全,网络安全专家告诉您!

    中国知名“黑客”教父,网络安全专家郭盛华曾说过,Wifi路由器这样设置最安全.因为无线路由器都有不同的接口,不同的设置方式以及可以调整的不同设置.在本文中,我将探讨TP-LinkArcher的界面.您 ...

  7. man fdisk

    FDISK(8)       Linux Programmer?. Manual/Linux程序手册       FDISK(8) NAME/名称       fdisk - Partition ta ...

  8. 对webpack的初步研究8

    模块 编辑文档 在模块化编程中,开发人员将程序分解为称为模块的离散功能块. 每个模块的表面积小于完整程序,使验证,调试和测试变得微不足道.编写良好的模块提供了可靠的抽象和封装边界,因此每个模块在整个应 ...

  9. git config使用

    我们知道config是配置的意思,那么git config命令就是对git进行一些配置.而配置一般都是写在配置文件里面,那么git的配置文件在哪里呢?互动一下,先问下大家. 你们所知的git配置文件是 ...

  10. 计算一段日期内的周末天数的php代码(星期六,星期日总和)

    代码如下: /*| Author: Yang Yu <niceses@gmail.com>| @param char|int $start_date 一个有效的日期格式,例如:200910 ...