现在的位置: 首页 > 综合 > 正文

01. 搭配飞行员 [网络流24题]

2018年12月24日 ⁄ 综合 ⁄ 共 1909字 ⁄ 字号 评论关闭

提交地址:http://cojs.tk/cogs/problem/problem.php?pid=14

分析:二分图的最大匹配,网络最大流。

思路: 

采用最大流算法。 增加源点和汇点。 

1、S向X集合中每个顶点连一条容量为1的有向边。
2、Y集合中每个顶点向T连一条容量为1的有向边。
3、XY集合之间的边都设为从A集合中的点到B集合之中的点,容量为1的有向边。

建好图之后, Dinic算法  or ISAP算法。


Code( Dinic算法):

#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
using namespace std;

const int MAXN = 100+10;
const int INF = 1000000000;
struct Edge {
    int from, to, cap, flow;
};

struct Dinic {
    int n, m, s, t;
    vector<Edge> edges; //边表.edges[e]和edges[e^1]互为反向弧
    vector<int> G[MAXN]; //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
    bool vis[MAXN]; //BFS使用
    int d[MAXN];  //从起点到i的距离
    int cur[MAXN]; //当前弧指针

    void ClearAll(int n) {
        for (int i = 0; i < n; i++) G[i].clear();
        edges.clear();
    }

    void AddEdge(int from, int to, int cap) {
        edges.push_back((Edge) {
            from, to, cap, 0
        });
        edges.push_back((Edge) {
            to, from, 0, 0
        });
        m = edges.size();
        G[from].push_back(m - 2);
        G[to].push_back(m - 1);
    }

    bool BFS() {//使用BFS计算出每一个点在残量网络中到t的最短距离d.
        memset(vis, 0, sizeof(vis));
        queue<int> Q;
        Q.push(s);
        vis[s] = 1;
        d[s] = 0;
        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 (!vis[e.to] && e.cap > e.flow) { //只考虑残量网络中的弧
                    vis[e.to] = 1;
                    d[e.to] = d[x] + 1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x, int a) {//使用DFS从S出发,沿着d值严格递减的顺序进行多路增广。
        if (x == t || a == 0) return a;
        int flow = 0, f;
        for (int& i = cur[x]; i < G[x].size(); i++) {
            Edge& e = edges[G[x][i]];
            if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
                e.flow += f;
                edges[G[x][i] ^ 1].flow -= f;
                flow += f;
                a -= f;
                if (a == 0) break;
            }
        }
        return flow;
    }

    int Maxflow(int s, int t) {
        this->s = s;
        this->t = t;
        int flow = 0;
        while (BFS()) {
            memset(cur, 0, sizeof(cur));
            flow += DFS(s, INF);
        }
        return flow;
    }

};
Dinic g;

int main() {
    int n, m, x, y, i, j;
    freopen("flyer.in", "r", stdin);
    freopen("flyer.out", "w", stdout);
    scanf("%d%d",&m, &n);
    g.ClearAll(m+2);
    for(i=n; i>0; --i) g.AddEdge(0,i,1);
    for(i=m; i>n; --i) g.AddEdge(i,m+1,1);
    while(scanf("%d%d",&x,&y)!=EOF) {
        g.AddEdge(x,y,1);
    }
    int flow = g.Maxflow(0,m+1);
    printf("%d\n",flow);
//        for(i=1; i<=n; i++)
//            for(j=0; j<g.G[i].size(); j++) {
//                Edge& e = g.edges[ g.G[i][j] ];
//                if(e.flow>0) {
//                    printf("%d %d\n",i, e.to);
//                }
//            }
    return 0;
}












抱歉!评论已关闭.