uva10480 最大流最小割定理

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1421
题意:
求最小割的边。

方法:
跑最大流,最后跑完后,S-T割,将与源点相连的点分到一个集合,与汇点相连的点分到另一个集合。如果边的两个点分别在不同的集合中,那么这个就是最小割上的边。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
typedef pair<int,int> pii;
const int M_node = 1009, M_edge = 10090,INF = 0x3f3f3f3f;
int n,m,s,t,num;
int level[M_node];
pii node[M_edge];
struct edge
{
    int to,cap,next;
}edge[M_edge];
int head[M_node];
void init()
{
    memset(head,-1,sizeof(head));
    num = 0;
}
void add_edge(int u,int v,int cap)
{
    edge[num].to = v;
    edge[num].cap = cap;
    edge[num].next = head[u];
    head[u] = num++;

    edge[num].to = u;
    edge[num].cap = 0;
    edge[num].next = head[v];
    head[v] = num++;
}
bool bfs(int s,int t)
{
    memset(level,-1,sizeof(level));
    level[s] = 0;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int v = q.front();
        q.pop();
        for(int i = head[v];i != -1;i = edge[i].next)
        {
            int u = edge[i].to;
            if(level[u] == -1 && edge[i].cap > 0)
            {
                level[u] = level[v] + 1;
                q.push(u);
            }
        }
    }
    if(level[t] != -1) return true;
    return false;
}
int dfs(int v,int t,int f)
{
    if(v == t) return f;
    for(int i = head[v];i != -1 ;i = edge[i].next)
    {
        int u = edge[i].to;
        if(level[u] > level[v] && edge[i].cap > 0)
        {
            int d = dfs(edge[i].to,t,min(f,edge[i].cap));
            if(d > 0)
            {
                edge[i].cap -= d;
                edge[i^1].cap += d;
                return d;
            }
        }
    }
    level[v] = -1; //优化
    return 0;
}
int dinic(int s,int t)
{
    int flow = 0;
    while(bfs(s,t))
    {
        int f = 0;
        while((f = dfs(s,t,INF)) > 0) flow += f;
    }
    return flow;
}
int main()
{
    while(scanf("%d%d",&n,&m) == 2)
    {
        if(n == 0 && m == 0) break;
        s = 1;
        t = 2;
        init();
        for(int i = 0;i < m;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add_edge(a,b,c);
            add_edge(b,a,c);
            node[i] = make_pair(a,b);
        }
        int flow = dinic(s,t);
        for(int i = 0;i < m;i++)
        {
            if(level[node[i].first] == -1 && level[node[i].second] != -1)
                printf("%d %d\n",node[i].first,node[i].second);
            else if(level[node[i].first] != -1 && level[node[i].second] == -1)
                printf("%d %d\n",node[i].first,node[i].second);
        }
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(uva10480 最大流最小割定理)