zoukankan      html  css  js  c++  java
  • 企鹅游行(最大流,拆点,枚举)

    题意

    思路

    将企鹅个数看作流量。

    考虑转移方式,如果两块浮冰之间距离在企鹅跳跃距离以内,那么这两块浮冰之间就是可以互相转移的,因此可以互相连容量是(infty)的边。

    由于每块浮冰都有跳跃次数限制,因此考虑拆点,拆成入点和出点,入点向出点连容量大小等于跳跃次数限制的边。

    设置虚拟源点(S),连向每块浮冰,容量是每块浮冰的企鹅个数。

    枚举每块浮冰作为汇点,跑最大流,若流量等于企鹅总数,则满足条件。

    代码

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    
    #define x first
    #define y second
    
    using namespace std;
    
    typedef pair<int, int> pii;
    
    const int N = 210, M = (100 + 100 + N * N) * 2, inf = 1e8;
    const double eps = 1e-8;
    
    int n, S, T;
    double D;
    int h[N], e[M], f[M], ne[M], idx;
    int cur[N], d[N];
    pii p[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 main()
    {
        int cas;
        scanf("%d", &cas);
        while(cas --) {
            scanf("%d%lf", &n, &D);
            memset(h, -1, sizeof(h));
            idx = 0;
            S = 0;
            int tot = 0;
            for(int i = 1; i <= n; i ++) {
                int x, y, a, b;
                scanf("%d%d%d%d", &x, &y, &a, &b);
                p[i] = {x, y};
                add(S, i, a);
                add(i, i + n, b);
                tot += a;
            }
            for(int i = 1; i <= n; i ++) {
                for(int j = i + 1; j <= n; j ++) {
                    double dx = p[i].x - p[j].x, dy = p[i].y - p[j].y;
                    if(dx * dx + dy * dy <= D * D + eps) {
                        add(n + i, j, inf);
                        add(n + j, i, inf);
                    }
                }
            }
            int cnt = 0;
            for(int i = 1; i <= n; i ++) {
                T = i;
                for(int j = 0; j < idx; j += 2) {
                    f[j] += f[j ^ 1];
                    f[j ^ 1] = 0;
                }
                if(tot == dinic()) {
                    printf("%d ", i - 1);
                    cnt ++;
                }
            }
            if(!cnt) puts("-1");
            else puts("");
        }
        return 0;
    }
    
  • 相关阅读:
    最长公共子序列
    小测试 炒书
    洛谷P1968 美元汇率
    洛谷P3611 [USACO17JAN]Cow Dance Show奶牛舞蹈
    【刷题】【树】【模拟】树网的核
    【刷题】【dp】地精的贸易
    【刷题】【模拟】复制cs
    【刷题】【模拟】3n+1
    【刷题】【dp】中国象棋
    【刷题】【搜索】新数独
  • 原文地址:https://www.cnblogs.com/miraclepbc/p/14404057.html
Copyright © 2011-2022 走看看