首页 > 其他分享 >2173. Dinic/ISAP求最小割

2173. Dinic/ISAP求最小割

时间:2022-11-27 13:23:30浏览次数:70  
标签:le idx int 最小 2173 Dinic define ISAP

题目链接

2173. Dinic/ISAP求最小割

给定一个包含 \(n\) 个点 \(m\) 条边的有向图,并给定每条边的容量,边的容量非负。

图中可能存在重边和自环。求从点 \(S\) 到点 \(T\) 的最小割。

输入格式

第一行包含四个整数 \(n,m,S,T\)。

接下来 \(m\) 行,每行三个整数 \(u,v,c\),表示从点 \(u\) 到点 \(v\) 存在一条有向边,容量为 \(c\)。

点的编号从 \(1\) 到 \(n\)。

输出格式

输出点 \(S\) 到点 \(T\) 的最小割。

如果从点 \(S\) 无法到达点 \(T\) 则输出 \(0\)。

数据范围

\(2 \le n \le 10000\),
\(1 \le m \le 100000\),
\(0 \le c \le 10000\),
\(S \neq T\)

输入样例:

7 14 1 7
1 2 5
1 3 6
1 4 5
2 3 2
2 5 3
3 2 2
3 4 3
3 5 3
3 6 7
4 6 5
5 6 1
6 5 1
5 7 8
6 7 7

输出样例:

14

解题思路

最小割

2171. EK求最大流 可知,最小割等于最大流,所以求最小割即求最大流

  • 时间复杂度:\(O(n^2m)\)

代码

// Problem: Dinic/ISAP求最小割
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/2175/
// 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=10005,M=200005,inf=1e9;
int n,m,S,T;
int h[N],ne[M],f[M],e[M],idx;
int d[N],q[N],hh,tt,cur[N];
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);
	d[S]=hh=tt=0;
	q[0]=S;
	cur[S]=h[S];
	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 res=0,flow;
	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 x,y,z;
    	scanf("%d%d%d",&x,&y,&z);
    	add(x,y,z);
    }
    printf("%d",dinic());
    return 0;
}

标签:le,idx,int,最小,2173,Dinic,define,ISAP
From: https://www.cnblogs.com/zyyun/p/16929509.html

相关文章

  • sheep match disappear game All In One
    sheepmatchdisappeargameAllInOne羊了个羊小游戏在线网页版sheepNsheepH5gamedogmatchdisappeargameAllInOne狗了个狗小游戏在线网页版dogNdogH5......
  • Dinic(最大流/最小割+费用流)
    最大流/最小割:1typedeflonglongll;2constintN=1e4+5;3constintM=2e5+5;4constllinf=1e18;5intn,m,s,t,tot;6llhead[N],to[......
  • 2.4 RedisAPI之list
    1.简介字符串键值结构(keyvalue)特点有序可重复左右两边都可插入和删除2.命令从列表右端插入值rpushkeyvalue1value2......valueN时间复杂度为O(1~n)从列表左端插入值l......
  • 2.6 RedisAPI之zset
    1.简介字符串键值结构(keyscorevalue)特点有序不重复支持集合间操作2.命令向集合内添加元素,element不可以重复但score是可以重复的zaddkeyscoreelement时间复杂度为O(l......
  • 2.5 RedisAPI之set
    1.简介字符串键值结构(keyvalue)特点无序不重复支持集合间操作2.命令向集合内添加元素element,如果element已经存在则添加失败saddkeyelement时间复杂度为O(1)删除集合内......
  • 2.3 RedisAPI之hash
    1.简介字符串键值结构(keyfieldvalue)2.命令设置key对应的field的valuehsetkeyfieldvalue时间复杂度为O(1)获取key对应的field的valuehgetkeyfieldvalue时间复杂度......
  • 2.2 RedisAPI之string
    1.简介字符串键值结构(keyvalue)value的值小于512m,一般建议一个key-value的大小为100k使用场景缓存计数器分布式锁2.命令设置key-value不管key是否存在都设置setkeyvalue......
  • 2.1 RedisAPI之简介
    1.通用命令遍历所有keykeys*keys命令一般不在生产环境使用,主要原因是生产环境下通常有大量的key,列出所有key没有实际的意义并且会消耗很多内存资源。删除指定keydelkey计......
  • LeetCode448. Find All Numbers Disappeared in an Array
    题意n个数,统计1-n中未出现的数方法遍历和标记代码classSolution{public:vector<int>findDisappearedNumbers(vector<int>&nums){sort(nums.beg......
  • Jeffrey's ambition(Dinic板子题)
    Jeffrey'sambition(网络流板子题)网路流的经典例题,会有两种需要匹配的东西,这两种东西直接可以构成一个二分图,这时候题目就会要求你求出最大匹配(水题)//要与这道Arranget......