Network flow/Charge flow
Compare the naked problem
is still a binary graph model, from the source point s to each school I (1,0), "Attention is to the school, not even to the school label M[i] ... Oh, here wa once. "
Then for each school I connect j+n $ (A[i]\leq J \leq B[i]) $ flow of 1, the cost is $abs (M[I]-J) *k[i]$, and finally for each label J Edge j+n->t Traffic is 1 cost for 0.
After running the cost stream to see if the flow is N, if not the explanation of no solution.
1 /**************************************************************2 problem:15203 User:tunix4 language:c++5 result:accepted6 time:1452 Ms7 memory:5964 KB8 ****************************************************************/9 Ten //Bzoj 1520 One#include <vector> A#include <cstdio> -#include <cstring> -#include <cstdlib> the#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 + using namespacestd; AInlineintGetint () { at intv=0, sign=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} - returnv*Sign ; - } - Const intn=410, m=200000, inf=~0u>>2; intypedefLong LongLL; - /******************tamplate*********************/ to intN,ans,flow; + 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 } + int from[n],q[m],d[n],s,t,ed; - BOOLinq[n],sign; $ BOOLSPFA () { $ intL=0, r=-1; -F (I,1, T) d[i]=INF; -d[s]=0; Q[++r]=s; inq[s]=1; the while(l<=R) { - intx=q[l++];Wuyiinq[x]=0; the for(intI=head[x];i;i=Next[i]) - if(e[i].v>0&& d[x]+e[i].c<D[e[i].to]) { Wud[e[i].to]=d[x]+e[i].c; - from[e[i].to]=i; About if(!Inq[e[i].to]) { $q[++r]=e[i].to; -inq[e[i].to]=1; - } - } A } + returnd[t]!=INF; the } - voidmcf () { $ intx=INF; the 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; -e[i^1].v+=x; in } theflow+=x; theans+=x*D[t]; About } the voidinit () { theN=getint (); Cnt=1; thes=0; t=n*2+1; + intm,x,y,k; -F (I,1, N) { theM=getint (); X=getint (); Y=getint (); k=getint ();BayiAdd (S,i,1,0); theF (j,x,y) Add (I,j+n,1, K*abs (J-m)); theAdd (I+n,t,1,0); - } - while(SPFA ()) MCF (); the if(flow==n) printf ("%d\n", ans); the Elseprintf"NIE"); the } the }g1; - the intMain () { the #ifndef Online_judge theFreopen ("1520.in","R", stdin);94Freopen ("1520.out","W", stdout); the #endif the g1.init (); the return 0;98}
View Code
"Bzoj" "1520" "POI2006" Szk-schools