This part is related to engineering, that is, when AOV, AOE is very complicated, can display this part of the value - simple words, manual is more than the program, the input data is handed out. . I have no examples, I always give me a feeling that I have nothing to do, I have finished writing the program. Compared with the front, this is a little profession, in turn, not everyone is interested, don't want to see it.
Ready to work
The event network mainly has two algorithms, topologies, and key paths, the latter for the former. In addition, a "algorithm class" is applied to the algorithm, and the figure is bound to the algorithm.
#include "network.h"
#define iterator list :: Edge> :: Iterator
#define begin (i) g-> data.vertices [i] .e-> begin ()
#define end (i) g-> data.vertices [i] .E-> end ()
Struct CRIACT
{
CRIACT () {}
CRIACT (Int Source, Int Dest): s (Source), D (DEST) {}
Int S, D;
}
Template
Class ActivityNetwork
{
PUBLIC:
ActivityNetwork (Network
{
Count = new int [n]; result = new int [n];
}
~ ActivityNetwork ()
{
DELETE [] Count; delete [] Result
}
Const Vector
Const int * OUT;
Private:
void initialize ()
{
For (int J = 0; j For (int i = 0; i { For (Iteerator Iter = Begin (i); item! = end (i); it ) count [ney-> vid] ; } OUT = Result; } Network Vector INT N, * Count, * Result } Because AOV and AOE are too much (imagine how much more, the event is the chicken fry garlic skin), so the storage structure directly selects the adjacent table. And in order to reflect the advantages of neighboring tables, you need to directly operate private data, so declare this class as a Link class and a NetWork class, and due to this class, you need a pre-view statement. details as follows: Template Template {Friend Class ActivityNetwork {Friend Class ActivityNetwork Topology sort This algorithm is excellent, avoiding the scanning of the vertex of the already row, in addition, the method of playing the count array in the counting array is also very clever. Explanation of the algorithm See the relevant textbooks and will not be described again. Bool Toposort () { Initialize (); int i, top = -1; For (i = 0; i For (i = 0; i { IF (TOP == -1) Return False; Result [i] = top; TOP = count [TOP]; ITerator it = begin (result [i]); it! = end (result [i]); iTer ) IF (! - count [item-> vid]) {count [item-> vid] = top; TOP = ITER-> VID; } Return True; } Since the public data member OUT and Private data member Result points to the same array, the outside of the class can be sorted by OUT, but cannot be changed (of course, there is no way to change the const data). Below is the test program, the data is from the Yin version: #include Using namespace std; #include "activity" ActivityNetwork.h " int main () { Network A.insertv (0); A.Nsertv (1); A.insertv (2); A.insertv (3); A.Nsertv (4); A.Nsertv (5); A.Nserte (0, 3, 1); A. INSERTE (0, 1, 1); A.NSERTE (1, 5, 1); A.NSERTE (2, 1, 1); A.NSerte (2, 5, 1); A.NSerte (4, 0, 1); A.Nserte (4, 1, 1); A.NSERTE (4, 5, 1); ActivityNetwork IF (b.toposort ()) for (int i = 0; i Return 0; } Critical Path With the result of topology, this app is more written, those so-called "skills" are not available, the procedures are all straightforward, and the algorithm will refer to the textbook. BOOL CRIPATH () { IF (! Toposort ()) Return False; INT I; Iterator Iter; Ca.clear (); Dist * ve = new dist (n]; dist * VL = new dist [n]; // VE earliest start time, VL latest start time For (i = 0; i For (item = begin); item! = end (result [i]); iTer ) IF (ve [result [i]] iter-> cost> ve [iter-> vid]) ve [i>>]] = ve [result [i]] iter-> cost; For (i = 0; i For (i = n - 2; i> = 0; i -) // calculates VL in reverse topology sequence For (item = begin); item! = end (result [i]); iTer ) IF (VL [item-> vid] -iter-> cost For (i = 0; i For (iter = begin (i); ore! = end (i); it ) IF (VE [i] == VL [ITER-> VID] - ITER-> COST) CA.PUSH_BACK (CRIACT (I, ITER-> VID)); Return True; } The same can be obtained by OutCriact outside, which is a const reference. The following test procedures, data from Yin Edition: #include Using namespace std; #include "activity" ActivityNetwork.h " int main () { Network A.insertv (0); a.insertv (1); a.insertv (2); a.insertv (3); A.insertv (4); A.insertv (5); A.insertv (6); A. Insertv (7); A.Nsertv (8); A.NSerte (0, 1, 6); A. INSERTE (0, 2, 4); A.NSERTE (0, 3, 5); A.NSERTE (1, 4, 1); A. INSERTE (2, 4, 1); A. INSERTE (3, 5, 2); A.NSerte (4, 6, 9); A. INSERTE (4, 7, 7); A.NSERTE (5, 7, 4); A.NSerte (6, 8, 2); A. INSERTE (7, 8, 4); ActivityNetwork IF (b.cripath ()) For (int J = 0; j Cout << '<' << A.GetV (B.outcriact [J] .s) << ',' << A.Getv (B.outCriact [J] .d) << '> <<' ' ; Return 0; }