HDU 6214 Smallest Minimum Cut(网络流 最小割最少边数)

来源:互联网 发布:赵洪文国 知乎 编辑:程序博客网 时间:2024/06/05 00:52
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 casesT (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 from1 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
 

题目大意:给定一个有向图,求边数最小的最小割。

青岛网赛的一道题目,万万没想到是原题啊。。。并不会做,赛后百度了一下就吐血了。。。

先跑一次最大流,满流边必为某最小割中的一条边(网上的题解这么说的)。然后改一下图,满流的边流量改为1,不满流的流量改为INF,再跑一次,该最小割对应的就是最小割的边数。题解上本来说反向边要初始化的,但是一直 WA,把反向边的初始化去掉以后就 A 了。先把代码挂这儿,抽时间再想想吧。

代码:

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#include<vector>using namespace std;typedef struct node{    int from,to,cap,flow;    node(int f = 0,int t = 0,int c = 0,int ff = 0):from(f),to(t),cap(c),flow(ff){}}node;const int INF = 1000000;const int maxn = 200 + 5;int n,s,t;vector<node> edge;vector<int> G[maxn];int cur[maxn],di[maxn];bool vis[maxn];void addedge(int from,int to,int cap,int flow){    edge.push_back(node(from,to,cap,0));    edge.push_back(node(to,from,0,0));    int m = edge.size();    G[from].push_back(m-2);    G[to].push_back(m-1);}bool BFS(){    int x;    memset(vis,false,sizeof(vis));    queue<int> q;    q.push(s);    vis[s] = true;    di[s] = 0;    while(!q.empty())    {        x = q.front();        q.pop();        for(int i = 0;i < G[x].size(); ++i)        {            node &e = edge[G[x][i]];            if(e.cap > e.flow && !vis[e.to])            {                di[e.to] = di[x] + 1;                vis[e.to] = true;                q.push(e.to);            }        }    }    return vis[t];}int DFS(int x,int a){    int flow = 0,f;    if(x == t || a == 0) return a;    for(int &i = cur[x];i< G[x].size(); ++i)    {        node &e = edge[G[x][i]];        if(di[e.to] == di[x] + 1 && (f = DFS(e.to,min(a,e.cap - e.flow))) > 0)        {            e.flow += f;            edge[G[x][i]^1].flow -= f;            flow += f;            a -= f;            if(a == 0) break;        }    }    return flow;}int maxflow(){    int flow = 0;    while(BFS())    {        memset(cur,0,sizeof(cur));        flow += DFS(s,INF);    }    return flow;}void Init(){    edge.clear();    for(int i = 0;i <= n; ++i) G[i].clear();}int main(){    int T;    int x,y,z,m;    scanf("%d",&T);    while(T--)    {        scanf("%d %d %d %d",&n,&m,&s,&t);        Init();        for(int j = 0;j < m; ++j)        {            scanf("%d %d %d",&x,&y,&z);            addedge(x,y,z,0);        }        maxflow();        int len = edge.size();        for(int i = 0;i < len; i += 2)        {            if(edge[i].cap == edge[i].flow)            {                edge[i].cap = 1;                edge[i].flow = 0;            }            else            {                edge[i].cap = INF;                edge[i].flow = 0;            }        }        printf("%d\n",maxflow());    }    return 0;}

阅读全文
0 0
原创粉丝点击