关于算法-数据结构:PAT甲级2019年秋季考试-74-Dijkstra-Sequence

2次阅读

共计 3082 个字符,预计需要花费 8 分钟才能阅读完成。

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​​) $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

题目限度:

题目粗心:

给定一个 Nv 个顶点,Ne 条边的图,K 个查问,每一次查问给定一个 Nv 个长度的顶点序列,判断该顶点序列是否是 Dijkstra sequence,如果是输入 Yes, 否则输入 No。

算法思路:

首先应用 query 数组存储每一次查问的顶点序列,那么 query[0] 即为源点,应用惯例的 Dijkstra 算法求解每一个点到 query[0] 的最短距离,并在每次在为抉择的顶点汇合中抉择间隔源点最短的间隔的时候,将该间隔增加进 chosenDist 数组中,这样,chosenDist数组就保留了每一次抉择的最短距离,而后再遍历 query 数组,判断 dis[query[j]]chosenDist[j]是否全副相等,如果是输入 Yes,否则输入 No。这么做之所以可行,是因为 Dijkstra 算法不变的量就是第 k 次抉择进去的最短距离是不变的,无论第 k 个顶点抉择哪个。

提交后果:

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;
}
正文完
 0