PAT Grade [September 2019] --A1164 DijkstraSequence exam [30]

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

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


【statement】

  Since this question has not been on the official website PAT exam, there is no test set is only through a sample, if errors are found, correct me thank comments.

Solution:

  This question is difficult, first of all, it will be the starting point for each consultation Dijkstra, then asked to determine the order of the array is not from small to large distances from, so then is Dijstra, otherwise not.

  Of course, the process may be directly determined in Dijstra, i.e. the shortest distance selected each intermediate node that is not satisfying given node array is then continued. Direct false otherwise

  

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 int n, m, k, v[1010][1010] = { 0 }, path[1010], check[1010];
 6 bool Dijkstra(int x)
 7 {
 8     vector<bool>visit(n + 1, false);
 9     fill(path, path + n + 1, INT32_MAX);
10     path[x] = 0;
11     for (int t = 1; t <= n; ++t)
12     {
13         int index = -1, minDis = INT32_MAX;
14         for (int i = 1; i <= n; ++i)
15         {
16             if (visit[i] == false && minDis > path[i])
17             {
18                 minDis = path[i];
19                 index = i;
20             }
21         }
 22 is          IF (path [Check [T]] == minDis) Check index = [T]; // selected intermediate nodes in order to give the array 
23 is          the else  return  to false ;
 24          Visit [index] = to true ;
 25          for ( int = U . 1 ; U <= n-; ++ U)
 26 is              IF (Visit [U] == to false && V [index] [U]> 0 )
 27                  IF (path [U]> path [index] + V [index ] [U])
 28                      path [U] = path [index] + V [index] [U];
 29      }
 30      return true;
31 }
32 int main()
33 {
34     cin >> n >> m;
35     while (m--)
36     {
37         int a, b, c;
38         cin >> a >> b >> c;
39         v[a][b] = v[b][a] = c;
40     }
41     cin >> k;
42     while (k--)
43     {
44         for (int i = 1; i <= n; ++i)
45             cin >> check[i];
46         if (Dijkstra(check[1]))
47             printf("Yes\n");
48         else
49             printf("No\n");
50     }
51     return 0;
52 }

Guess you like

Origin www.cnblogs.com/zzw1024/p/11964374.html