POJ 1815 Friendship 最小割

时间:2020-12-11 04:27:57

和POJ 1966差不多?
http://blog.csdn.net/huanghongxun/article/details/51056665

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f, N = 405, M = 10005;

int level[N], del[N], n, cnt = 0, v[M], w[M], p[M], h[N], q[M], s, t;
void add(int a, int b, int c) {
    p[++cnt] = h[a]; v[cnt] = b; w[cnt] = c; h[a] = cnt;
    p[++cnt] = h[b]; v[cnt] = a; w[cnt] = 0; h[b] = cnt;
}

bool bfs() {
    int f = 0, r = 0, u, i;
    memset(level, -1, sizeof level);
    q[r++] = s; level[s] = 1;
    while (f < r) {
        u = q[f++];
        for (i = h[u]; i; i = p[i]) {
            if (w[i] && level[v[i]] == -1) {
                level[v[i]] = level[u] + 1;
                q[r++] = v[i]; if (v[i] == t) return 1;
            }
        }
    }
    return false;
}

int dfs(int u, int low) {
    int i, tmp = 0; int res = 0;
    if (u == t) return low;
    for (i = h[u]; i && res < low; i = p[i]) {
        if (w[i] && level[v[i]] == level[u] + 1) {
            tmp = dfs(v[i], min((int) w[i], low - res));
            w[i] -= tmp; w[i ^ 1] += tmp; res += tmp;
        }
    }
    if (!res) level[u] = -1;
    return res;
}

int dinic() {
    int ans = 0;
    while (bfs()) ans += dfs(s, inf);
    return ans;
}

void reset() {
    for (int i = 2; i <= cnt; i += 2)
        w[i] += w[i ^ 1], w[i ^ 1] = 0;
}

int mp[N][N], ans[N];

int main() {
    int m, a, b, c, np, nc, i, j, k, num, ss, tt, mf;
    while (scanf("%d%d%d", &n, &ss, &tt) == 3) {
        num = 0;
        memset(del, 0, sizeof del);
        for (i = 1; i <= n; ++i)
            for (j = 1; j <= n; ++j)
                scanf("%d", &mp[i][j]);
        if (mp[ss][tt]) { puts("NO ANSWER!"); continue; }
        for (k = 0; k <= n; ++k) {
            if (k == ss || k == tt) continue;
            memset(h, 0, sizeof h); cnt = 1;
            del[k] = 1;
            for (i = 1; i <= n; ++i)
                if (!del[i])
                    add(i, i + n, 1);
            for (i = 1; i <= n; ++i)
                for (j = 1; j <= n; ++j)
                    if (mp[i][j])
                        add(i + n, j, 1);
            s = ss + n; t = tt;
            int d = dinic();
            if (!k) mf = d;
            else if (mf > d) mf = d, ans[++num] = k;
            else del[k] = 0;
        }
        printf("%d\n", num);
        if (num) {
            for (i = 1; i < num; ++i) printf("%d ", ans[i]);
            printf("%d\n", ans[num]);
        }
    }

    return 0;
}

Friendship

Description

In modern society, each person has his own friends. Since all the people are very busy, they communicate with each other only by phone. You can assume that people A can keep in touch with people B, only if
1. A knows B’s phone number, or
2. A knows people C’s phone number and C can keep in touch with B.
It’s assured that if people A knows people B’s number, B will also know A’s number.

Sometimes, someone may meet something bad which makes him lose touch with all the others. For example, he may lose his phone number book and change his phone number at the same time.

In this problem, you will know the relations between every two among N people. To make it easy, we number these N people by 1,2,…,N. Given two special people with the number S and T, when some people meet bad things, S may lose touch with T. Your job is to compute the minimal number of people that can make this situation happen. It is supposed that bad thing will never happen on S or T.

Input

The first line of the input contains three integers N (2<=N<=200), S and T ( 1 <= S, T <= N , and S is not equal to T).Each of the following N lines contains N integers. If i knows j’s number, then the j-th number in the (i+1)-th line will be 1, otherwise the number will be 0.

You can assume that the number of 1s will not exceed 5000 in the input.

Output

If there is no way to make A lose touch with B, print “NO ANSWER!” in a single line. Otherwise, the first line contains a single number t, which is the minimal number you have got, and if t is not zero, the second line is needed, which contains t integers in ascending order that indicate the number of people who meet bad things. The integers are separated by a single space.

If there is more than one solution, we give every solution a score, and output the solution with the minimal score. We can compute the score of a solution in the following way: assume a solution is A1, A2, …, At (1 <= A1 < A2 <…< At <=N ), the score will be (A1-1)*N^t+(A2-1)*N^(t-1)+…+(At-1)*N. The input will assure that there won’t be two solutions with the minimal score.

Sample Input

3 1 3
1 1 0
1 1 1
0 1 1

Sample Output

1
2

Source

POJ Monthly