1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <stack>6#include <queue>7#include <cstring>8 #definePAU Putchar (")9 #defineENT Putchar (' \ n ')Ten #defineMSE (A, B) memset (A,b,sizeof (a)) One #defineREN (x) for (TED*E=FCH[X];E;E=E->NXT) A #defineTIL (x) for (int i=1;i<=x;i++) - using namespacestd; - Const intmaxn= -+Ten, maxm=20000+Ten, inf=1e9; the structzkw{ - structted{intX,y,w,c;ted*nxt,*re;} adj[maxm],*fch[maxn],*MS; - intN,s,t,d[maxn],cost,ans;BOOLINQ[MAXN],VIS[MAXN]; - voidInitintN) { This->n=n;ms=adj; MSE (Vis,false); MSE (INQ,false);return;} + voidAddintXintYintWintc) { -*ms= (Ted) {x,y,w,c,fch[x],ms+1};fch[x]=ms++; +*ms= (Ted) {y,x,0,-c,fch[y],ms-1};fch[y]=ms++; A return; at } - BOOLBFs () { -TIL (n) d[i]=inf;queue<int>Q; Q.push (T);d [t]=0; - while(!Q.empty ()) { - intX=q.front (); Q.pop (); inq[x]=false; REN (x) { - intv=e->y;if(e->re->w&&d[v]>d[x]+e->re->c) { ind[v]=d[x]+e->re->c;if(!inq[v]) inq[v]=true, Q.push (v); - } to } +} for(ted*e=adj;e!=ms;e++) e->c+=d[e->y]-d[e->x];cost+=d[s];returnd[s]!=inf; - } the intDfsintXint( ) { * if(X==t| |! Augreturn(Ans+=aug*cost,aug);intflow=0, k;vis[x]=true; REN (x) { $ intv=e->y;if(e->w&&!e->c&&!vis[v]&& (K=dfs (V,min (aug,e->(w)))) {Panax Notoginsenge->w-=k;e->re->w+=k;flow+=k;aug-=k;if(!aug) Break; - } the}returnflow; + } A intMCMF (intSintT) { the This->s=s; This->t=t; while(BFS ()) DoMSE (Vis,false); while(Dfs (S,inf));returnans; + } - }sol; $InlineintRead () { $ intx=0, sig=1;CharCh=GetChar (); - for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') sig=0; - for(; isdigit (ch); Ch=getchar ()) x=Ten*x+ch-'0'; the returnsig?x:-x; - }WuyiInlinevoidWriteintx) { the if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; - intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; Wu for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; - } About intn,m; $ intMain () { -N=read (); M=read (); Sol.init (n);intx,y,w; -TIL (M) x=read (), Y=read (), W=read (), Sol.add (X,y,w,read ()); Write (SOL.MCMF (1, N)); - return 0; A}
ZKW Cost Flow correction