2190. 有源汇上下界最小流

题目链接

2190. 有源汇上下界最小流

给定一个包含 n 个点 m 条边的有向图,每条边都有一个流量下界和流量上界。

给定源点 S 和汇点 T,求源点到汇点的最小流。

注意,为了方便,本题做出如下约定:

  • 可行流流量 = 从源点流出的流量 - 流入源点的流量,可以为负值。
  • 例如,当 ST 的流量为 0TS 的流量为 10 时,ST 的流量为 10

输入格式

第一行包含四个整数 n,m,S,T

接下来 m 行,每行包含四个整数 a,b,c,d 表示点 ab 之间存在一条有向边,该边的流量下界为 c,流量上界为 d

点编号从 1n

输出格式

输出一个整数表示最小流。

如果无解,则输出 No Solution

数据范围

1n50003,
1m125003,
1a,bn,
0cd2147483647,
数据保证答案不超过 int 范围。

输入样例:

7 12 6 7 6 1 0 2147483647 1 7 0 2147483647 6 2 0 2147483647 2 7 0 2147483647 6 3 0 2147483647 3 7 0 2147483647 6 4 0 2147483647 4 7 0 2147483647 6 5 0 2147483647 5 7 0 2147483647 5 1 1 2147483647 3 4 1 2147483647

输出样例:

2

解题思路

最大流,有源汇上下界最小流

2189. 有源汇上下界最大流,这里求解最小流,由于在残余网络中的正边和反边流量关于容量对称,求解 ts 的可行流,相当于将 st 的流量退流,可行流达到最大时说明 st 的流量已经退干,故答案为现残余网络的可行流减去从 ts 的最大流

  • 时间复杂度:O(n2m)

代码

// Problem: 有源汇上下界最小流 // Contest: AcWing // URL: https://www.acwing.com/problem/content/description/2192/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=50010,M=(N+125005)*2,inf=2147483647; int n,m,S,T,s,t,C[N]; int h[N],e[M],ne[M],f[M],idx; int d[N],cur[N],q[N],hh,tt; void add(int a,int b,int c) { e[idx]=b,f[idx]=c,ne[idx]=h[a],h[a]=idx++; e[idx]=a,f[idx]=0,ne[idx]=h[b],h[b]=idx++; } bool bfs() { memset(d,-1,sizeof d); q[0]=S; cur[S]=h[S]; tt=hh=d[S]=0; while(hh<=tt) { int x=q[hh++]; for(int i=h[x];~i;i=ne[i]) { int y=e[i]; if(d[y]==-1&&f[i]) { d[y]=d[x]+1; cur[y]=h[y]; if(y==T)return true; q[++tt]=y; } } } return false; } int dfs(int x,int limit) { if(x==T)return limit; int flow=0; for(int i=cur[x];~i&&flow<limit;i=ne[i]) { cur[x]=i; int y=e[i]; if(d[y]==d[x]+1&&f[i]) { int t=dfs(y,min(f[i],limit-flow)); if(!t)d[y]=-1; f[i]-=t,f[i^1]+=t,flow+=t; } } return flow; } int dinic() { int flow=0,res=0; while(bfs())while(flow=dfs(S,inf))res+=flow; return res; } int main() { memset(h,-1,sizeof h); scanf("%d%d%d%d",&n,&m,&s,&t); for(int i=1;i<=m;i++) { int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); add(a,b,d-c); C[a]-=c,C[b]+=c; } S=0,T=n+1; int tot=0; for(int i=1;i<=n;i++) if(C[i]>0)tot+=C[i],add(S,i,C[i]); else if(C[i]<0) add(i,T,-C[i]); add(t,s,inf); if(tot!=dinic())puts("No Solution"); else { int res=f[idx-1]; f[idx-1]=f[idx-2]=0; T=s,S=t; printf("%d\n",res-dinic()); } return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16925565.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示