1#include <cstdio>2#include <iostream>3 #defineM 16054 #defineS 30000055 using namespacestd;6 intQ[s],f[m],n,m,ans,a[m],head[m],next[s],u[s],v[s],c[s],fr[s],cnt=1, d[m],fro[m],t;7 voidJiaintA1,intA2,intA3,intA4)8 {9cnt++;Tennext[cnt]=HEAD[A1]; Onehead[a1]=CNT; Au[cnt]=A2; -v[cnt]=A3; -c[cnt]=A4; thefr[cnt]=A1; - return; - } - BOOLBFS () + { - for(intI=1; i<=t;i++) +d[i]=0x7fffffff; Af[0]=1; at intH=0, t=1; -q[1]=0; - for(; h!=t;) - { -h++; - intp=Q[h]; inf[p]=0; - for(intI=head[p];i;i=Next[i]) to if(v[i]&&d[u[i]]>d[p]+C[i]) + { -fro[u[i]]=i; thed[u[i]]=d[p]+C[i]; * if(!F[u[i]]) $ {Panax Notoginsengt++; -q[t]=U[i]; the } + } A } the if(d[t]==0x7fffffff) + return 0; - Else $ return 1; $ } - voidZhao () - { the intMa=0x7fffffff; - for(intI=fro[t];i;i=Fro[fr[i]])WuyiMa=min (ma,v[i]); the for(intI=fro[t];i;i=Fro[fr[i]]) - { Wuv[i]-=Ma; -v[i^1]+=Ma; Aboutans+=ma*C[i]; $ } - return; - } - intMain () A { +scanf"%d%d",&n,&m); thet=2*n+1; - for(intI=1; i<=n;i++) $scanf"%d",&a[i]); the for(intI=1; i<=n;i++) the { theJia0I1,0); theJia (i,0,0,0); -Jia (I+n,t,1,0); inJia (T,i+n,0,0); theJia0, I+n,1, A[i]); theJia (I+n,0,0,-a[i]); About } the for(intI=1; i<=m;i++) the { the inta1,a2,a3; +scanf"%d%d%d",&a1,&a2,&A3); - if(a1>A2) the swap (A1,A2);BayiJia (A1,a2+n,1, A3); theJia (A2+N,A1,0,-A3); the } - for(; BFS ();) - Zhao (); theprintf"%d\n", ans); the return 0; the}
Cost flow, split source points to the split point to the cost of 0, the cost is the point of the location fee, and then the original route to build out.
Bzoj 1927: [Sdoi2010] Star Racing