求边数最少的最小割

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
struct node{
	int to,next,flow;
}e[2010];
int head[1010],cnt,S,T,d[1010],cur[1010],n,m;
void add_edge(int from,int to,int flow){
	e[cnt].to = to;
	e[cnt].flow = flow;
	e[cnt].next = head[from];
	head[from] = cnt++;
	e[cnt].to = from;
	e[cnt].flow = 0;
	e[cnt].next = head[to];
	head[to] = cnt++;
}
int BFS(){
	memset(d,0,sizeof(d));
	d[S] = 1;
	queue<int>q;
	q.push(S);
	while(!q.empty()){
		int u = q.front();
		q.pop();
		for( int i = head[u]; i != -1; i = e[i].next){
			int to = e[i].to;
			if(!d[to] && e[i].flow){
				d[to] = d[u] + 1;
				q.push(to);
				if(to == T) return 1;
			}
		}
	}
	return 0;
}
int dfs(int u,int flow){
	int cost = 0;
	if(u == T) return flow;
	for(int &i = cur[u]; i!= -1; i = e[i].next){
		int to = e[i].to;
		if(d[to] == d[u] + 1 && e[i].flow){
			int t = dfs(to,min(flow - cost,e[i].flow));
			if(t){
				e[i].flow -= t;
				e[i^1].flow += t;
				cost += t;
				if(flow == cost) break;
			}
		}
	}
	return cost;
}
int Dinic(){
	int res = 0;
	while(BFS()){
		for(int i = 0; i <= n; i++)
		cur[i] = head[i];
		res += dfs(S,INF);
	}
	return res;
}
int main(){
	int t,a,b,c;
	scanf("%d",&t);
	while(t--){
		memset(head,-1,sizeof(head));
		cnt = 0; 
		scanf("%d%d",&n,&m);
		scanf("%d%d",&S,&T);
		for(int i = 0; i < m; i++){
			scanf("%d%d%d",&a,&b,&c);
			add_edge(a,b,c * (m + 1) + 1);
		}
		printf("%d\n",Dinic()%(m+1));
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值