#115. 无源汇有上下界可行流
描述
输入格式
输出格式
样例
数据范围与提示
显示分类标签
板子题,就不细将了,有空整理一下。
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> using namespace std; const int MAXN=2000001; inline char nc() { static char buf[MAXN],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++; } inline int read() { char c=nc();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=nc();} return x*f; } int n,m,s,t; struct node { int u,v,flow,nxt; }edge[MAXN]; int head[MAXN],cur[MAXN],A[MAXN]; int num=0; void AddEdge(int x,int y,int z) { edge[num].u=x; edge[num].v=y; edge[num].flow=z; edge[num].nxt=head[x]; head[x]=num++; } void add_edge(int x,int y,int z) { AddEdge(x,y,z); AddEdge(y,x,0); } int deep[MAXN],L[MAXN]; bool BFS() { memset(deep,0,sizeof(deep)); deep[s]=1; queue<int>q; q.push(s); while(q.size()!=0) { int p=q.front(); q.pop(); for(int i=head[p];i!=-1;i=edge[i].nxt) if(!deep[edge[i].v]&&edge[i].flow) { deep[edge[i].v]=deep[edge[i].u]+1;q.push(edge[i].v); if(edge[i].v==t) return 1; } } return deep[t]; } int DFS(int now,int nowflow) { if(now==t||nowflow<=0) return nowflow; int totflow=0; for(int &i=cur[now];i!=-1;i=edge[i].nxt) { if(deep[edge[i].v]==deep[edge[i].u]+1&&edge[i].flow) { int canflow=DFS(edge[i].v,min(nowflow,edge[i].flow)); edge[i].flow-=canflow; edge[i^1].flow+=canflow; totflow+=canflow; nowflow-=canflow; if(nowflow<=0) break; } } return totflow; } int Dinic() { int ans=0; while(BFS()) { for(int i=0;i<=n;i++) cur[i]=head[i]; ans+=DFS(s,1e8); } return ans; } int main() { #ifdef WIN32 freopen("a.in","r",stdin); #else #endif n=read();m=read();s=0;t=n+1; memset(head,-1,sizeof(head)); for(int i=1;i<=m;i++) { int x=read(),y=read(),lower=read(),upper=read();L[i-1]=lower; add_edge(x,y,upper-lower);A[x]-=lower;A[y]+=lower; } int sum=0; for(int i=1;i<=n;i++) { if(A[i]>0) sum+=A[i],add_edge(s,i,A[i]); else add_edge(i,t,-A[i]); } if(Dinic()!=sum) printf("NO"); else { printf("YES "); for(int i=0;i<m;i++) printf("%d ",edge[i*2|1].flow+L[i]); } return 0; }