HDU 6214.Smallest Minimum Cut 最少边数最小割

Smallest Minimum Cut

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


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
 
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6216 6215 6214 6213 6212 
 
题意:求最少边数的最小割
注意:增广路和最小割集没有关系
方案一:先跑一边最大流,如果边满流,容量+1;否者,容量inf。再跑一遍最大流
方案二:对容量进行处理,容量*(大数或者边数M+1)+1,跑一边最大流Maxflow,处理前的最大流为Maxflow/(M+1),最少边数的最小割集为Maxflow%(M+1)。说明:原图的最小割为为x1、x2、x3...,即∑x,处理后的最小割为∑x*(M+1)+B,其中B为原图最小割边的数量,如果能有边数更少的情况,处理后最小割也会相应的减少,所以处理后得到的B即为最少边数。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<bitset>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define bug(x) cout<<"bug"<<x<<endl;
#define PI acos(-1.0)
#define eps 1e-8
const int N=1e5+100,M=1e5+100;
const int inf=0x3f3f3f3f;
const ll INF=1e18+7,mod=1e9+7;
struct edge
{
    int from,to,cap,flow;
};
vector<edge>es;
vector<int>G[N];
bool vis[N];
int dist[N];
int iter[N];
void init(int n)
{
    for(int i=0; i<=n+10; i++) G[i].clear();
    es.clear();
}
void addedge(int from,int to,int cap)
{
    es.push_back((edge)
    {
        from,to,cap,0
    });
    es.push_back((edge)
    {
        to,from,0,0
    });
    int x=es.size();
    G[from].push_back(x-2);
    G[to].push_back(x-1);
}
bool BFS(int s,int t)
{
    memset(vis,false,sizeof(vis));
    queue <int> Q;
    vis[s]=true;
    dist[s]=0;
    Q.push(s);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        for (int i=0; i<G[u].size(); i++)
        {
            edge &e=es[G[u][i]];
            if (!vis[e.to]&&e.cap>e.flow)
            {
                vis[e.to]=1;
                dist[e.to]=dist[u]+1;
                Q.push(e.to);
            }
        }
    }
    return vis[t];
}
int DFS(int u,int t,int f)
{
    if(u==t||f==0) return f;
    int flow=0,d;
    for(int &i=iter[u]; i<G[u].size(); i++)
    {
        edge &e=es[G[u][i]];
        if(dist[u]+1==dist[e.to]&&(d=DFS(e.to,t,min(f,e.cap-e.flow)))>0)
        {
            e.flow+=d;
            es[G[u][i]^1].flow-=d;
            flow+=d;
            f-=d;
            if(f==0) break;
        }
    }
    return flow;
}
int Maxflow(int s,int t)
{
    int flow=0;
    while(BFS(s,t))
    {
        memset(iter,0,sizeof(iter));
        int d=0;
        while(d=DFS(s,t,inf)) flow+=d;
    }
    return flow;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m,s,t;
        scanf("%d%d",&n,&m);
        scanf("%d%d",&s,&t);
        for(int i=1; i<=m; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
        }
        Maxflow(s,t);
        for(int i=0; i<es.size(); i+=2)
        {
            if(es[i].flow==es[i].cap) es[i].cap++;
            else es[i].cap=inf;
        }
        printf("%d
",Maxflow(s,t));
        init(n);
    }
    return 0;
}
方案一
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<bitset>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define bug(x) cout<<"bug"<<x<<endl;
#define PI acos(-1.0)
#define eps 1e-8
const int N=1e5+100,M=1e5+100;
const int inf=0x3f3f3f3f;
const ll INF=1e18+7,mod=1e9+7;
struct edge
{
    int from,to,cap,flow;
};
vector<edge>es;
vector<int>G[N];
bool vis[N];
int dist[N];
int iter[N];
void init(int n)
{
    for(int i=0; i<=n+10; i++) G[i].clear();
    es.clear();
}
void addedge(int from,int to,int cap)
{
    es.push_back((edge)
    {
        from,to,cap,0
    });
    es.push_back((edge)
    {
        to,from,0,0
    });
    int x=es.size();
    G[from].push_back(x-2);
    G[to].push_back(x-1);
}
bool BFS(int s,int t)
{
    memset(vis,false,sizeof(vis));
    queue <int> Q;
    vis[s]=true;
    dist[s]=0;
    Q.push(s);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        for (int i=0; i<G[u].size(); i++)
        {
            edge &e=es[G[u][i]];
            if (!vis[e.to]&&e.cap>e.flow)
            {
                vis[e.to]=1;
                dist[e.to]=dist[u]+1;
                Q.push(e.to);
            }
        }
    }
    return vis[t];
}
int DFS(int u,int t,int f)
{
    if(u==t||f==0) return f;
    int flow=0,d;
    for(int &i=iter[u]; i<G[u].size(); i++)
    {
        edge &e=es[G[u][i]];
        if(dist[u]+1==dist[e.to]&&(d=DFS(e.to,t,min(f,e.cap-e.flow)))>0)
        {
            e.flow+=d;
            es[G[u][i]^1].flow-=d;
            flow+=d;
            f-=d;
            if(f==0) break;
        }
    }
    return flow;
}
int Maxflow(int s,int t)
{
    int flow=0;
    while(BFS(s,t))
    {
        memset(iter,0,sizeof(iter));
        int d=0;
        while(d=DFS(s,t,inf)) flow+=d;
    }
    return flow;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m,s,t;
        scanf("%d%d",&n,&m);
        scanf("%d%d",&s,&t);
        for(int i=1; i<=m; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w*(m+1)+1);
        }
        printf("%d
",Maxflow(s,t)%(m+1));
        init(n);
    }
    return 0;
}
方案二
原文地址:https://www.cnblogs.com/GeekZRF/p/7553280.html