PAT甲级【2019年9月考题】——A1164 DijkstraSequence【30】

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[1010][1010] = { 0 }, path[1010], check[1010];
bool Dijkstra(int x)
{
    vector<bool>visit(n + 1, false);
    fill(path, path + n + 1, INT32_MAX);
    path[x] = 0;
    for (int t = 1; t <= n; ++t)
    {
        int index = -1, minDis = INT32_MAX;
        for (int i = 1; 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 = 1; u <= n; ++u)
            if (visit[u] == false && v[index][u] > 0)
                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 = 1; i <= n; ++i)
            cin >> check[i];
        if (Dijkstra(check[1]))
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值