Upper and lower bounds network flow

Source: Internet
Author: User

sgu194:

No Yuanhui upper and lower bounds network flow, to find the maximum flow.
/*
Each side add (U,v,up[i]-dn[i])
Each point out[i]=∑dn[i,v];in[i]=∑dn[v,i];
Each point in[i]-out[i]>0 Add (S,i, in[i]-out[i]), otherwise add (I,t,out[i]-in[i]);
Determine if the side connected to St is full stream.
*/

#include <cstdio> #include <cstring> #include <cctype> #include <algorithm>using namespace std; #define REP (I,s,t) for (int. i=s;i<=t;i++) #define DWN (i,s,t) for (int i=s;i>=t;i--) #define CLR (x,c) memset (X,c, sizeof (x)) #define QWQ (x) for (edge *o=head[x];o;o=o->next) int read () {int X=0;char C=getchar (), while (!isdigit (c)) c= GetChar (); while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return x;} const int NMAX=205;CONST int Maxn=8e4+5;const int inf=0x7f7f7f7f;struct edge{int to,cap;edge *next,*rev;}; Edge es[maxn],*pt=es,*head[nmax];void Add (int u,int v,int d) {pt->to=v;pt->cap=d;pt->next=head[u];head[u]= pt++;p T->to=u;pt->cap=0;pt->next=head[v];head[v]=pt++;head[u]->rev=head[v];head[v]->rev=head[u] ;} Edge *cur[nmax],*p[nmax];int cnt[nmax],h[nmax];int maxflow (int s,int t,int n) {cnt[0]=n;int Flow=0,a=inf,x=s;edge *e; while (H[s]<n) {for (E=cur[x];e;e=e->next) if (e->cap>0&&h[x]==h[e->to]+1) break;if (e) {a=min ( A,E-&GT;CAP);cur[x]=p[e->to]=e;X=e->to;if (x==t) {while (x!=s) P[x]->cap-=a,p[x]->rev->cap+=a,x=p[x]->rev->to;flow+=a,a=inf;}} Else{if (!--cnt[h[x]]) break;h[x]=n;for (e=head[x];e;e=e->next) if (e->cap>0&&h[x]>h[e->to]+ 1) cur[x]=e,h[x]=h[e->to]+1;cnt[h[x]]++;if (x!=s) x=p[x]->rev->to;}} return flow;} int out[nmax],in[nmax],dn[maxn];bool work (int s,int t,int n,int m) {maxflow (s,t,n); Qwq (s) if (O-&GT;CAP) return 0;qwq (t) if (O->cap!=out[o->to]-in[o->to]) return 0;puts ("YES");p t=es+1;rep (i,1,m) printf ("%d\n", Dn[i]+pt->cap) , Pt+=2;return 1;} int main () {int n=read (), M=read (), U,v,d,tp;rep (i,1,m) {u=read (), V=read (), Dn[i]=read (); Tp=read (); Add (U,v,tp-dn[i]); In[v]+=dn[i];out[u]+=dn[i];} Rep (i,1,n) {if (in[i]-out[i]>0) Add (0,i,in[i]-out[i]); else add (I,n+1,out[i]-in[i]);} if (!work (0,n+1,n+2,m)) puts ("NO"); return 0;}

  

Upper and lower bounds network flow

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.