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

来源:互联网 发布:工程预算软件大全 编辑:程序博客网 时间:2024/06/06 01:45

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
24 51 41 2 31 3 12 3 12 4 13 4 24 51 41 2 31 3 12 3 12 4 13 4 3
 

Sample Output
23
 

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

思路:

原题。。。

最小割=最大流

建图时,每条边的容量*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
原创粉丝点击