Network flow/Charge flow
Orz too god ben This problem ...
I thought it was the same as intervals. Each number A[i] is equivalent to covering (a[i]-n,a[i]+n) this interval ... But this is wrong!! Just find a counter-example ... I have always been a positive solution ...
In fact, there is another problem with that idea: the length of n in the title refers to the original sequence of the given! And not the weight of the range! The problem is misunderstood ...
Look at the zyf of the solution, only to understand, to use the same way as the volunteers recruit to do; In addition, volunteers are recruited with an unlimited number of volunteers, but this problem can only be selected once per number, so the edge of the edge of the a[i] side of the traffic can not be INF, but 1.
The problem: Doing this is really a torment ... Review the method according to the flow balance Fang, mainly worship the byvoid of volunteers recruitment ... Let's first list the flow equilibrium equation: a[i] means I choose not to select, B[i] represents the auxiliary variable for the I-equation: 0=0a[1]+a[2]+......a[n]+b[1]=ka[2]+a[3]+......a[n+1]+b[2]=k............a[2*n+1]+a[2* N+2]+......+a[3*n]+b[2*n+1]=k0=0 differential is followed by a[1]+a[2]+......a[n]+b[1]=ka[n+1]-a[1]+b[2]-b[1]=0a[n+2]-a[2]+b[3]-b[2]=0............-a[2 *N+1]-A[2*N+2]-.........-A[3*n]-b[2*n+1]=-k According to Byvoid said: You can find that each equation to the left is a few variables and a constant add minus, the right is 0, just like the network flow in addition to the source point and the vertex of the meeting point are satisfied
Flow Balance。 Each positive variable corresponds to the flow that flows into the vertex, and the negative variable is the amount of traffic that flows out of the vertex, while the normal number can be considered as traffic from the attached source point, and the negative constant is the flow to the additional sink point. Therefore, the network can be constructed accordingly, and the maximum network flow from the additional source to the additional sink is satisfied. We also ask for the smallest, so we need to add weights to the X-variables, and then ask
minimum cost maximum flow。 So our composition:
s=0;t=3*n+1; For1 (I,3*n) a[i]=Read (); Insert (S,1,k,0); Insert (2*n+2,t,k,0); For1 (i,n) Insert (1,i+1,1,-a[i]); For2 (I,n+1,2*n) Insert (i-n+ 1,i+1,1,-a[i]); For2 (I,2*n+1,3*n) Insert (I-n+1,2*n+2,< Span style= "color: #800080;" >1,-a[i]); For1 (I,2*n+1) Insert (i,i+ 1,inf,0);
You need to figure out in which equation A[i] first appears, the second occurrence in which equation, and what the positive and negative numbers are.
B[i] appears obviously, I is positive, i+1 is negative
Then ask for maximum cost of the maximum flow can be over.
1 /**************************************************************2 problem:35503 User:tunix4 language:c++5 result:accepted6 time:20 Ms7 memory:6016 KB8 ****************************************************************/9 Ten //Bzoj 3550 One#include <cmath> A#include <vector> -#include <cstdio> -#include <cstring> the#include <cstdlib> -#include <iostream> -#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 A #defineCC (A, B) memset (A,b,sizeof (a)) at using namespacestd; - intGetint () { - 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 ; in } -typedefLong LongLL; to Const intn= -, m=200000, inf=~0u>>2; + Const Doubleeps=1e-8; - /*******************template********************/ the intN,m,k,a[n],b[n],c[n],w[n]; * LL ans; $ structedge{int from, To,v,c;};Panax Notoginseng structnet{ - Edge E[m]; the inthead[n],next[m],cnt; + voidInsintXintYintZintc) { Ae[++cnt]=(Edge) {x,y,z,c}; theNEXT[CNT]=HEAD[X]; head[x]=CNT; + } - voidAddintXintYintZintc) { $Ins (x,y,z,c); Ins (y,x,0,-c); $ } - intS,t,d[n], from[n],q[m]; - BOOLInq[n]; the BOOLSPFA () { - intL=0, r=-1;WuyiF (I,0, T) d[i]=INF; thed[s]=0; Q[++r]=s; inq[s]=1; - while(l<=R) { Wu intX=q[l++]; inq[x]=0; - for(intI=head[x];i;i=Next[i]) About if(E[I].V && d[x]+e[i].c<D[e[i].to]) { $d[e[i].to]=d[x]+e[i].c; - from[e[i].to]=i; - if(!Inq[e[i].to]) { -q[++r]=e[i].to; Ainq[e[i].to]=1; + } the } - } $ returnd[t]!=INF; the } the voidmcf () { the intx=INF; the for(intI= from[t];i;i= from[E[i]. from]) -x=min (x,e[i].v); in for(intI= from[t];i;i= from[E[i]. from]){ thee[i].v-=x; thee[i^1].v+=x; About } theans+=x*D[t]; the } the voidinit () { +M=n=getint (); m*=3; K=getint (); Cnt=1; -F (I,1, m) a[i]=getint (); thes=0; t=2*n+3;BayiAdd (S,1K0); theAdd (n2+2, T,k,0); theF (I,1N2+1) Add (i,i+1Inf0); - -F (I,2, n+1) Add (1I1,-a[i-1]); theF (i,n+2,2*n+1) Add (I,2*n+2,1,-a[i+n-1]); theF (I,2, n+1) Add (I,i+n,1,-a[i+n-1]); the the while(SPFA ()) MCF (); -printf"%lld\n",-ans); the } the }g1; the intMain () {94 #ifndef Online_judge theFreopen ("Input.txt","R", stdin); the //freopen ("Output.txt", "w", stdout); the #endif98 g1.init (); About return 0; -}
View Code
"Bzoj" "3550" "ONTAK2010" Vacation