社区讨论

萌新刚学OI,求大佬指错

P2756飞行员配对方案问题参与者 5已保存回复 9

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
9 条
当前快照
1 份
快照标识符
@mi6xaank
此快照首次捕获于
2025/11/20 12:19
4 个月前
此快照最后确认于
2025/11/20 12:19
4 个月前
查看原帖
63分
CPP
//#include <iostream>
#include <cstdio>
#include <queue>

#define MAXN 110
#define mod 1000000007
#define INF 2147483600
#define rep(i, a, b) for(register int i = a; i <= b; i++)
#define foi(i, a, b) for(register int i = a; i >= b; i--)
typedef long long ll;

namespace Own_math_temp{
template<class _Tp> inline _Tp max(_Tp x, _Tp y) { return x > y ? x : y; }
template<class _Tp> inline _Tp min(_Tp x, _Tp y) { return x < y ? x : y; }
template<class _Tp> inline _Tp abs(_Tp x) { return x > _Tp(0) ? x : _Tp(0) - x; }
}

//using Own_math_temp::max;
using Own_math_temp::min;
//using Own_math_temp::abs;
using std::queue;

/*template<class _Tp> _Tp*/
int read() {
    /*_Tp*/int ans = 0; char ch = getchar(), t = ch;
    while (ch < '0' || ch > '9') { t = ch; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { ans = ans * 10 + ch - '0'; ch = getchar(); }
    /*if (ch == '.') {
        int l = 0.1; ch = getchar();
        while (ch >= '0' && ch <= '9') {
            ans = ans + (ch - '0') * l;l *= 0.1; ch = getchar();
        }
    }*/
    if (t == '-') ans = -ans; return ans;
}

int head[MAXN], deep[MAXN], sum = 1, s, t, n, m;
struct node {
    int pre, next, cost;
    inline void add(int x, int y, int z) {
        pre = y; cost = z;
        next = head[x]; head[x] = sum;
    }
}e[MAXN * MAXN * 2];
queue<int>q;

bool bfs() {
    while (!q.empty()) q.pop();
    rep(i, 1, m) deep[i] = 0;
    q.push(s); deep[s] = 1;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int i = head[u]; i; i = e[i].next) {
            int v = e[i].pre;
            if (!deep[v] && e[i].cost) {
                deep[v] = deep[u] + 1;
                q.push(v);
                if (v == t) return true; 
            }
        }
    }
    return false;
}

int dinic(int u, int flow) {
    if (u == t) return flow;
    int ret = flow, k = 0;
    for (int i = head[u]; i && ret; i = e[i].next) {
        int v = e[i].pre;
        if (deep[v] == deep[u] + 1 && e[i].cost) {
            k = dinic(v, min(ret, e[i].cost));
            if (!k) deep[v] = 0;
            e[i].cost -= k; e[i ^ 1].cost += k;
            ret -= k;
        }
    }
    return flow - ret;
}

int main() {
/*	freopen(".in", "r" ,stdin);
    freopen(".out", "w" ,stdout);*/
    m = read(), n = read(); s = 0, t = n + 1;
    while (1) {
        int x = read(), y = read();
        if (x == -1 && y == -1) break;
        if (x <= m) e[++sum].add(x, y, INF), e[++sum].add(y, x, 0);
        else e[++sum].add(y, x, INF), e[++sum].add(x, y, 0);
    }
    rep(i, 1, m) e[++sum].add(s, i, 1), e[++sum].add(i, s, 0);
    rep(i, m + 1, n) e[++sum].add(i, t, 1), e[++sum].add(t, i, 0);
    int flow = 0, maxflow = 0;
    while (bfs()) while (flow = dinic(s, INF)) maxflow += flow;
    printf("%d\n", maxflow);
    for (int i = head[t]; i; i = e[i].next) {
        if (e[i].cost) for (int j = head[e[i].pre]; j; j = e[j].next) {
            if (e[j].cost) printf("%d %d\n", e[j].pre, e[i].pre);
        }
    }
    return 0;
}

回复

9 条回复,欢迎继续交流。

正在加载回复...