• bzoj4443


    二分+二分图匹配

    晚上脑子不太好使。。。

    行列模型,填充数量性质,种种迹象告诉我们这是二分图,但是我觉得好像不太科学就弃了网络流。。。

    二分第k大值,转化为求第n-k+1小值,二分求匹配判定即可。

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 810, inf = 1000000010;
    namespace IO 
    {
        const int Maxlen = N;
        char buf[Maxlen], *C = buf;
        int Len;
        inline void read_in()
        {
            Len = fread(C, 1, Maxlen, stdin);
            buf[Len] = '';
        }
        inline void fread(int &x) 
        {
            x = 0;
            int f = 1;
            while (*C < '0' || '9' < *C) { if(*C == '-') f = -1; ++C; }
            while ('0' <= *C && *C <= '9') x = (x << 1) + (x << 3) + *C - '0', ++C;
            x *= f;
        }
        inline void read(int &x)
        {
            x = 0;
            int f = 1; char c = getchar();
            while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
            while(c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); }
            x *= f;
        }
        inline void read(long long &x)
        {
            x = 0;
            long long f = 1; char c = getchar();
            while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
            while(c >= '0' && c <= '9') { x = (x << 1ll) + (x << 3ll) + c - '0'; c = getchar(); }
            x *= f;
        } 
    } using namespace IO;
    struct edge {
        int nxt, to, f;
    } e[N * N * 10];
    int n, m, cnt = 1, source, sink, k;
    int head[N], d[N], a[N][N], iter[N];
    inline void link(int u, int v, int f)
    {
        e[++cnt].nxt = head[u];
        head[u] = cnt;
        e[cnt].to = v;
        e[cnt].f = f;
    }
    inline void insert(int u, int v, int f)
    {
        link(u, v, f);
        link(v, u, 0);
    }
    inline bool bfs()
    {
        queue<int> q;
        memset(d, -1, sizeof(d));
        d[source] = 0;
        q.push(source);
        while(!q.empty())
        {
            int u = q.front();
            q.pop();
            for(int i = head[u]; i; i = e[i].nxt) if(e[i].f && d[e[i].to] == -1)
            {
                d[e[i].to] = d[u] + 1;
                q.push(e[i].to);
            }
        }
        return d[sink] != -1;
    }
    inline int dfs(int u, int delta)
    {
        if(u == sink) return delta;
        int ret = 0;
        for(int &i = iter[u]; i && delta; i = e[i].nxt) if(e[i].f && d[e[i].to] == d[u] + 1)
        {
            int flow = dfs(e[i].to, min(e[i].f, delta));
            e[i].f -= flow;
            e[i ^ 1].f += flow;
            delta -= flow;
            ret += flow;
        }
        return ret;
    }
    inline int dinic()
    {
        int ret = 0;
        while(bfs())
        {
            for(int i = source; i <= sink; ++i) iter[i] = head[i];
            ret += dfs(source, inf);
        }
        return ret;
    }
    inline bool check(int mid)
    {
        memset(head, 0, sizeof(head));
        cnt = 1;
        for(int i = 1; i <= n; ++i)
        {
            insert(source, i, 1);
            for(int j = 1; j <= m; ++j) if(a[i][j] <= mid)
                insert(i, j + n, 1);
        }
        for(int i = 1; i <= m; ++i) insert(i + n, i + m + n, 1), insert(i + m + n, sink, 1);
        int ret = dinic();
        return ret >= n - k + 1;
    }
    int main()
    {
        read(n);
        read(m);
        read(k);
        sink = 2 * m + n + 1;
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= m; ++j) read(a[i][j]);
        int l = 0, r = inf + 10, ans = 0;
        while(r - l > 1)
        {
            int mid = (l + r) >> 1;
            if(check(mid)) r = ans = mid;
            else l = mid;
        }   
        printf("%d
    ", ans);
        return 0;
    }
    View Code
  • 相关阅读:
    安装MySQL时出现黄色感叹号,提示3306已被占用
    python使用xlrd读取excel数据时,整数变小数的解决办法
    Xenu Link Sleuth 简单好用的链接测试工具 使用说明
    python的with用法(参考)
    关于Selenium HTMLTestRunner 无法生成测试报告
    关于python-生成HTMLTestRunner测试报告
    如何出(改编)一道ACM算法题?
    近期思考(2019.07.20)
    爱,死亡和机器人 第十四集 齐马蓝 中文字幕(Python处理utf8文件获取想要的内容)
    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排的分割操作)
  • 原文地址:https://www.cnblogs.com/19992147orz/p/7414133.html
Copyright © 2020-2023  润新知