Link:
Key activities
Ideas:
1, first through the queue and adjacency table to complete the topology sequencing:
All nodes A with a degree of 0 in the queue
Find all subsequent nodes of a in the adjacency table
Subsequent node penetration-1
If the subsequent node is in the degree of 0
Then the successor node is enqueued
2, the task scheduling is not feasible when the ring appears in the diagram:
Just decide if you want to queue n times
3, in the process of topological sequencing with the path array to save all (key activities) of the precursor node
Finally through the queue and path array
Traverse the Save all the edges that appear from the final activity in turn
Sort the output to
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector > #include <algorithm>using namespace std;struct node{int to; int w;}; struct a{//build structure Save all sides of the critical path easy to sort int A, b;} edge[4999];vector<node>w[105]; adjacency Table vector<int>path[105]; Save all precursor nodes for each point key activity int last[105]; Save the latest time to complete int degree[105]; Save each point in degrees int n;int cmp (A aa,a bb) {return AA.A<BB.A;} void print (int loc)//Save All sides of the critical path by queue {int s=0; queue<int>qq; int vis[105],head,i; memset (vis,0,sizeof (VIS)); Qq.push (Loc); while (!qq.empty ()) {Head=qq.front (); Qq.pop (); For (I=path[head].size () -1;i>=0;i--) {loc=path[head][i]; Edge[s].a=loc; Edge[s++].b=head; if (!vis[loc]) {vis[loc]=1; Qq.push (Loc); }}} sort (edge,edge+s,cmp); for (int i=0;i<s;i++) cout<<edge[i].a<< "and" <<EDGE[I].B<<ENDL;} void Top_sort ()//queue implementation topological sort {int i,head,loc,s=0,ans=0,ans_loc; queue<int>q; for (I=1; i<=n; i++) if (degree[i]==0) Q.push (i); while (!q.empty ()) {Head=q.front (); s++; N nodes are all queued to complete the topological sequencing otherwise formed ring ans=last[head]; Ans_loc=head; The last to enter the team is the final activity q.pop (); for (i=0; I<w[head].size (); i++) {loc=w[head][i].to; if (Last[head]+w[head][i].w>last[loc])//If this path takes longer {path[loc].clear (); Previously saved precursor key activity emptied LAST[LOC]=LAST[HEAD]+W[HEAD][I].W; Path[loc].push_back (head); Add current precursor key activity} else if (Last[head]+w[head][i].w==last[loc]) {PATH[LOC].PU Sh_back (head); Spend the same time-add} degree[loc]--; if (!degree[loc]) Q.push (Loc); }} if (S==n) cout<<ans<<endl; else {cout<<0<<endl; Return } print (Ans_loc); Start by identifying all the side return from the last key activity;} int main () {int m,i,j; int a,b,weight; scanf ("%d%d", &n,&m); for (I=1; i<=n; i++) {degree[i]=0; last[i]=0; } while (m--) {scanf ("%d%d%d", &a,&b,&weight); W[a].push_back (node) {b,weight}); degree[b]++; } top_sort (); return 0;}
PAT Key activity topology sequencing-critical path