Prerequisites
1. Because the entire project has only one start point and one end point, under normal circumstances (no ring), there is only one inbound point and one outbound point of 0 in the network.
2. Unlike the AOV network, some activities in the AOE network can be performed in parallel. Therefore, the shortest time for project completion is the length of the longest path from the start point to the completion point. The path with the longest length is the Key Path.
Additional reading
I will not repeat the detailed proof process in Yan Weimin's data structure. Here I will only provide the specific implementation code.
[Cpp]
# Include <iostream>
# Include <cstdlib>
# Include <cstdio>
# Include <cstring>
# Include <queue>
# Include <stack>
Using namespace std;
Const int MAXN = 1010;
Const int MAXM = 10010;
Struct Edge
{
Int v, w;
Int id;
Int next;
} Edge [MAXM];
Int n, m;
Int cnt;
Int first [MAXN], topo [MAXN];
Int ind [MAXN], outd [MAXN];
Int tot;
Int Ee [MAXN], El [MAXN], E [MAXN], L [MAXN];
/* Ee indicates the earliest possible event time, and El indicates the latest event time allowed */
/* E indicates the earliest possible time of the activity, and L indicates the latest allowed time of the activity */
Void init ()
{
Cnt = 0;
Tot = 0;
Memset (first,-1, sizeof (first ));
Memset (ind, 0, sizeof (ind ));
Memset (outd, 0, sizeof (outd ));
Memset (Ee, 0, sizeof (Ee ));
Memset (E, 0, sizeof (E ));
Memset (L, 0, sizeof (L ));
}
Void read_graph (int u, int v, int w, int id)
{
Edge [cnt]. v = v, edge [cnt]. w = w, edge [cnt]. id = id;
Edge [cnt]. next = first [u], first [u] = cnt ++;
}
Void toposort () // topological sorting
{
Queue <int> q;
For (int I = 0; I <n; I ++) if (! Ind [I]) q. push (I );
While (! Q. empty ())
{
Int x = q. front (); q. pop ();
Topo [++ tot] = x;
For (int e = first [x]; e! =-1; e = edge [e]. next)
{
Int v = edge [e]. v, w = edge [e]. w;
If (-- ind [v] = 0) q. push (v );
If (Ee [v] <Ee [x] + w) // obtain the Ee values of each vertex.
{
Ee [v] = Ee [x] + w;
}
}
}
}
Void CriticalPath ()
{
Toposort ();
Int top = tot;
For (int I = 0; I <n; I ++) El [I] = Ee [n-1]; // the latest time when a vertex event is initialized.
While (top) // calculate the vertex El value in reverse topological sorting
{
Int x = topo [top --];
For (int e = first [x]; e! =-1; e = edge [e]. next)
{
Int v = edge [e]. v, w = edge [e]. w;
If (El [x]> El [v]-w)
{
El [x] = El [v]-w;
}
}
}
For (int u = 0; u <n; u ++) // obtain E, L key activities
{
For (int e = first [u]; e! =-1; e = edge [e]. next)
{
Int v = edge [e]. v, id = edge [e]. id, w = edge [e]. w; // id indicates the active label
E [id] = Ee [u], L [id] = El [v]-w;
If (E [id] = L [id]) // equality must be a key activity.
{
Printf ("a % d: % d-> % d \ n", id, u, v );
}
}
}
}
Void read_case ()
{
Init ();
For (int I = 1; I <= m; I ++)
{
Int u, v, w;
Scanf ("% d", & u, & v, & w );
Read_graph (u, v, w, I); // read_graph
Outd [u] ++, ind [v] ++;
}
}
Int main ()
{
While (~ Scanf ("% d", & n, & m ))
{
Read_case ();
Printf ("\ nThe Critical activities are: \ n ");
CriticalPath ();
}
Return 0;
}
/*
Input:
9 11
0 1 6
0 2 4
0 3 5
1 4 1
2 4 5
3 5 2
4 6 9
4 7 7
5 7 4
6 8 2
7 8 4
*/
/*
Output:
A2: 0-> 2 // a1: 0-> 1
A5: 2-> 4 // a4: 1-> 4
A8: 4-> 7
A7: 4-> 6
A10: 6-> 8
A11: 7-> 8
*/