Go Running(最小割,最小点覆盖)

题意

有若干个人在数轴上跑步,每个人可能往左跑,也可能往右跑,但是方向不能变。速度是(1 m/s)

现在给定(n)个报告,每个报告给出(t)时刻在(x)位置有人经过。

问至少有多少个人在跑步。

思路

  • 注:坐标旋转公式:

[egin{pmatrix} x \ y end{pmatrix} = egin{pmatrix} 1 & 1 \ 1 & -1 end{pmatrix} egin{pmatrix} a \ b end{pmatrix} ]

即,

[egin{cases} x = a + b, &\ y = a - b, & end{cases} ]

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

const int N = 200010, M = 4 * N, inf = 1e8;

int n, S, T;
int h[N], e[M], ne[M], f[M], idx;
int cur[N], d[N];
vector<int> nums;
pii report[N];

void add(int a, int b, int c)
{
    e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx ++;
    e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx ++;
}

bool bfs()
{
    memset(d, -1, sizeof(d));
    queue<int> que;
    que.push(S);
    d[S] = 0, cur[S] = h[S];
    while(que.size()) {
        int t = que.front();
        que.pop();
        for(int i = h[t]; ~i; i = ne[i]) {
            int ver = e[i];
            if(d[ver] == -1 && f[i]) {
                d[ver] = d[t] + 1;
                cur[ver] = h[ver];
                if(ver == T) return true;
                que.push(ver);
            }
        }
    }
    return false;
}

int find(int u, int limit)
{
    if(u == T) return limit;
    int flow = 0;
    for(int i = cur[u]; ~i && flow < limit; i = ne[i]) {
        cur[u] = i;
        int ver = e[i];
        if(d[ver] == d[u] + 1 && f[i]) {
            int t = find(ver, min(f[i], limit - flow));
            if(!t) d[ver] = -1;
            f[i] -= t, f[i ^ 1] += t, flow += t;
        }
    }
    return flow;
}

int dinic()
{
    int res = 0, flow;
    while(bfs()) {
        while(flow = find(S, inf)) {
            res += flow;
        }
    }
    return res;
}

int find(int x)
{
    return lower_bound(nums.begin(), nums.end(), x) - nums.begin() + 1;
}

int main()
{
    int TT;
    scanf("%d", &TT);
    while(TT --) {
        scanf("%d", &n);
        memset(h, -1, sizeof(h));
        idx = 0;
        nums.clear();
        for(int i = 1; i <= n; i ++) {
            int t, x;
            scanf("%d%d", &t, &x);
            report[i] = {t + x, t - x};
            nums.push_back(t + x);
            nums.push_back(t - x);
        }
        sort(nums.begin(), nums.end());
        nums.erase(unique(nums.begin(), nums.end()), nums.end());
        int m = nums.size();
        S = 0, T = 2 * m + 1;
        for(int i = 1; i <= m; i ++) {
            add(S, i, 1);
            add(m + i, T, 1);
        }
        for(int i = 1; i <= n; i ++) {
            int a = find(report[i].first), b = find(report[i].second);
            add(a, m + b, inf);
        }
        printf("%d
", dinic());
    }
    return 0;
}
原文地址:https://www.cnblogs.com/miraclepbc/p/14419406.html