Smallest Minimum Cut

这篇博客介绍了如何运用Dinic算法解决网络流中的最小割问题,通过实例展示了算法过程,并提供了AC代码。文章提及在实现过程中遇到的问题及解决方法,强调了边的权重处理技巧,即为每条边的权重乘以(E+1)+1,以方便计算最小割的数量。
摘要由CSDN通过智能技术生成

题目:https://acm.hdu.edu.cn/showproblem.php?pid=6214

Problem Description
Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition of nodes set V into two parts such that s and t belong to different parts. The cut set is the subset of E with all edges connecting nodes in different parts. A minimum cut is the one whose cut set has the minimum summation of capacities. The size of a cut is the number of edges in the cut set. Please calculate the smallest size of all minimum cuts.

Input
The input contains several test cases and the first line is the total number of cases T (1≤T≤300).
Each case describes a network G, and the first line contains two integers n (2≤n≤200) and m (0≤m≤1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from 1 to n.
The second line contains two different integers s and t (1≤s,t≤n) corresponding to the source and sink.
Each of the next m lines contains three integers u,v and w (1≤w≤255) describing a directed edge from node u to v with capacity w.

Output
For each test case, output the smallest size of all minimum cuts in a line.

Sample Input
2
4 5
1 4
1 2 3
1 3 1
2 3 1
2 4 1
3 4 2
4 5
1 4
1 2 3
1 3 1
2 3 1
2 4 1
3 4 3

Sample Output
2
3

跑一个dinic的板子竟然花了两个小时。。。一直找不到问题,重新敲了一遍又可以了。。。

建边的时候每条边权 w = w * (E + 1) + 1;
这样得到最大流 maxflow / (E + 1) ,最少割边数 maxflow % (E + 1)

AC代码:

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;

const int N = 10010,M = 200020;
const ll inf = ll(1e18);

struct edge{
	int next,to;
	ll w;
}edges[M];

int head[N];
int cnt = 1;

int cur[N],lv[N];
int n,m,s,t;

void add(int u,int v,ll w){
	
	edges[++cnt].next = head[u];
	edges[cnt].to = v;
	edges[cnt].w = w;
	head[u] = cnt;
}

void init(){
	cnt = 1;
	memset(head,0,sizeof(head));
}

bool bfs(){
	
	memset(lv,0,sizeof(lv));
	lv[s] = 1;
	
	queue<int> q;
	q.push(s);
	
	while(!q.empty()){
		
		int u = q.front();
		q.pop();
		
		for(int i = head[u]; i; i = edges[i].next){
			
			int to = edges[i].to;
			ll w = edges[i].w;
			
			if(w > 0 && lv[to] == 0){
				lv[to] = lv[u] + 1;
				if(to == t) return true;
				q.push(to);
			}
		}
	}
	
	return false;
}

ll dfs(int p = s,ll flow = inf){
	
	if(p == t || !flow) return flow;
	
	ll ret = 0;
	
	for(int& i = cur[p]; i; i = edges[i].next){
		
		int to = edges[i].to;
		ll w = edges[i].w;
		
		if(w > 0 && lv[to] == lv[p] + 1){
			
			ll f = dfs(to,min(flow - ret,w));
			
			edges[i].w -= f;
			edges[i^1].w += f;
			
			ret += f;
			if(ret == flow) return ret;
		}
	}
	
	return ret;
}

ll Dinic(){
	
	ll ans = 0;
	
	while(bfs()){
		memcpy(cur,head,sizeof(head));
		ans += dfs();
	}
	return ans;
}

int main(){
	
	int k;
	scanf("%d",&k);
	
	while(k--){
		
		init();
		
		scanf("%d%d",&n,&m);
		scanf("%d%d",&s,&t);
		
		int u,v;
		ll w;
		for(int i = 1;i<=m;i++){
			scanf("%d%d%lld",&u,&v,&w);
			add(u,v,w*(m+1)+1);
			add(v,u,0);
		}
		
		ll ans = Dinic();
		
		printf("%lld\n",ans%(m+1));
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值