SGU 194
题意:无源汇有上下界的最大流
收获:https://wenku.baidu.com/view/0f3b691c59eef8c75fbfb35c.html
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int maxn = 2e2+5; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,s,t,tot; int b[maxn*maxn],c[maxn*maxn],in[maxn],out[maxn]; int pre[maxn],dis[maxn]; int q[maxn],l,r; struct edge{ int v,w,nt; }e[maxn*maxn]; void init(){ tot = 0; mt(pre,-1);mt(in,0);mt(out,0); } void addedge(int u,int v,int w){ e[tot].v=v;e[tot].w=w;e[tot].nt=pre[u]; pre[u]=tot++; e[tot].v=u;e[tot].w=0;e[tot].nt=pre[v]; pre[v]=tot++; } bool bfs(){ mt(dis,-1); dis[s]=0; l = r = 0; q[r++] = s; while(l<r){ int u=q[l++]; for(int i=pre[u];~i;i=e[i].nt){ int v=e[i].v,w=e[i].w; if(dis[v]==-1&&w>0){ dis[v]=dis[u]+1; q[r++] = v; } } } return dis[t]!=-1; } int dfs(int u,int cp){ if(u==t||!cp) return cp; int tmp=cp; for(int i=pre[u];~i;i=e[i].nt){ int v=e[i].v,w=e[i].w; if(dis[v]==dis[u]+1&&w>0){ int tt=dfs(v,min(tmp,w)); e[i].w-=tt; e[i^1].w+=tt; tmp-=tt; if(tmp<=0) return cp; } } dis[u]=inf; return cp-tmp; } int dinic(){ int ret = 0; while(bfs()) ret+=dfs(s,inf); return ret; } int main(){ int u,v; init(); scanf("%d%d",&n,&m); rep(i,0,m) scanf("%d%d%d%d",&u,&v,&b[i],&c[i]),addedge(u,v,c[i]-b[i]),in[v] += b[i],out[u] += b[i]; s = 0;t = n + 1; int ans = 0; rep(i,1,n+1) if(out[i] - in[i] > 0) addedge(i,t,out[i] - in[i]),ans += out[i] - in[i]; else addedge(s,i,in[i] - out[i]); if(ans!=dinic()) return puts("NO"),0; puts("YES"); rep(i,0,m) printf("%d ",b[i] + e[i << 1 | 1].w); return 0; }