Smallest Minimum Cut hdu-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

让我们求 最小割 的割边 最少是多少

建图的一个技巧吧。 我们把每条边的边权*(m+1)+1, 然后直接最大流%(m+1) 就是答案了。

考虑之前有两个相同的 最小割,一个边数多,一个边数少, 没有改变权值之前,他们是一样的,如果我们同时 *(m+1)+1,那么就不会再相等了。还是考虑满流的情况,最大流还是 最小割,但是这里 的最小割只会是边数 最少的了,所以%(m+1) 就是答案了

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)

const int MAXN=210;
const int MAXM=2*1010;
const int INF=20000000;

//SAP模板
//注意从1开始,不要去用0
struct SAP {
	struct Edge {
		int from,to,next,cap,flow;
		Edge(int _from=0,int _to=0,int _cap=0,int _flow=0,int _next=0){
			from=_from,to=_to,next=_next,cap=_cap,flow=_flow;
		}
	} edge[MAXM]; //注意是 MAXM !!!!!!!!!!!!!!!!!!!!!!! 记住乘2 
	int tol;
	int head[MAXN];
	int gap[MAXN],dep[MAXN],cur[MAXN];
	void init() {
		tol = 0;
		memset(head,-1,sizeof(head));
	}
	void add_edge(int u,int v,int w,int rw = 0) {
		edge[tol]=Edge(u,v,w,0,head[u]);
		head[u] = tol++;
		edge[tol]=Edge(v,u,rw,0,head[v]);
		head[v] = tol++;
	}
	int Q[MAXN];
	void BFS(int start,int end) {
		memset(dep,-1,sizeof(dep));
		memset(gap,0,sizeof(gap));
		gap[0] = 1;
		int front = 0, rear = 0;
		dep[end] = 0;
		Q[rear++] = end;
		while(front != rear) {
			int u = Q[front++];
			for(int i = head[u]; i != -1; i = edge[i].next) {
				int v = edge[i].to;
				if(dep[v] != -1)continue;
				Q[rear++] = v;
				dep[v] = dep[u] + 1;
				gap[dep[v]]++;
			}
		}
	}

	int S[MAXN];
	int MaxFlow(int start,int end,int N) {//一共多少个点
		BFS(start,end);
		memcpy(cur,head,sizeof(head));
		int top = 0;
		int u = start;
		int ans = 0;
		while(dep[start] < N) {
			if(u == end) {
				int Min = INF;
				int inser;

				for(int i = 0; i < top; i++)
				if(Min > edge[S[i]].cap - edge[S[i]].flow) {
						Min = edge[S[i]].cap - edge[S[i]].flow;
						inser = i;
				}
				for(int i = 0; i < top; i++) {
					edge[S[i]].flow += Min;
					edge[S[i]^1].flow -= Min;
				}
				ans += Min;
				top = inser;
				u = edge[S[top]^1].to;
				continue;
			}
			bool flag = false;
			int v;
			for(int i = cur[u]; i != -1; i = edge[i].next) {
				v = edge[i].to;
				if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u]) {
					flag = true;
					cur[u] = i;
					break;
				}
			}
			if(flag) {
				S[top++] = cur[u];
				u = v;
				continue;
			}
			int Min = N;
			for(int i = head[u]; i != -1; i = edge[i].next)
				if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min) {
					Min = dep[edge[i].to];
					cur[u] = i;
				}
			gap[dep[u]]--;
			if(!gap[dep[u]])return ans;
			dep[u] = Min + 1;
			gap[dep[u]]++;
			if(u != start)u = edge[S[--top]^1].to;
		}
		return ans;
	}
}sap;



int main()
{
    int T;
    scanf("%d",&T);
    while(T--){

        sap.init();

        int n,m;
        scanf("%d %d",&n,&m);
        int st,ed;
        scanf("%d %d",&st,&ed);
        rep(i,0,m){
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            sap.add_edge(u,v,w*(m+1)+1);
        }

        int ans=sap.MaxFlow(st,ed,n);
        //printf("ans:%d\n",ans);
        printf("%d\n",ans%(m+1));
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值