Bzoj 1061 Volunteer Recruitment

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=1061

Idea: You can use the conversion of inequalities into the cost stream.

The inequality is listed, if there is a negative constant, then from the equation to the T, if it is positive from the s to the equation, the flow is constant, the cost is 0.

If it is a variable, then find the two equations of this variable, from the negative to the positive flow of the INF edge, if there is a cost, then add the cost.

1#include <cstdio>2#include <cmath>3#include <algorithm>4#include <cstring>5#include <iostream>6 #defineINF 0x7fffffff7 inttot,go[200005],next[200005],first[200005],flow[200005],cost[200005];8 intop[200005],a[200005];9 intc[200005],vis[200005],dis[200005];Ten ints,t,n,m,edge[200005], from[200005],ans; One intRead () { A     intt=0, f=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; ch=GetChar ();} -      while('0'<=ch&&ch<='9') {t=t*Ten+ch-'0'; ch=GetChar ();} the     returnt*F; - } - voidInsertintXintYintZintl) { -tot++; +go[tot]=y; -next[tot]=First[x]; +first[x]=tot; Aflow[tot]=Z; atcost[tot]=l; - } - voidAddintXintYintZintl) { -Insert (x,y,z,l); op[tot]=tot+1; -Insert (Y,x,0,-L); op[tot]=tot-1; - } in BOOLSPFA () { -      for(inti=s;i<=t;i++) vis[i]=0, dis[i]=0x7fffffff; to     intH=1, t=1; vis[s]=1;d is[s]=0; +      while(h<=t) { -         intnow=c[h++]; the          for(intI=first[now];i;i=Next[i]) { *               intPur=Go[i]; $               if(flow[i]&&dis[pur]>dis[now]+Cost[i]) {Panax Notoginsengedge[pur]=i; -                   from[pur]=Now ; thedis[pur]=dis[now]+Cost[i]; +                  if(Vis[pur])Continue; Avis[pur]=1; thec[++t]=pur; +                 } -         } $vis[now]=0; $     } -     returndis[t]!=0x7fffffff; - } the voidUpdata () { -     intmn=0x7fffffff;Wuyi      for(intI=t;i!=s;i= from[i]) { themn=std::min (Mn,flow[edge[i]); -     } Wu      for(intI=t;i!=s;i= from[i]) { -ans+=mn*Cost[edge[i]]; Aboutflow[edge[i]]-=mn; $flow[op[edge[i]]]+=mn; -     } - } - intMain () { AN=read (); m=read (); +s=0; t=n+2; the      for(intI=1; i<=n;i++) a[i]=read (); -      for(intI=1; i<=m;i++){ $          intU=read (), V=read (), c=read (); theAdd (u,v+1, inf,c); the     }     the      for(intI=1; i<=n+1; i++){ the          inttmp=a[i]-a[i-1]; -          if(tmp>=0) Add (S,i,tmp,0); in          ElseAdd (I,t,-tmp,0); the          if(i>1) Add (i,i-1Inf0); the     } About      while(SPFA ()) Updata (); theprintf"%d\n", ans); the}

Bzoj 1061 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.