#include<bits/stdc++.h>usingnamespace std;typedeflonglong ll;//三年竞赛一场空,不开long long见祖宗//typedef __int128 lll;#define print(i) cout << "debug: " << i << endl#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)#define mem(a, b) memset(a, b, sizeof(a))#define pb(a) push_back(a)#define x first#define y secondtypedef pair<int,int> pii;const ll mod =1e9+7;constint maxn =2e5+10;constint inf =0x3f3f3f3f;struct edge
{int ep, val, nex;}e[maxn];int head[maxn], tot;int n, m, s, t;int depth[maxn], cur[maxn];int q[maxn];int low[maxn];int dif[maxn];voidinit(){mem(head,-1), tot =0;}voidaddedge(int x,int y,int a,int b){
e[tot]={y, b - a, head[x]}, low[tot]= a;
head[x]= tot++;
e[tot]={x,0, head[y]};
head[y]= tot++;}boolbfs(){int hh =0, tt =0;
q[tt]= s;mem(depth,-1), depth[s]=1;
cur[s]= head[s];while(hh <= tt){int now = q[hh++];for(int i = head[now];~i; i = e[i].nex){int ep = e[i].ep;if(depth[ep]==-1&& e[i].val){
cur[ep]= head[ep];
depth[ep]= depth[now]+1;if(ep == t)returntrue;
q[++tt]= ep;}}}returnfalse;}intdfs(int now,int flow){if(now == t)return flow;int res =0;for(int i = head[now];~i && flow; i = e[i].nex){
cur[now]= i;int ep = e[i].ep;if(depth[ep]== depth[now]+1&& e[i].val){int k =dfs(ep,min(flow, e[i].val));if(k ==0) depth[ep]=-1;
e[i].val -= k, e[i ^1].val += k;
flow -= k, res += k;}}return res;}intdinic(){int res =0;while(bfs())
res +=dfs(s, inf);return res;}intmain(){
cin >> n >> m;init();
s =0, t = n +1;for(int i =1; i <= m; i++){int x, y, a, b; cin >> x >> y >> a >> b;addedge(x, y, a, b);
dif[x]-= a, dif[y]+= a;}int sum =0;for(int i =1; i <= n; i++)if(dif[i]>0)addedge(s, i,0, dif[i]), sum += dif[i];elseif(dif[i]<0)addedge(i, t,0,-dif[i]);int d =dinic();if(d != sum) cout <<"NO"<< endl;else{
cout <<"YES"<< endl;for(int i =0; i < m <<1; i +=2){
cout << low[i]+ e[i ^1].val << endl;}}}
题目:2189. 有源汇上下界最大流
代码:
#include<bits/stdc++.h>usingnamespace std;typedeflonglong ll;//三年竞赛一场空,不开long long见祖宗//typedef __int128 lll;#define print(i) cout << "debug: " << i << endl#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)#define mem(a, b) memset(a, b, sizeof(a))#define pb(a) push_back(a)#define x first#define y secondtypedef pair<int,int> pii;const ll mod =1e9+7;constint maxn =2e5+10;constint inf =0x3f3f3f3f;struct edge
{int ep, val, nex;}e[maxn];int head[maxn], tot;voidinit(){mem(head,-1), tot =0;}voidaddedge(int x,int y,int val){
e[tot]={y, val, head[x]};
head[x]= tot++;
e[tot]={x,0, head[y]};
head[y]= tot++;}int add[maxn];int S, T;int n, m, s, t;int depth[maxn], cur[maxn];int q[maxn];boolbfs(){int hh =0, tt =0; q[hh]= S;mem(depth,-1), depth[S]=1;
cur[S]= head[S];while(hh <= tt){int now = q[hh++];for(int i = head[now];~i; i = e[i].nex){int ep = e[i].ep;if(depth[ep]==-1&& e[i].val){
depth[ep]= depth[now]+1;
cur[ep]= head[ep];if(ep == T)returntrue;
q[++tt]= ep;}}}returnfalse;}intdfs(int now,int flow){if(now == T)return flow;int sum =0;for(int i = cur[now];~i && flow; i = e[i].nex){
cur[now]= i;int ep = e[i].ep;if(depth[ep]== depth[now]+1&& e[i].val){int k =dfs(ep,min(flow, e[i].val));if(k ==0) depth[ep]=-1;
e[i].val -= k, e[i ^1].val += k;
sum += k, flow -= k;}}return sum;}intdinic(){int res =0;while(bfs())
res +=dfs(S, inf);return res;}intmain(){init();
cin >> n >> m >> s >> t;for(int i =1; i <= m; i++){int x, y, a, b; cin >> x >> y >> a >> b;addedge(x, y, b - a);
add[x]-= a, add[y]+= a;}
S =0, T = n +1;int sum =0;for(int i =1; i <= n; i++){if(add[i]>0)addedge(S, i, add[i]), sum += add[i];elseif(add[i]<0)addedge(i, T,-add[i]);}addedge(t, s, inf);if(sum !=dinic()) cout <<"No Solution"<< endl;else{int res = e[tot -1].val;
e[tot -1].val = e[tot -2].val =0;
S = s, T = t;
cout << res +dinic()<< endl;}return0;}