【2017青岛网络赛】1009 Smallest Minimum Cut hdu6214 最小割 最大流模版

20 篇文章 0 订阅
9 篇文章 0 订阅

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
 

题意:求边数最少的最小割。

思路:

原题。。。

最小割=最大流

建图时,每条边的容量*k+1(k为任意一个大于容量的数。

最小割的最少边数为当前图的最小割%k。

因为原图的最小割中,只有边数最少的最小割才是现在的最小割。

现在的最小割=原图的最小割*k+边数。


#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
const int Ni = 1100;
const int MAX = 1<<26;
struct Edge{
    int u,v,c;
    int next;
}edge[20*Ni];
int n,m;
int edn;//边数
int p[Ni];//父亲
int d[Ni];
int sp,tp;//原点,汇点

void addedge(int u,int v,int c)
{
    edge[edn].u=u; edge[edn].v=v; edge[edn].c=c;
    edge[edn].next=p[u]; p[u]=edn++;
    
    edge[edn].u=v; edge[edn].v=u; edge[edn].c=0;
    edge[edn].next=p[v]; p[v]=edn++;
}
int bfs()
{
    queue <int> q;
    memset(d,-1,sizeof(d));
    d[sp]=0;
    q.push(sp);
    while(!q.empty())
    {
        int cur=q.front();
        q.pop();
        for(int i=p[cur];i!=-1;i=edge[i].next)
        {
            int u=edge[i].v;
            if(d[u]==-1 && edge[i].c>0)
            {
                d[u]=d[cur]+1;
                q.push(u);
            }
        }
    }
    return d[tp] != -1;
}
int dfs(int a,int b)
{
    int r=0;
    if(a==tp)return b;
    for(int i=p[a];i!=-1 && r<b;i=edge[i].next)
    {
        int u=edge[i].v;
        if(edge[i].c>0 && d[u]==d[a]+1)
        {
            int x=min(edge[i].c,b-r);
            x=dfs(u,x);
            r+=x;
            edge[i].c-=x;
            edge[i^1].c+=x;
        }
    }
    if(!r)d[a]=-2;
    return r;
}

int dinic(int sp,int tp)
{
    int total=0,t;
    while(bfs())
    {
        while(t=dfs(sp,MAX))
            total+=t;
    }
    return total;
}
int main()
{
    int n,m,ncase,s,t;
    scanf("%d",&ncase);
    while(ncase--)
    {
        edn=0;
        memset(p,-1,sizeof(p));
        scanf("%d%d",&n,&m);
        scanf("%d%d",&s,&t);
        sp = s,tp = t;
        for(int i = 0;i < m;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            addedge(a, b, c*332+1);
        }
        printf("%d\n",dinic(sp,tp)%332);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值