hdu 6124 Smallest Minimum Cut - 最少边最小割-2017 ICPC网络赛青岛站 -1009

Smallest Minimum Cut

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 83    Accepted Submission(s): 21


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 (1T300) .
Each case describes a network
G , and the first line contains two integers n (2n200) and m (0m1000) 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 (1s,tn) corresponding to the source and sink.
Each of the next
m lines contains three integers u,v and w (1w255) 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
 

Source
 

/*
题意:求最小割中边数最少的边数

题解:
在建图时,每个边权乘以一个大的数E,然后加1,求出最大流后对E取模,就可以得到边数。
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;
typedef long long ll;
const ll maxv = 2000+10;
const ll INF = 0x3f3f3f3f;

struct edge
{
    ll to,cap,rev;
};
vector <edge> G[maxv];
ll step[maxv];//从源点到点x的距离
ll iter[maxv];//定点x的第几条边开始有用

void add_edge(ll from, ll to, ll cap)
{
    G[from].push_back((edge){to,cap,G[to].size()});
    G[to].push_back((edge){from,0,G[from].size()-1});
}

void bfs(ll s)
{
    memset(step,-1,sizeof(step));
    step[s]=0;
    queue <ll> q;
    q.push(s);
    while(!q.empty())
    {
        ll v=q.front();
        q.pop();
        for(ll i=0;i<G[v].size();++i)
        {
            edge &e = G[v][i];
            if(e.cap>0 && step[e.to]<0)
            {
                step[e.to]=step[v]+1;
                q.push(e.to);
            }
        }
    }

}


ll dfs(ll v,ll t, ll f)
{
    if(v==t) return f;

    for(ll &i=iter[v];i<G[v].size();++i)//这里是引用,i++的同时iter也++,其实相当于上个的used,不过不用判断了
    {
        edge &e = G[v][i];
        if(e.cap>0 && step[e.to]>step[v])
        {
            ll d=dfs(e.to,t,min(e.cap,f));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

ll max_flow(ll s,ll t)
{
    ll flow =0;
    for(;;)
    {
        bfs(s);

        if(step[t]<0) return flow;

        memset(iter,0,sizeof(iter));
        ll f;
        while((f = dfs(s,t,INF))>0)
            flow += f;
    }
}


ll n,m;
ll s,t;
ll w;
int main(){
    ll T;

    scanf("%lld",&T);
    while(T--){
        scanf("%lld%lld",&n,&m);
        scanf("%lld%lld",&s,&t);
        for(ll i = 0;i<maxv;++i) G[i].clear();

        for(ll i=0;i<m;++i){
            ll c,d;
            scanf("%lld%lld%lld",&c,&d,&w);
            add_edge(c,d,w*1000+1);
        }
        printf("%lld\n",max_flow(s,t)%1000);
    }

    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值