bzoj1061: [Noi2008] Volunteer recruitment

Source: Internet
Author: User

Cost flow.

The legend of the modeling of the synthesis of the question ...

Https://www.byvoid.com/blog/noi-2008-employee

There is a whole lot of talk about modeling.

I write and write the actual meaning of modeling according to my own understanding.

If a[i]-a[i-1]>=0, the representative needs new hires. Otherwise, it means someone has left. (Leave does not have to be in t[i]+1 days, if the latter person is superfluous, will leave very early).

1. The model is s->i capacity of a[i]-a[i-1], the cost is 0. 2.i->t capacity is – (A[i]-a[i-1)) at a cost of 0.

For each person he will enter in S[i] days, and leave at (t[i]+1) day or earlier.

3. In s[i] to t[i]+1 a capacity unlimited cost for the side of C[i]. 4. On the side of (i+1)->i a capacity unlimited cost of 0.

s represents the person entering, and T represents the person who left.

The most important is the idea of flow balance when modeling.

Set Y[i] for the extra person for the first day. X[i] is the number of volunteers of type I.

For each point (A[i]-a[i-1]) + (y[i]-y[i-1]) +sum (X[k1])-sum (X[k2]) =0 (K1 and K2 meet t[k1]+1=i,s[k2]=i).

Each point satisfies such a flow balance

#include <cstdio>#include<algorithm>#include<cstring>using namespaceStd;typedefLong LongLL;Const intMAXN = ++Ten;Const intMAXM =100000+Ten;Const intINF =0x3f3f3f3f;intG[maxn],v[maxm],f[maxm],c[maxm],next[maxm],eid;intn,m,s,t;intA[MAXN];Long LongRes; LL DIST[MAXN];intPRE[MAXN];BOOLINQUE[MAXN];intQ[maxm],l,r,u;voidAddedge (intAintBintFintC) {V[eid]=b; F[eid]=f; C[eid]=c; Next[eid]=g[a]; g[a]=eid++; V[eid]=a; f[eid]=0; C[eid]=-c; NEXT[EID]=G[B]; g[b]=eid++; }voidbuild () {memset (g,-1,sizeof(g)); scanf ("%d%d",&n,&m); S=0; t=n+2;  for(intI=1; i<=n;i++) scanf ("%d",&A[i]);  for(intI=1, d;i<=n+1; i++) {D=a[i]-a[i-1]; if(d>=0) Addedge (S,i,d,0); ElseAddedge (i,t,-d,0); }     for(intI=1, s,t,c;i<=m;i++) {scanf ("%d%d%d",&s,&t,&B); Addedge (S,t+1, Inf,c); }     for(intI=1; i<=n;i++) Addedge (i+1, I,inf,0);}BOOLSPFA () {memset (dist,0x3f,sizeof(Dist)); L=r=0; Q[r++]=S; Dist[s]=0;  while(l<r) {Inque[u=q[l++]]=0;  for(intI=g[u];~i;i=next[i])if(F[i] && dist[v[i]]>dist[u]+C[i]) {Dist[v[i]]=dist[u]+C[i]; Pre[v[i]]=i; if(!inque[v[i]]) inque[q[r++]=v[i]]=1; }    }    returndist[t]<inf;}Long Longaugment () {Long Longaug=inf,res=0;  for(inti=t;i!=s;i=v[pre[i]^1]) Aug=min (Long Long) f[pre[i]);  for(inti=t;i!=s;i=v[pre[i]^1]) {F[pre[i]]-=; F[pre[i]^1]+=; Res+=aug*C[pre[i]]; }    returnRes;}voidsolve () {Long Longres=0;  while(SPFA ()) res+=augment (); printf ("%lld\n", res);}intMain () {build ();    Solve (); return 0;}

bzoj1061: [Noi2008] Volunteer recruitment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.