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.

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

题目限制:

PAT(甲级)2019年秋季考试 7-4 Dijkstra Sequence_第1张图片

题目大意:

给定一个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个顶点选择哪个。

提交结果:

PAT(甲级)2019年秋季考试 7-4 Dijkstra Sequence_第2张图片

AC代码:

#include
#include
#include

using namespace std;

int Nv,Ne;// 顶点数和边数
int G[1005][1005];
int dis[1005];// 保存每一个节点的最短距离
bool visited[1005];
vector chosenDist;// 保存每一次选择的最短距离

void Dijkstra(int start){
    fill(dis,dis+1005,0x3fffffff);
    dis[start] = 0;
    for(int i=0;idis[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]

你可能感兴趣的:(算法-数据结构,c++)