HDU 6214 Smallest Minimum Cut【最小割的最小边数】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214

题目大意:n个点,m条边,给定起点s和终点t、有向图u-v的权值,求从s-t不连通所删除的边数权值之和最小,当权值一样时,边数最小,输出要删除边的数目。

分析:

对于第一种,即使所有的边都算上,也就E条,最大流结果%(E+1)就是最小割的边数。

对于第二种,要保证s-t不连通,肯定要删除跑完最大流后满流的边,在原来最大流的基础上修改边的容量,再跑一遍最大流即是要删的边的数目。

Code One:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long LL;
using namespace std;
const int maxn=208;
const int maxm=3008;
struct edge
{
    int to,cap,rev;
};
vector<edge>G[maxn];
int iter[maxn];
int level[maxn];
void addedge(int from,int to,int cap)
{
    G[from].push_back((edge)
    {
        to,cap*(maxm+1)+1,G[to].size()
    });
    G[to].push_back((edge)
    {
        from,0,G[from].size()-1
    });
}
void bfs(int s)
{
    memset(level,-1,sizeof(level));
    queue<int>que;
    level[s]=0;
    que.push(s);
    while(!que.empty())
    {
        int v=que.front();
        que.pop();
        for(int i=0; i<G[v].size(); i++)
        {
            edge &e=G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}
int dfs(int v,int t,int f)
{
    if(v==t)
        return f;
    for(int &i=iter[v]; i<G[v].size(); i++)
    {
        edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to])
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t)
{
    int flow=0;
    for(;;)
    {
        bfs(s);
        if(level[t]<0)
            return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,INF))>0)
        {
            flow+=f;
        }
    }
}
int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        int s,t,u,v,w;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
            G[i].clear();
        scanf("%d%d",&s,&t);
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
        }
        int ans=max_flow(s,t);
        cout<<ans%(maxm+1)<<endl;
    }
    return 0;
}
Code Two:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<math.h>
#define INF 0x3f3f3f3f
#define lson l,m,root<<1
#define rson m+1,r,root<<1|1
typedef long long LL;
using namespace std;
const int maxn=2008;
const int maxm=200008;
const int mod=1000000007;
struct edge
{
    int to,next,cap,flow,cost;
} edge[maxm];
int head[maxn],tol;
int pre[maxn],dis[maxn];
bool vis[maxn];
int N;
void init(int n)
{
    N=n;
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost)
{
    edge[tol].to=v;
    edge[tol].cap=cap;
    edge[tol].cost=cost;
    edge[tol].flow=0;
    edge[tol].next=head[u];
    head[u]=tol++;
    edge[tol].to=u;
    edge[tol].cap=0;
    edge[tol].cost=-cost;
    edge[tol].flow=0;
    edge[tol].next=head[v];
    head[v]=tol++;
}
bool spfa(int s,int t)
{
    queue<int>q;
    for(int i=0; i<N; i++)
    {
        dis[i]=INF;
        vis[i]=false;
        pre[i]=-1;
    }
    dis[s]=0;
    vis[s]=true;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
            {
                dis[v]=dis[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t]==-1)
        return false;
    else
        return true;
}
int minCostMaxflow(int s,int t,int &cost)
{
    int flow=0;
    cost=0;
    while(spfa(s,t))
    {
        int Min=INF;
        for(int i=pre[t]; i!=-1; i=pre[edge[i^1].to])
        {
            if(Min>edge[i].cap-edge[i].flow)
                Min=edge[i].cap-edge[i].flow;
        }
        for(int i=pre[t]; i!=-1; i=pre[edge[i^1].to])
        {
            edge[i].flow+=Min;
            edge[i^1].flow-=Min;
            cost+=edge[i].cost*Min;
        }
      //  cout<<flow<<"* ";
        flow+=Min;
    }
    //cout<<cost<<"***"<<flow<<endl;
    return flow;
}
int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        int s,t,u,v,w;
        scanf("%d%d",&n,&m);
        init(maxn);
        scanf("%d%d",&s,&t);
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w,0);
        }
        int cost=0;
        minCostMaxflow(s,t,cost);
        for(int i=0;i<tol;i+=2)
        {
            if(edge[i].cap==edge[i].flow)
                edge[i].cap++;
            else
                edge[i].cap=INF;
        }
        int flow=minCostMaxflow(s,t,cost);
        cout<<flow<<endl;
    }
    return 0;
}

Smallest Minimum Cut

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


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值