zoukankan      html  css  js  c++  java
  • luogu P2756 飞行员配对方案问题(Dinic板子)

    建立一个超级源点,将每个外籍飞行员连一条capacity为1的路,一个超级汇点,每个英国飞行员也连一条capacity为1的路,根据读入在英国飞行员和外籍飞行员连接capacity为1的路,匹配方案就是最大流,遍历每一个外籍飞行员的连接,当有流时就输出即可

    #include<bits/stdc++.h>
    using namespace std;
    #define lowbit(x) ((x)&(-x))
    typedef long long LL;
    
    const int maxm = 3e4+5;
    const int INF = 0x3f3f3f3f;
    
    struct edge{
        int u, v, cap, flow, nex;
    } edges[maxm<<1];
    
    int head[maxm<<1], cur[maxm<<1], cnt, level[105];
    
    void init() {
        memset(head, -1, sizeof(head));
    }
    
    void add(int u, int v, int cap) {
        edges[cnt] = edge{u, v, cap, 0, head[u]};
        head[u] = cnt++;
    }
    
    void addedge(int u, int v, int cap) {
        add(u, v, cap), add(v, u, 0);
    }
    
    void bfs(int s) {
        memset(level, -1, sizeof(level));
        queue<int> q;
        level[s] = 0;
        q.push(s);
        while(!q.empty()) {
            int u = q.front();
            q.pop();
            for(int i = head[u]; i != -1; i = edges[i].nex) {
                edge& now = edges[i];
                if(now.cap > now.flow && level[now.v] < 0) {
                    level[now.v] = level[u] + 1;
                    q.push(now.v);
                }
            }
        }
    }
    
    int dfs(int u, int t, int f) {
        if(u == t) return f;
        for(int& i = cur[u]; i != -1; i = edges[i].nex) {
            edge& now = edges[i];
            if(now.cap > now.flow && level[u] < level[now.v]) {
                int d = dfs(now.v, t, min(f, now.cap - now.flow));
                if(d > 0) {
                    now.flow += d;
                    edges[i^1].flow -= d;
                    return d;
                }
    
            }
        }
        return 0;
    }
    
    int dinic(int s, int t) {
        int maxflow = 0;
        for(;;) {
            bfs(s);
            if(level[t] < 0) break;
            memcpy(cur, head, sizeof(head));
            int f;
            while((f = dfs(s, t, INF)) > 0)
                maxflow += f;
        }
        return maxflow;
    }
    
    void run_case() {
        int n, m, u, v, cap;
        init();
        cin >> m >> n;
        for(int i = 1; i <= m; ++i)
            addedge(0, i, 1);
        while(cin >> u >> v && (u+v)>0) {
            addedge(u, v, 1);
        }
        for(int i = m+1; i <= n; ++i)
            addedge(i, n+1, 1);
        int maxflow = dinic(0, n+1);
        if(maxflow == 0) {
            cout << "No Solution!
    ";
            return;
        }
        cout << maxflow << "
    ";
        for(int u = 1; u <= m; ++u) {
            for(int i = head[u]; i != -1; i = edges[i].nex)
                if(edges[i].flow) {cout << edges[i].u << " " << edges[i].v << "
    "; break;}
        }
    }
    
    int main() {
        ios::sync_with_stdio(false), cin.tie(0);
        run_case();
        //cout.flush();
        return 0;
    }
    Dinic
  • 相关阅读:
    xgzc— math 专题训练(一)
    floyd判圈算法
    CF961G Partitions
    luoguP4778 Counting swaps
    AT3913 XOR Tree(巧妙转换+状压dp)
    手动实现aop编程
    代理模式
    spring重点一:处理对象创建时间 个数以及方式
    spring helloword
    spring用来干什么,解决的问题
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12269043.html
Copyright © 2011-2022 走看看