网络流24题(03)最小路径覆盖问题(二分图匹配 + 最大流)

思路:

1. 把每个点拆分成 2 个点 Xi, Yi,由 s 向 Xi 引弧,Yi 向 t 引弧,如果 Xi, Yj 存在弧则引弧。所有弧的容量均为 1;

2. 这样就构造出来了二分图的模型,然后求最大流即是这个二分图的最大匹配了。路径数 = 点数 - 最大流;

3. 因为如果存在一个匹配边,则被覆盖的点数就会减 1,所以此时路径数就是如 2 中求得;

 

#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

const int MAXN = 1010;
const int INFS = 0x3FFFFFFF;

struct edge {
    int from, to, cap, flow;
    edge(int _from, int _to, int _cap, int _flow) 
        : from(_from), to(_to), cap(_cap), flow(_flow) {}
};

class Dinic {
public:
    void initdata(int n, int s, int t) {
        this->n = n, this->s = s, this->t = t;
        edges.clear();
        for (int i = 0; i < n; i++)
            G[i].clear();
    }
    void addedge(int u, int v, int cap) {
        edges.push_back(edge(u, v, cap, 0));
        edges.push_back(edge(v, u, 0, 0));
        G[u].push_back(edges.size() - 2);
        G[v].push_back(edges.size() - 1);
    }
    bool BFS() {
        for (int i = 0; i < n; i++)
            vis[i] = false, d[i] = 0;
        queue<int> Q;
        Q.push(s);
        vis[s] = true;
        while (!Q.empty()) {
            int x = Q.front(); Q.pop();
            for (int i = 0; i < G[x].size(); i++) {
                edge& e = edges[G[x][i]];
                if (e.cap > e.flow && !vis[e.to]) {
                    vis[e.to] = true;
                    d[e.to] = d[x] + 1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int DFS(int x, int aug) {
        if (x == t || aug == 0) return aug;
        int flow = 0;
        for (int i = 0; i < G[x].size(); i++) {
            edge& e = edges[G[x][i]];
            if (d[e.to] == d[x] + 1) {
                int f = DFS(e.to, min(aug, e.cap - e.flow));
                if (f <= 0) continue;
                e.flow += f;
                edges[G[x][i]^1].flow -= f;
                flow += f;
                aug -= f;
                if (aug == 0) break;
            } 
        }
        return flow;
    }
    int maxflow() {
        int flow = 0;
        while (BFS()) {
            flow += DFS(s, INFS);
        }
        return flow;
    }
    void print(int x) {
        vis[x] = true;
        int num = (n-2)/2;
        for (int i = 0; i < G[x].size(); i++) {
            edge& e = edges[G[x][i]];
            if (!vis[e.to] && e.to != t && e.flow == 1) {
                printf(" %d", e.to - num); print(e.to - num);
            }
        }
    }
    void printpath() {
        int flow = maxflow();
        memset(vis, false, sizeof(vis));
        int num = (n-2)/2;
        for (int i = 1; i <= num; i++) {
            if (!vis[i]) {
                printf("%d", i);
                print(i); printf("\n");
            }
        }
        printf("%d\n", num - flow);
    }
private:
    vector<edge> edges;
    vector<int> G[MAXN];
    int n, s, t, d[MAXN];
    bool vis[MAXN];
};

Dinic dinic;

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    int s = 0, t = 2*n + 1;
    dinic.initdata(t+1, s, t);
    for (int i = 1; i <= n; i++) {
        dinic.addedge(s, i, 1);
        dinic.addedge(i + n, t, 1);
    }
    while (m--) {
        int u, v;
        scanf("%d%d", &u, &v);
        dinic.addedge(u, v + n, 1);
    }
    dinic.printpath();
    return 0;
}
posted @ 2013-04-17 22:26  kedebug  阅读(346)  评论(0编辑  收藏  举报