2019年9月8日秋季PAT甲級題解-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​​) and N​e​​ (≤10^​5​​), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to N​v​​.

Then N​e​​ 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​​ 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的題(也就是像1087那種套個模板就行的)”,但是不用恐懼,只要仔細分析一下:

題目給的輸入的最後一個序列 3 2 1 5 4爲什麼不行呢?

因爲在第3個數(即1)的時候,起點3到點1的距離d[1]爲3(點3->點2->點1),但是最短路徑爲1(點3->點4).

也就是說給出的序列是Dijkstra序列的關鍵在於,序列的第i個數x的d[x]是等於當時求到的最短路徑。

如果是的話,在後面的一步就要以x爲u來鬆弛了。

與題目同樣的圖例,我再寫三個非常相似的輸入用例:

5 7
1 2 2
1 5 1
2 3 1
2 4 1
2 5 2
3 5 1
3 4 1
3
3 2 5 4 1
3 2 5 1 4
3 2 1 5 4
正確代碼的測試輸出是:
s[i] u minn d[s[i]]
  3  3   0   0
  2  2   1   1
  5  4   1   1
  4  4   1   1
  1  1   2   2
Yes
s[i] u minn d[s[i]]
  3  3   0   0
  2  2   1   1
  5  4   1   1
  1  4   1   2
No
s[i] u minn d[s[i]]
  3  3   0   0
  2  2   1   1
  1  4   1   3
No
 

滿分代碼

//得分30,滿分30
#include<iostream>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1005;
int d[maxn],s[maxn],G[maxn][maxn];
bool visit[maxn];
int main(){
    int n,m,a,b,c,k;
    scanf("%d %d\n",&n,&m);
    fill(G[0],G[0]+maxn*maxn,INF);
    while(m--){
        scanf("%d %d %d\n",&a,&b,&c);
        G[a][b]=G[b][a]=c;
    }
    scanf("%d\n",&k);
    while(k--){
        m=n;
        for(int i=1;i<=n;i++){
            scanf("%d",&s[i]);
        }
        bool f=true;
        int start=s[1];
        fill(visit,visit+maxn,false);
        fill(d,d+maxn,INF);
        d[start]=0;
        for(int i=1;i<=n;i++){
            int u = -1, minn = INF;
            for (int j = 1; j <= n; j++) {
                if (!visit[j] && d[j] < minn) {
                    u = j;
                    minn = d[j];
                }
            }
            if (u == -1 || d[u] != d[s[i]]){
                f=false;
                continue;
            }
            u = s[i];
            visit[u]=true;
            for(int v=1;v<=n;v++){
                if(!visit[v] && G[u][v]!=INF && G[u][v]+d[u]<d[v]){
                    d[v]=G[u][v]+d[u];
                }
            }
        }
        if(!f)printf("No\n");
        else printf("Yes\n");
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章