Given a point-weighted Dag, find the longest path with a 0-node to a 0-node dimension
Turn the point right into Benquan (at the same time multi-source conversion into a single source): Edge u->v of the weight of w[v], so that the degree of 0 of the node weights will be omitted, a new point 0 into the degree of 0 points u connected to the edge, the weight is w[u], so that only 0 is the point of 0 degrees.
First, the topology is sorted, and then the longest path is obtained by using the features of the DAG topology ordering.
1#include <iostream>2#include <algorithm>3#include <cstring>4#include <cmath>5#include <cstdio>6 using namespacestd;7typedefLong LongLL;8 9template<classT>voidRead (T &x) {TenRegisterintc = GetChar (), F =1; x =0; One while(!isdigit (c)) {if(c = ='-') F =-1; c =GetChar ();} A while(IsDigit (c)) x = x*Ten+ (c& the), C =GetChar (); -X *=F; - } the Const intN =100005; - structedge{intV, W, next;} g[n* -]; - intN, M, S, Head[n], tot, ind[n], out[N], w[n]; LL Dis[n], ans =-123023423423; - voidToposort (ints) { + intTopo[n], cnt =0; -TOPO[++CNT] =0; + for(intK =0; K <= CNT; ++k) A for(inti = head[topo[k]]; I i =G[i].next) { at intv =g[i].v; -ind[v]--; - if(ind[v]==0) topo[++cnt] =v; - } -memset (DIS,0xc0,sizeof(dis)); Dis[s] =0; - for(intK =0; K <= CNT; ++k) in for(inti = head[topo[k]]; I i =G[i].next) { - intv = g[i].v, w =G[I].W; toDIS[V] = max (dis[topo[k]]+W, Dis[v]); + } - } the *InlinevoidAddintUintVintW) { $G[++tot].v=v, G[tot].w=w, g[tot].next=head[u];head[u]=tot;Panax Notoginsengind[v]++, out[u]++; - } the + intMainvoid){ A while(SCANF ("%d%d", &n, &m) = =2){ theMemset (Head,0,sizeof(head)); tot=0; Memset (Ind,0,sizeof(Ind)); Memset out,0,sizeof( out)); +Ans =0xc0c0c0c0c0c0c0c0; - for(inti =1; I <= N; ++i) read (W[i]); $ for(intU, V, i =1; I <= m; ++i) { $ Read (U), read (v); - Add (U, V, w[v]); - } the for(inti =1; I <= N; ++i) { - if(!ind[i]) Add (0, I,w[i]);Wuyi } the Toposort (s); - for(inti =1; I <= N; ++i) { Wu if(! out[i]) ans =Max (ans, dis[i]); - } Aboutcout << ans <<'\ n'; $ } - return 0; -}
View Code
[POJ3249] Test for Job [longest path on topological sort +dag]