PAT

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

Then 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 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

题目大意:

给出一个图和几个序列(每个序列的第一个值就是起点),判断序列是否是Dijkstra序列,如果是,则输出“Yes”,否则输出“No”。

思路

Dijkstra序列:在执行Dijkstra算法求最短路径的时候,每次选择最短距离所到达的结点所组成的一个序列(简单说就是求最短路径时每次访问结点组成的一个序列);因为一个图中可能到达其他几个结点的最短距离一样,所以访问结点的顺序可能也不一样,就可能导致出现多中访问顺序。

这道题的任务其实可以简化为:

  • 按照给出的Dijkstra序列是否能求出最短路径
  • 可以这样判断:Dijkstra算法每一步不管选哪一个结点,对于最优解的每一步距离是相同的

    • 按照普遍的Dijkstra求最短路径的过程中,在寻找最短距离所到达的结点时,与给出的序列在当前那一步作比较:判断两者的最短距离是否相同,若相同则进行下一轮比较,否则就直接返回“false”

AC代码

#include<bits/stdc++.h>
using namespace std;

int n, e, k; // vertices, edges, k sequences
vector<int> dst;
vector<bool> visit;
const int maxV = 1010;
vector<pair<int, int> > adj[maxV]; // first: end; second: weight; index: start;

bool dijkstra(int start, vector<int> &seq);

int main() {
    scanf("%d%d", &n, &e);
    int s, d, w;
    for (int i = 0; i < e; ++i) {
        cin >> s >> d >> w;
        adj[s].emplace_back(d, w);
        adj[d].emplace_back(s, w);
    }
    scanf("%d", &k);
    vector<int> seq(n);
    for (int i = 0; i < k; ++i) {
        for (int j = 0; j < n; ++j) {
            scanf("%d", &seq[j]);
        }
        if (dijkstra(seq[0], seq)) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

bool dijkstra(int start, vector<int> &seq) {
    dst = vector<int>(n + 1, INT_MAX);
    visit = vector<bool>(n + 1, false);
    dst[start] = 0;
    int cnt = 0;
    for (int i = 1; i <= n; ++i) {
        int u = -1, MIN = INT_MAX;
        for (int j = 1; j <= n; ++j) {
            if (!visit[j] && dst[j] < MIN) {
                u = j;
                MIN = dst[j];
            }
        }
        // 与给出的序列作比较
        if (u != -1 && MIN == dst[seq[cnt]]) {
            ++cnt;
        } else return false;
        visit[u] = true;
        for (int j = 0; j < adj[u].size(); ++j) {
            int v = adj[u][j].first;
            int w = adj[u][j].second;
            if (!visit[v] && dst[u] + w < dst[v]) {
                dst[v] = dst[u] + w;
            }
        }
    }
    return true;
}
This is just a placeholder img.