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. Python2/3 安装各类包的教程

    1.pycryptodome(pyCrypto) pyCrypto包已经失效了,需要替换为pycryptodome 有SSR直接 pip install pycryptodome 国内用 pip in ...

  2. mysql中explain输出列之id的用法详解

    参考mysql5.7 en manual,对列id的解释: The SELECT identifier. This is the sequential number of the SELECT wit ...

  3. IO流详解及测试代码

    IO流 (1)IO用于在设备间进行数据传输的操作 (2)分类:    A:流向       输入流 读取数据      输出流 写出数据   B:数据类型     字节流         字节输入流  ...

  4. 初步理解React

    1.组件化 在 MV* 架构出现之前,组件主要分为两种: 狭义上的组件,又称为 UI 组件,比如 Tabs 组件.Dropdown 组件.组件主要围绕在交互 动作上的抽象,针对这些交互动作,利用 Ja ...

  5. Python实战之网上银行及购物商城

    前言:这是初学时写的小项目,觉得有意思就写来玩玩,也当是巩固刚学习的知识.现在看来很不成熟,但还是记录一下做个纪念好了~ 1.名称:网上网上银行及购物商城 2.项目结构: 当时刚接触python啦,哪 ...

  6. Sql server 启用调试

    在SQL Server 2008管理平台上,调试2005的数据库,会报错. 用 SQL Server 2008管理平台,调试本机数据库,当登录服务器名为“.”的时候也会报错.   解决方法,暂时使用S ...

  7. c# tcp 服务客户端

    session connection protobuf-net

  8. Test 3.27 T1 立方体大作战

    Description ​ 一个叫做立方体大作战的游戏风靡整个 Byteotia.这个游戏的规则是相当复杂的,所以我们只介绍他的简单规则:给定玩家一个有 2n 个元素的栈,元素一个叠一个地放置.这些元 ...

  9. iOS----实现scrollView或者scrollView的子类下拉图片放大的效果

    代码是通过Tableview来说明的,用在其他情况下同样适用 - (void)viewDidLoad { [super viewDidLoad]; _imageview = [[UIImageView ...

  10. css3新增的属性 - 分享

    CSS3新增属性   一.transform变换效果 CSS3 提供了元素变形效果,也叫做变换.它可以将元素实现旋转.缩放和平移的功能. 属性有两个:transform 和 transform-ori ...