HDU - 6214 Smallest Minimum Cut (最小割边数)

链接:https://cn.vjudge.net/problem/HDU-6214

题意:T组样例,下一行给出n、m(n个点,m条边),加下来给出源点s、汇点t,加下来m行描述边u、v(边的两端点),w边的容量。求最小割的边数。

思路:给容量乘以一个较大的系数(W)+1(即容量变为w*W+1),跑最大流,得出的最大流%W就是答案。写过一个一样的题,写这个纯属为了锻炼写网络流。

#include <bits/stdc++.h>
using namespace std;
const int N = 210;
const int M = 2200;
const int W = 300;
const int inf = 0x3f3f3f3f;
struct node
{
	int to,ca,next;
}g[M]; 
int head[N],cur[N],deep[N],cnt,n,m,s,t;
void Init()
{
	cnt=0;
	memset(head,-1,sizeof(head));
	return ;
}
void add(int u,int v,int w)
{
	g[cnt].to=v;
	g[cnt].ca=w;
	g[cnt].next=head[u];
	head[u]=cnt++;
	return ;
}
bool bfs()
{
	memset(deep,0,sizeof(deep));
	queue<int> q;
	int u,v;
	deep[s]=1;
	q.push(s);
	while(!q.empty())
	{
		
		u=q.front();
		q.pop();
		//cout<<u<<endl;
		if(u==t) return 1;
		for(int i=head[u];i!=-1;i=g[i].next)
		{
			v=g[i].to;
			if(g[i].ca>0&&!deep[v])
			{
				deep[v]=deep[u]+1;
				q.push(v);
			}
		}
	}
	return deep[t]!=0;
}
int dfs(int u,int flow)
{
	
	if(u==t||!flow) return flow;
	int ans=0,nowflow,v;
	for(int& i=cur[u];i!=-1;i=g[i].next)
	{
		v=g[i].to;
		//cout<<u<<" "<<v<<endl;
		if(g[i].ca>0&&deep[v]==deep[u]+1)
		{
			nowflow=dfs(v,min(flow,g[i].ca));
			if(nowflow)
			{
				flow-=nowflow;
				g[i].ca-=nowflow;
				g[i^1].ca+=nowflow;
				//return nowflow;
				ans+=nowflow;
				if(!flow) break;
			}
		}
	}
	if(!ans) deep[u]=0;
	return ans;	
	//return 0;
}
int Dinic()
{
	int maxflow=0,flow;
	while(bfs())
	{
		for(int i=1;i<=n;i++)
			cur[i]=head[i];
		while(flow=dfs(s,inf))
			maxflow+=flow;
	}
	return maxflow;
}
int main(void)
{
	int T,u,v,w;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d%d",&n,&m,&s,&t);
		Init();	
		while(m--)
		{
			scanf("%d%d%d",&u,&v,&w);
			add(u,v,w*W+1);
			add(v,u,0);
		}
		printf("%d\n",Dinic()%W);
	}
	
	return 0;	
} 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值