[Critical Path] [Topological Sort + Inverse Topological Sort] [Turn]

https://blog.csdn.net/haskei/article/details/53749380

The specific algorithm is described as follows:
1. Enter e arcs <j,k> to establish the storage structure of the AOE-net.
2. Topological sort, and get ve[]. Starting from the source point V0, let ve[0]=0, and find the earliest occurrence time ve[i] of the remaining vertices according to the topological order. If the number of vertices in the obtained topologically ordered sequence is less than the number n of vertices in the network, it means that there is a loop in the network, and the critical path cannot be found, and the algorithm terminates; otherwise, go to step 3.
3. Topological inversion, to obtain vl[]. Starting from the sink Vn, let vl[n-1] = ve[n-1], and find the latest occurrence time vl[i] of the remaining vertices according to the inverse topological order.
4. Find the critical path. According to the ve and vl values ​​of each vertex, find the earliest start time e(s) and latest start time l(s) of each arc s. An arc is a critical activity if it satisfies the condition e(s) = l(s).

In order to calculate the vl value of each vertex in the order of the reverse topological sorted sequence, it is necessary to record the topological sorted sequence obtained in the topological sorting process, which requires adding a stack in the topological sorting algorithm to record the topology For an ordered sequence, after calculating the ve value of each vertex, it is an inverse topological ordered sequence from the top of the stack to the bottom of the stack.

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<string>
  6 #include<cstdlib>
  7 #include<stack> 
  8 
  9 using namespace std;
 10 
 11 typedef long long ll;
 12 const int maxm = 20;
 13 const int maxn = 100;
 14 const int inf = 0x3f3f3f3f;
 15  struct node {
 16      int x, y, w;
 17      int next;
 18  };
 19  node edge[maxm];
 20  int n, m;
 21  int head[maxn];
 22  // e represents the earliest time the activity started , l The latest start time of the activity, the earliest time of the ve[i] event, the latest time of the vl[i] event, the indegree[i] vertex indegree 
 23  // This place does not need to be e, l respectively The array is opened, because in the end it is just an assignment, and then compare whether the two numbers are equal. There is no need to open the array. If you don't understand, you can see the following code 
24  int e, l, ve[maxn], vl[maxn], indegree [maxn];
 25 stack< int > s, t; // s represents topological sorting in reverse order, t represents a stack with zero in-degree, which stores points with zero in-degree 
26  
27 int TopologicalSort() {
 28     int i, cnt = 0;
 29     for(i=1; i<=n; ++i) //入度为零的点入栈 
 30         if(!indegree[i]) {
 31             t.push(i);
 32             ++cnt;
 33             //printf("%d ", i);
 34         }
 35     while(!t.empty()) {
 36         int a = t.top();
 37         s.push(a);
 38         //printf("%d ", a);
 39         t.pop();
40          // Remove the edge connected to the point with zero in-degree, and subtract one from the in-degree of the corresponding end point. 
41          int k = head[a];
 42          while (k != - 1 ) {
 43              if (!--indegree [edge[k].y]) { // After the degree of the end point is decremented by one, if the degree is zero, push 
44                  t.push(edge[k].y);
 45                  ++ cnt;
 46                  // printf(" %d ", edge[k].y); 
47              }
 48              if (ve[edge[k].y] < ve[a] + edge[k].w) // Positive topological sort to find the earliest time of the event ve[i], the longest path to edge[k].y 
49                  ve[edge[k].y] = ve[a] + edge[k].w;
 50             k = edge[k].next;    
 51         }
 52     }
 53     if(cnt < n) 
 54         return 0;
 55     return 1;
 56 }
 57 
 58 
 59 int main()
 60 {
 61     int i;
 62     memset(head, -1, sizeof(head));
 63     scanf("%d%d", &n, &m);
 64     
 65     //建立邻接表 
 66     for (i= 1 ; i<=m; ++ i) {
 67          scanf( " %d%d%d " , &edge[i].x, &edge[i].y, & edge[i].w) ;
 68          ++indegree[edge[i].y];   // Add one to the indegree of the end point 
69          edge[i].next = head[edge[i].x];
 70          head[edge[i].x] = i;
 71      }
 72      
73      if (TopologicalSort() == 0 ) { // The problem of ve has been solved in the TopologicalSort() function 
74          printf( " There is no critical path, there is a loop\n " );
 75          return 0 ;
 76      }
 77      
78      memset(vl, inf, sizeof (vl));
 79      vl[n] = ve[n]; // The latest event of the last event is equal to the earliest occurrence time, because it is the last event That is to say, after the project is finished, there will be nothing to do in the future. 
80      while (!s.empty()) { // Inverse topological sorting to find vl[i] 
81          int a = s.top();
 82          s. pop();
 83          int k = head[a];
 84          while (k != - 1 ) {
 85              if (vl[a] > vl[edge[k].y] - edge[k].w) {
 86                  vl[a] = vl[edge[k].y] -edge[k].w;
 87              }
 88              k = edge[k].next;
 89          }
 90      }
 91      printf( " \nKey activities (this activity cannot be postponed) are:\n " );
 92      for (i= 1 ; i<=n; ++ i) { 
 93          int k = head[i];
 94          while (k != - 1 ) {
 95              e = ve[i]; // The starting point of the edge represents things, the The earliest occurrence time of the activity represented by the edge is equal to the earliest occurrence time of the event represented by the starting point 
 96              // The latest occurrence time of the activity 
97              l = vl[edge[k].y] - edge[k].w;
 98             if(l == e)
 99                 printf("%d %d %d\n", i, edge[k].y, edge[k].w);
100             k = edge[k].next;
101         }
102     }
103     return 0;
104 }
105 /*
106 9 11
107 1 2 6
108 1 3 4
109 1 4 5
110 2 5 1
111 3 5 1
112 4 6 2
113 5 7 9
114 5 8 7
115 6 8 4
116 7 9 2
117 8 9 4
118 */

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325689357&siteId=291194637