PAT(甲级)2019年秋季考试 7-4 Dijkstra Sequence (C++)

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 ( ≤ 1 0 3 ) N_v(≤10^3) Nv(103) and N e ( ≤ 1 0 5 ) 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 N_v Nv.
Then N e 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 ≤100 100) of the edge. It is guaranteed that the given graph is connected.

Finally the number of queries, K K K, is given as a positive integer no larger than 100, followed by K K K lines of sequences, each contains a permutationof the N v 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

Caution:

最初一个一个判断新加入的点是不是距离最短的,但是超时了(但是结束后我查到的答案大部分都是这样做的,竟然也AC了),所以就优化了一下,先输入所有的path,然后按照 path 的第一个数字进行排序,这样就会使得以同一个点为起点的路径会放在一起,然后对这个点做一次 Dijkstra,判断后加入的点距起点的最短距离 dis 不会小于前面加入的点。

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-09-09 14:24:09
// All rights reserved.

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;

int edges[1010][1010];
vector<vector<int>> edges2;
int dis[1010];
int nv, ne;

struct Query{
    vector<int> path;
    int pos;

    Query(){
        path.resize(nv);
        pos = -1;
    }
    ~Query(){}
};

bool cmp(Query &a, Query &b){
    return a.path[0] < b.path[0];
}

int main(){
    
    scanf("%d %d", &nv, &ne);
    edges2.resize(nv + 1);

    for(int i = 0; i < ne; ++i){
        int v1, v2;
        scanf("%d %d", &v1, &v2);

        scanf("%d", &edges[v1][v2]);
        edges[v2][v1] = edges[v1][v2];

        edges2[v1].push_back(v2);
        edges2[v2].push_back(v1);
    }

    int k;
    scanf("%d", &k);

    vector<Query> queries(k);

    for(int i = 0; i < k; ++i){
        
        for(int j = 0; j < nv; ++j) scanf("%d", &queries[i].path[j]);
        queries[i].pos = i;
    }


    sort(queries.begin(), queries.end(), cmp);
    unordered_map<int, bool> ans;
    bool vis[1010];
    
    for(int i = 0; i < k; ++i){
        ans[queries[i].pos] = true;

        if(i == 0 || queries[i].path[0] != queries[i - 1].path[0]){
            
            fill(dis, dis + 1010, 2147483647);
            fill(vis, vis + 1010, false);
            dis[queries[i].path[0]] = 0;

            for(int j = 0; j < nv; ++j){
                int u = -1, minDis = 2147483647;

                for(int k = 0; k < nv; ++k){
                    if(vis[queries[i].path[k]]) continue;
                    if(dis[queries[i].path[k]] < minDis){
                        minDis = dis[queries[i].path[k]];
                        u = queries[i].path[k];
                    }
                }

                if(u == -1) break;

                for(int x = 0; x < edges2[u].size(); ++x){
                    int v = edges2[u][x];

                    if(!vis[v] && dis[v] > dis[u] + edges[u][v]) dis[v] = dis[u] + edges[u][v];
                }
                vis[u] = true;
            }
        }

        for(int j = 1; j < nv; ++j){
            if(dis[queries[i].path[j]] < dis[queries[i].path[j - 1]]){
                ans[queries[i].pos] = false;
                break;
            }
        }
    }

    for(int i = 0; i < k; ++i){
        if(ans[i]) printf("Yes\n");
        else printf("No\n");
    }

    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

负反馈循环

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值