http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2498&cid=1304 Topic Description
A non-circular, forward graph is called a non-circular graph (directed acyclic graphdag
aoe (activity on Edge) Net: As the name implies, use the edge to represent the active net, of course it is dag. Unlike aov, activities are represented on the edge as shown in:
&NBS P
as shown above, there are 11 activities ( 11 edges), 9 events (
critical Path: The length of the longest path from the start point to the completion point. The length of the path is the time spent on the edge. As shown, 1 to 2 to 5 to 7 to 18.
InputThere are multiple sets of data, guaranteed to be no more than 10 groups, guaranteed to have only one source and sink point. Enter a top point N (2<=n<=10000), number of sides m (1<=m <=50000), next m line, input start SV, end ev, weight value w (1<=sv, Ev<=n,sv! = ev,1<=w <=20). Data Assurance diagram connectivity. OutputThe weight of the critical path and the path on the critical path from the source point (if there are more than one, output the smallest dictionary order). Sample input
9 111 2 61 3 41 4 52 5 13 5 14 6 25 7 95 8 76 8 48 9 47 9 2
Sample output
181 22 55) 77 9
Topic Analysis:
It is up to the test instructions to know that this is the longest path required from the starting point S to the end point E, because there are 10,000 points, 50,000 edges, and the shortest path with SPFA, but one problem is that the dictionary order of the paths is minimal.
If you're going to Jiantu, For example: If the end point is 6 now, both 2 and 3 will be able to reach the maximum and the same value for 6. We will choose 2 when processing, but still 2 this path is not optimal, but 3 is optimal.
So we see the picture in reverse, ask for a shortest circuit and then pour the output is good.
How to resolve:
In reverse order, when the relaxation (U,V), encounter the same situation, try to make u smaller, then the final result is the smallest dictionary sequence.
For the longest path, set dis to-inf,dis[s] = 0
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#defineINF 0x3f3f3f3f#defineMaxx 200001using namespacestd;structnode{intX,y,c,next;} Eg[maxx];intn,m,flag,tt,pre[20002],ru[20002],ch[20002],dis[20002],v[20002],head[20002];voidinit () {TT=0; Flag=0; memset (Ru,0,sizeof(RU)); memset (CH,0,sizeof(CH)); memset (Head,-1,sizeof(head));}voidAddintXxintYyintZZ) {eg[tt].x=xx; Eg[tt].y=yy; EG[TT].C=ZZ; Eg[tt].next=HEAD[XX]; HEAD[XX]=tt++;}voidSPFA (intSinte) { intFF; V[s]=1; Queue<int>Q; while(!q.empty ()) Q.pop (); for(intI=1; i<=n; i++) {Dis[i]=-inf; Pre[i]=-1; V[i]=0; } Dis[s]=0; Q.push (s); while(!Q.empty ()) {FF=Q.front (); Q.pop (); V[FF]=0; for(intI=HEAD[FF]; i!=-1; I=Eg[i].next) { intvv=eg[i].y; intw=eg[i].c; if(dis[ff]+w>DIS[VV]) {PRE[VV]=FF;//dis[vv]=dis[ff]+W; if(v[vv]==0) {Q.push (vv); V[VV]=1; } } Else if(dis[ff]+w==dis[vv]&&pre[vv]>FF) {PRE[VV]=FF; if(v[vv]==0) {V[VV]=1; Q.push (VV); } }}} cout<<dis[e]<<Endl; intt=e; while(t!=s) {printf ("%d%d\n", t,pre[t]); T=Pre[t]; }}intMain () {intXx,yy,zz,ru,ch; while(SCANF ("%d%d", &n,&m)! =EOF) {init (); for(intI=0; i<m; i++) {scanf ("%d%d%d",&xx,&yy,&ZZ); Add (YY,XX,ZZ); RU[XX]++; CH[YY]++; } for(intI=1; i<=n; i++) { if(ru[i]==0) RU=i; if(ch[i]==0) CH=i; } SPFA (Ru,ch); } return 0;}
Sdut AoE Online Critical path (spfa+ forward star)