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. 第7章 PTA查找练习题

    这道题与第7章查找有关,当时提前看到,翻到书里面的算法,然后打进去,虽然是正确的,但是那时候并不知道二叉排序树的基础知识,包括插入查找的来龙去脉,现在已经学到了,有了一定了解,发现题目只用到了其中部分 ...

  2. C#如何在Socket传递负数,比如-51

    1.关于计算机中的原码.反码和补码定义 1.原码   将最高位作为符号位(以0代表正,1代表负),其余各位代表数值本身的绝对值(以二进制表示).为了简单起见,我们用1个字节来表示一个整数.     + ...

  3. Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer

    原文:Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处 ...

  4. Node 12 值得关注的新特性

    前言 时隔一年,Node.js 12 如约而至,正式发布第一个 Current 版本. 该版本带来了诸如: V8 更新带来好多不错的特性. HTTP 解析速度提升. 启动速度大幅提升. 更好的诊断报告 ...

  5. 在a标签中使用了onclick修改样式之后a:hover失效

    是因为优先级的原因造成,使用!important修改优先级. 如修改成: .button1:hover {            color: #FFF !important;            ...

  6. Power Designer将数据库表结构导出到Word

     一.   安装与运行PowerDesigner(本例中用的版本是15.1) 二.“File”→“New Model”→“Categories”→“Information”→“Physical Dat ...

  7. 12 | 为什么我的MySQL会“抖”一下? 学习记录

    <MySQL实战45讲>12 | 为什么我的MySQL会“抖”一下? 学习记录 http://naotu.baidu.com/file/15aa54cab2fa882c6a2a1dd52e ...

  8. thread.join() 阻塞原理分析

    参考: https://blog.csdn.net/u010983881/article/details/80257703

  9. kafka docker-composer.yml

    使用Docker快速搭建Kafka开发环境 表现力 关注  0.5 2018.05.04 03:00* 字数 740 阅读 25240评论 1喜欢 11 Docker在很多时候都可以帮助我们快速搭建想 ...

  10. 使用rabbitctl添加用户

    使用rabbitctl添加用户 第一.添加mq用户并设置密码 root@live-mq-01:~ # rabbitmqctl add_user mq 123456 1 root@live-mq-01: ...