【网络流24题】【LOJ6000】搭配飞行员(二分图最大匹配,最大流Dinic)

problem

  • 给出一张二分图
  • 求最大匹配

solution

  • 新建一个源点s和汇点t
  • 从源点s到集合A各连一条边,容量为1
  • 从集合B到汇点t到各连一条边,容量为1
  • 让二分图内部的边容量为1

很容易发现,形成的新的n+2个点,n+m条边的网络的最大流量就是二分图的最大匹配数。

于是就变成了最大流模板。

codes

#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int maxn = 110, maxm = 5050<<1;

int n, m, s, t;
int tot=1, head[maxn], Next[maxm], ver[maxm], edge[maxm];
void AddEdge(int x, int y, int z){
    ver[++tot] = y;  edge[tot] = z;
    Next[tot] = head[x]; head[x] = tot;
    ver[++tot] = x;  edge[tot] = 0;
    Next[tot] = head[y];  head[y] = tot;
}

queue<int>q;
LL dep[maxn], maxflow;
bool bfs(){
    memset(dep,0,sizeof(dep));
    while(q.size())q.pop();
    q.push(s); dep[s] = 1;
    while(q.size()){
        int x = q.front();  q.pop();
        for(int i = head[x]; i; i = Next[i]){
            if(edge[i] && !dep[ver[i]]){
                q.push(ver[i]);
                dep[ver[i]] = dep[x]+1;
                if(ver[i] == t)return true;
            }
        }
    }
    return false;
}
int findpath(int x, int flow){
    if(x == t)return flow;
    int rest = flow;
    for(int i = head[x]; i && rest; i = Next[i]){
        if(edge[i] && dep[ver[i]]==dep[x]+1){
            int k = findpath(ver[i], min(rest, edge[i]));
            if(!k)dep[ver[i]] = 0;
            edge[i] -= k;
            edge[i^1] += k;
            rest -= k;
        }
    }
    return flow-rest;
}
int dinic(int s, int t){
    LL flow = 0;
    while(bfs())
        while(flow=findpath(s,1<<30))maxflow += flow;
    return maxflow;
}

int main(){
    cin>>n>>m;
    int a, b;
    while(cin>>a>>b){
        if(a>b)swap(a,b);
        AddEdge(a,b,1);
    }

    s = 0, t = n+1;
    for(int i = 1; i <= m; i++)AddEdge(s,i,1);
    for(int i = m+1; i <= n; i++)AddEdge(i,t,1);

    cout<'\n';
    return 0;
}

你可能感兴趣的:(算法)