Network flow/Charge flow
Reference the following:
Lyd
First, the interval endpoint discretization, set the original value I discretization after the label is C[i]. After this discretization, the whole axis is divided into a section of a small interval.
1. Build s and T, from S to discretized the first point of the capacity K, the side of the cost 0. The last point after discretization to the T-even capacity K, the side of the cost 0.
2. An edge with a capacity of k and a cost of 0 between the discretized neighboring points (from I to i+1).
3. The input interval is from the left end of the discretization to the right endpoint with a capacity of 1, the side of the cost W.
Feel Good God ...
In fact should be as long as the source point to the first point of traffic limit to K should be able to ... This idea is quite ingenious ... Limit the number of k out of each place.
In addition, this is the maximum cost of the maximum flow, so according to the above method of mapping need to change the conditions of pre-treatment and augmentation, or is to build the plan when all the cost of negative, and then the final answer negative.
1 Source Code2Problem:3680user:sdfzyhy3 memory:752k time:563ms4language:g++result:accepted5 6 Source Code7 8 //Bzoj 36809#include <cmath>Ten#include <vector> One#include <cstdio> A#include <cstring> -#include <cstdlib> -#include <iostream> the#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) - #defineD (i,j,n) for (int i=j;i>=n;--i) + #definePB Push_back - #defineCC (A, B) memset (A,b,sizeof (a)) + using namespacestd; A intGetint () { at intv=0, sign=1;CharCh=GetChar (); - while(!isdigit (CH)) {if(ch=='-') sign=-1; Ch=GetChar ();} - while(IsDigit (CH)) {v=v*Ten+ch-'0'; Ch=GetChar ();} - returnv*Sign ; - } - Const intn=510, m=100000, inf=~0u>>2; in Const Doubleeps=1e-8; - /*******************template********************/ to intN,m,k,ans,a[n],b[n],c[n],w[n]; + structedge{int from, To,v,c;}; - structnet{ the Edge E[m]; * inthead[n],next[m],cnt; $ voidInsintXintYintZintc) {Panax Notoginsenge[++cnt]=(Edge) {x,y,z,c}; -NEXT[CNT]=HEAD[X]; head[x]=CNT; the } + voidAddintXintYintZintc) { AIns (x,y,z,c); Ins (y,x,0,-c); the } + intS,t,d[n],q[m], from[N]; - BOOLInq[n]; $ BOOLSPFA () { $ intL=0, r=-1; -F (i,s,t) d[i]=INF; -d[s]=0; Q[++r]=s; inq[s]=1; the while(l<=R) { - intX=q[l++]; inq[x]=0;Wuyi for(intI=head[x];i;i=Next[i]) the if(E[I].V && d[x]+e[i].c<D[e[i].to]) { -d[e[i].to]=d[x]+e[i].c; Wu from[e[i].to]=i; - if(!Inq[e[i].to]) { Aboutq[++r]=e[i].to; $inq[e[i].to]=1; - } - } - } A returnd[t]!=INF; + } the voidmcf () { - intx=INF; $ for(intI= from[t];i;i= from[E[i]. from]) thex=min (x,e[i].v); the for(intI= from[t];i;i= from[E[i]. from]){ thee[i].v-=x; thee[i^1].v+=x; - } inans+=x*D[t]; the } the voidinit () { AboutN=getint (); k=getint (); theCnt=1; ans=0; theMemset (Head,0,sizeofhead); the intx, y; +F (I,1, N) { -a[i]=c[(i<<1)-1]=getint (); theb[i]=c[i<<1]=getint ();Bayiw[i]=getint (); the } theSort (c+1, c+n*2+1); - intNum=unique (c+1, c+n*2+1)-c-1; -s=0; t=num+1; theF (I,0, num) Add (i,i+1K0); theF (I,1, N) { theA[i]=lower_bound (c+1, c+num+1, A[i])-C; theB[i]=lower_bound (c+1, c+num+1, B[i])-C; -Add (A[i],b[i],1,-w[i]); the } the while(SPFA ()) MCF (); theprintf"%d\n",-ans);94 } the }g1; the intMain () { the #ifndef Online_judge98Freopen ("Input.txt","R", stdin); About //freopen ("Output.txt", "w", stdout); - #endif101 intt=getint ();102 while(t--) G1.init ();103 return 0;104}
View Code
"POJ" "3680" intervals