「模板」「二分图匹配 => 最大流」飞行员配对方案问题

本文详细解析了二分图匹配问题,并通过最大流算法实现解决方案。阐述了如何构建二分图,设置边的容量,以及使用BFS和DFS进行增广路径查找,最终实现最大匹配数的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 题目链接:https://www.oj.swust.edu.cn/problem/show/1736
  • 题意:
    在这里插入图片描述
  • 思路:由题意得为二分图匹配。将S连向1~m,容量为1; 再将m+1 连向 T,容量为1;再将可配对的从1~m连向 m+1~n,容量为无穷大。
    • 如此一来,最大流就等于最大匹配数,在1~m 连向 m+1 ~ n之间的边中,容量减少(或者其反向边容量增大的)的都是 匹配方案里的边。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <math.h>
#define pi acos(-1)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 100 + 10;
const int maxm = 2500 + 10;
const int maxv = maxn * 2;
const int maxe = maxm * 2;
const int mod = 1e9 + 7;

int wi[maxv], wo[maxv];

//////

int S, T;
int head[maxe], tot=0, nowedge[maxe];
int dis[maxv];
int vis[maxv];
///////

struct Edge
{
    int to, next, cap;
}es[maxe];

int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void init()
{
    memset(head, -1, sizeof(head));
    memset(vis, 0, sizeof(vis));
    tot=0;
}

void add(int u, int v, int vol)
{
    es[tot].to = v;
    es[tot].cap = vol;
    es[tot].next = head[u];
    head[u] = tot++;

    es[tot].to = u;
    es[tot].cap = 0;
    es[tot].next = head[v];
    head[v] = tot++;
}

bool BFS()
{
    queue<int> que;
    memset(dis, -1, sizeof(dis));
    dis[S] = 0;
    que.push(S);
    int cur, v;
    while(!que.empty())
    {
        cur = que.front(); que.pop();
        for(int i=head[cur]; i!=-1; i=es[i].next){
            v = es[i].to;
            if(dis[v]==-1 && es[i].cap>0){
                dis[v] = dis[cur]+1;
                que.push(v);
            }
        }
    }
    if(dis[T] == -1) return false;
    else return true;
}

LL DFS(int cur, LL low)
{
    LL res=0, add=0;// 多路增广
    if(cur == T) return low;
    for(int i=nowedge[cur]; i!=-1; i=es[i].next){
        nowedge[cur] = i;
        if(dis[es[i].to]==dis[cur]+1
        && es[i].cap>0
        && (add = DFS(es[i].to, min(low, (LL)es[i].cap))) ){
            es[i].cap -= add;
            es[i^1].cap += add;
            low -= add;
            res += add;
            if(low == 0) break;
        }
    }
    if(res == 0) dis[cur] = 0;
    return res;
}

LL MAXFLOW()
{
    LL ans=0 , tmp=0;
    while(BFS()){
        for(int i=S; i<=T; i++){
            nowedge[i] = head[i];
        }
        if(tmp = DFS(S, INF))
            ans += tmp;
    }
    return ans;
}


int left_net(int cur)
{
    vis[cur] = 1;
    int ans = 1;
    for(int i=head[cur]; i!=-1; i = es[i].next){
        if(es[i].cap > 0 && !vis[es[i].to]) ans += left_net(es[i].to);
    }
    return ans;
}

int main()
{
    //--------------------------
    int n, m;
    init();
    scanf("%d%d", &m, &n);
    S = 0;
    T = n+1;
    //--------------------------
    for(int i=1; i<=m; i++) add(S, i, 1);
    for(int i=m+1; i<=n; i++) add(i, T, 1);
    int tmp1, tmp2;
    while(~scanf("%d%d", &tmp1, &tmp2)){
        if(tmp1==-1 && tmp2==-1) break;
        add(tmp1, tmp2, INF);
    }
    LL ans = MAXFLOW();
    cout << ans << '\n';
    if(ans == 0){
        printf("No Solution!\n");
        return 0;
    }
    /*
    for(int i=0; i<tot; i+=2){
        if(es[i].to != S && es[i^1].to != S){
            if(es[i].to != T && es[i^1].to !=T){
                if(es[i^1].cap != 0){
                    printf("%d %d\n", es[i^1].to, es[i].to);
                }
            }
        }
    }
    */


    for(int i=1; i<=m; i++){
        for(int j=head[i]; j!=-1; j=es[j].next){
            if(es[j].to == S || es[j].cap>=INF) continue;
            printf("%d %d\n", i, es[j].to);
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值