PAT(甲級)2019年秋季考試 7-4 Dijkstra Sequence

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.this

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​​.spa

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.code

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.blog

All the inputs in a line are separated by a space.three

Output Specification:

For each of the K sequences, print in a line Yes if it is a Dijkstra sequence, or No if not.ci

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

題目限制:

image.png

題目大意:

給定一個Nv個頂點,Ne條邊的圖,K個查詢,每一次查詢給定一個Nv個長度的頂點序列,判斷該頂點序列是不是Dijkstra sequence,若是是輸出Yes, 不然輸出No。input

算法思路:

首先使用query數組存儲每一次查詢的頂點序列,那麼query[0]即爲源點,使用常規的Dijkstra算法求解每個點到query[0]的最短距離,並在每次在爲選擇的頂點集合中選擇距離源點最短的距離的時候,將該距離添加進chosenDist數組中,這樣,chosenDist數組就保存了每一次選擇的最短距離,而後再遍歷query數組,判斷dis[query[j]]chosenDist[j]是否所有相等,若是是輸出Yes,不然輸出No。這麼作之因此可行,是由於Dijkstra算法不變的量就是第k次選擇出來的最短距離是不變的,不管第k個頂點選擇哪一個。string

提交結果:

image.png

AC代碼:

#include<cstdio>
#include<vector>
#include<cstring>

using namespace std;

int Nv,Ne;// 頂點數和邊數
int G[1005][1005];
int dis[1005];// 保存每個節點的最短距離
bool visited[1005];
vector<int> chosenDist;// 保存每一次選擇的最短距離

void Dijkstra(int start){
    fill(dis,dis+1005,0x3fffffff);
    dis[start] = 0;
    for(int i=0;i<Nv;++i){
        int min_dis = 0x3fffff;
        int min_index = -1;
        for(int j=1;j<=Nv;++j){
            if(!visited[j]&&min_dis>dis[j]){
                min_dis = dis[j];
                min_index = j;
            }
        }
        if(min_index==-1) return;
        visited[min_index] = true;
        // 將每次選擇的最短距離添加到chosenDist中。
        chosenDist.push_back(min_dis);
        for(int j=1;j<=Nv;++j){
            if(!visited[j]&&G[min_index][j]!=0&&dis[min_index]+G[min_index][j]<dis[j]){
                dis[j] = dis[min_index]+G[min_index][j];
            }
        }
    }
}

int main(){
    scanf("%d %d",&Nv,&Ne);
    for(int i=0;i<Ne;++i){
        int a,b,d;
        scanf("%d %d %d",&a,&b,&d);
        G[a][b] = G[b][a] = d;
    }
    int K;
    scanf("%d",&K);
    for(int i=0;i<K;++i){
        int query[Nv+1];
        for(int j=0;j<Nv;++j){
            scanf("%d",&query[j]);
        }
        chosenDist.clear();
        memset(visited,0,sizeof(visited));
        Dijkstra(query[0]);
        bool isTrue = true;
        for(int j=0;j<Nv;++j){
            if(dis[query[j]]!=chosenDist[j]){
                isTrue = false;
                break;
            }
        }
        if(isTrue){
            printf("Yes\n");
        }else{
            printf("No\n");
        }
    }
    return 0;
}
相關文章
相關標籤/搜索