hdu 3987 - 最小割最少割边

题目链接:点击打开链接


解题思路:几乎和hdu6214相同,具体了解看这里点击打开链接


代码:

#include<bits/stdc++.h>
using namespace std;
const __int64 inf = 1ll<<47;
const int mx = 1e5 + 10;
const int maxn = 1e3 + 10;
int n,m,S,T,tot,head[maxn],cur[maxn];
struct node
{
	int y;
	__int64 v;
	int nxt;
}edge[mx<<2];
void AddEdge(int x,int y,__int64 v)
{
	edge[tot].y = y;
	edge[tot].v = v;
	edge[tot].nxt = head[x];
	head[x] = tot++;
}
int dep[maxn];
bool bfs()
{
	memset(dep,0,sizeof(dep));
	queue<int> que;
	que.push(S);
	dep[S] = 1;
	while(!que.empty())
	{
		int no = que.front();
		que.pop();
		for(int i=head[no];~i;i=edge[i].nxt)
		{
			int y = edge[i].y;
			if(edge[i].v&&!dep[y]){
				dep[y] = dep[no] + 1;
				que.push(y);
			}
		}
	}
	return dep[T];
}
__int64 dfs(int x,__int64 flow)
{
	if(x==T || !flow) return flow;
	__int64 used = 0;
	for(int& i=cur[x];~i;i=edge[i].nxt)
	{
		int y = edge[i].y;
		if(dep[x]+1==dep[y]){
			__int64 w = dfs(y,min(flow-used,edge[i].v));
			edge[i].v -= w;
			edge[i^1].v += w;
			used += w;
			if(used==flow) return flow;
		}
	}
	//if(!used) dep[x] = 0;
	return used;
}
__int64 maxflow()
{
	__int64 ans = 0;
	while(bfs()){
		copy(head,head+maxn,cur);
		ans += dfs(S,inf);
	}
	return ans;
}
int main()
{
	int t,cas=1;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		//scanf("%d%d",&S,&T);
		S = 0,T = n-1;
		int a,b,d;
		__int64 c;
		tot = 0;
		memset(head,-1,sizeof(head));
		for(int i=1;i<=m;i++){
			scanf("%d%d%I64d%d",&a,&b,&c,&d);
			AddEdge(a,b,c*(m+1)+1);
			AddEdge(b,a,0);
			if(d){
				AddEdge(a,b,0);
				AddEdge(b,a,c*(m+1)+1);				
			}
		}
		int ans = maxflow()%(m+1);
		printf("Case %d: %d\n",cas++,ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/a1214034447/article/details/81007043