POJ 1815 最小割 拆点 枚举

求一个最小割点集,拆点,连一条自己出点到入点的容量为1的边,s,t连无穷边,保证不被割集割到,然后从1到N枚举删除点,求最小割,如果求得的最小割变小,则更新最小割,并且将该点记为删除,以后枚举的是偶构图不考虑该点,然后记录答案

要注意0的时候不要输出序列的那一行

#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef  long long LL;
const double PI = acos(-1.0);

template <class T> inline  T MAX(T a, T b){if (a > b) return a;return b;}
template <class T> inline  T MIN(T a, T b){if (a < b) return a;return b;}

const int N = 422;
const int M = 41111;
const LL MOD = 1000000007LL;
const int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
const int INF = 0x3f3f3f3f;

struct arclist {
    struct edge{double cap; int v, next;} E[N + M << 1];
    int head[N], cur;
    inline void init()
    {
        memset(head, -1, sizeof(head)); cur = 0;
    }
    inline void add(int u, int v, double cap = 1) {
        E[cur].v = v; E[cur].cap = cap;
        E[cur].next = head[u]; head[u] = cur++;
        E[cur].v = u; E[cur].cap = 0;
        E[cur].next = head[v]; head[v] = cur++;
    }
    double SAP(int S, int T, int n)
    {
        double maxflow = 0;
        int pre[N], dis[N] = {}, gap[N] = {};
        int cur[N]; memcpy(cur, head, sizeof(cur));
        gap[0] = n + 1; ++gap[dis[S] = 1];
        for (int u = pre[S] = S; dis[S] <= n; ++gap[++dis[u]], u = pre[u])
        {
            for (bool flag = true; flag;)
            {
                flag = false;
                for (int &p = cur[u]; ~p; p = E[p].next)
                {
                    if (!E[p].cap || dis[u] != dis[E[p].v] + 1) continue;
                    flag = true; pre[E[p].v] = u; u = E[p].v;
                    if (u == T)
                    {
                        double aug = INF;
                        for (int i = S; i != T; i = E[cur[i]].v)
                            if (aug > E[cur[i]].cap)
                            {
                                u = i; aug = E[cur[i]].cap;
                            }
                        for (int i = S; i != T; i = E[cur[i]].v)
                        {
                            E[cur[i]].cap -= aug;
                            E[cur[i] ^ 1].cap += aug;
                        }
                        maxflow += aug;
                    }
                    break;
                }

            }
            if (--gap[dis[u]] == 0) break;
            dis[u] = n;
            for (int p = head[u]; ~p; p = E[p].next)
                if (E[p].cap && dis[u] > dis[E[p].v]) {dis[u] = dis[E[p].v]; cur[u] = p;}
        }
        return maxflow;
    }
}flow;

int grid[222][222];
bool del[222];
int n, s, t;


void build()
{
    int i, j;
    flow.init();
    for (i = 1; i <= n; ++i)
    {
//        flow.add(0, i, INF);
//        flow.add(i + n, n + n + 1, INF);

        for (j = 1; j <= n; ++j)
            if (grid[i][j]) flow.add(i + n, j, INF);
        if (del[i] || i == s || i == t) continue;
        flow.add(i, i + n);
    }
    flow.add(s, s + n, INF);
    flow.add(t, t + n, INF);
}


int main()
{

    while (scanf("%d%d%d", &n, &s, &t) != EOF)
    {
        int i, j, k, seq[222];
        memset(del, false, sizeof(del));
        for (i = 1; i <= n; ++i)
            for (j = 1; j <= n; ++j)
                scanf("%d", &grid[i][j]);
        if (grid[s][t]) {printf("NO ANSWER!\n"); continue;}
        int ans = 0;
        build();
        int pre = flow.SAP(s + n, t, 2 * n);
        for (i = 1; i <= n; ++i)
        {
            if (i == s || i == t) continue;
            del[i] = true;
            build();
            int now = flow.SAP(s + n, t, n * 2);
            if (now < pre)
            {
                seq[ans++] = i;
                pre = now;
            }
            else del[i] = false;
            if (now == 0) break;
        }
        if (ans == 0) printf("%d\n", ans);
        else
        {
            printf("%d\n%d", ans, seq[0]);
            for (i = 1; i < ans; ++i)
                printf(" %d", seq[i]);
            printf("\n");
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值