• [SCOI2015] 小凸玩矩阵


    题目链接

    题意

      在一个 $n imes m$ 的矩阵 $A$ 中取出 $n$ 个数,任意两个数不能在同一行或同一列,求取出的第 $k$ 大数的最小值。

    数据范围

      $1 leq K leq N leq M leq 250$,$1 leq A_{i,j} leq 10^9$

    分析

      如果只是简单的取数,很容易想到把行和列分别放入两个点集做二分图

      但是这里要求了第 $k$ 大数最小,所以我们可以二分枚举答案,判断该值是否成立

      这也就相当于判断在小于等于该值的数中,能否取出合法的 $n-k+1$ 个数

      但如果是在小于该值的数中取出 $n-k$ 个数,这样判断是否可行

      实际上与枚举出的值相同的数不一定只有一个,这样做不能保证答案的最优性

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    using namespace std;
    #define ll long long
    #define inf 0x3f3f3f3f
    #define N 255
    #define M (N * N << 1)
    
    int n, m, k, ans, tot, maxflow;
    int g[N][N], h[N * N], d[N << 1], cur[N << 1];
    int to[M], cap[M], nxt[M], head[N << 1];
    
    void add(int u, int v, int w) {
        to[++tot] = v; cap[tot] = w;
        nxt[tot] = head[u]; head[u] = tot;
        to[++tot] = u; cap[tot] = 0;
        nxt[tot] = head[v]; head[v] = tot;
    }
    
    bool bfs(int s, int t) {
        memset(d, 0, sizeof d);
        queue<int> q;
        d[s] = 1; q.push(s);
        while (!q.empty()) {
            int x = q.front(); q.pop();
            for (int i = head[x]; i; i = nxt[i])
                if (!d[to[i]] && cap[i]) {
                    d[to[i]] = d[x] + 1;
                    q.push(to[i]);
                }
        }
        return d[t];
    }
    
    int dfs(int x, int t, int res) {
        if (x == t || !res) return res;
        int flow = 0, f;
        for (int &i = cur[x]; i; i = nxt[i])
            if (d[to[i]] == d[x] + 1 && (f = dfs(to[i], t, min(cap[i], res)))) {
                flow += f; res -= f;
                cap[i] -= f; cap[i ^ 1] += f;
                if (!res) break;
            }
        return flow;
    }
    
    bool dinic(int now) {
        int s = 0, t = n + m + 1;
        tot = 1; maxflow = 0;
        memset(head, 0, sizeof head);
        for (int i = 1; i <= n; i++) add(s, i, 1);
        for (int i = 1; i <= m; i++) add(n + i, t, 1);
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                if (g[i][j] <= now) add(i, n + j, 1);
        while (bfs(s, t)) {
            memcpy(cur, head, sizeof cur);
            maxflow += dfs(s, t, inf);
        }
        if (maxflow > n - k) return true;
        return false;
    }
    
    int main() {
        scanf("%d%d%d", &n, &m, &k);
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++) {
                scanf("%d", &g[i][j]);
                h[++h[0]] = g[i][j];
            }
        sort(h + 1, h + n * m + 1);
        int l = 1, r = n * m;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (dinic(h[mid])) ans = h[mid], r = mid - 1;
            else l = mid + 1;
        }
        printf("%d
    ", ans);
        
        return 0;
    }
    View Code
  • 相关阅读:
    Matlab中save与load函数的使用
    bsxfun函数
    matlab中nargin函数的用法
    Leetcode 188. Best Time to Buy and Sell Stock IV
    Leetcode 123. Best Time to Buy and Sell Stock III
    leetcode 347. Top K Frequent Elements
    Leetcode 224. Basic Calculator
    Leetcode 241. Different Ways to Add Parentheses
    Leetcode 95. Unique Binary Search Trees II
    Leetcode 96. Unique Binary Search Trees
  • 原文地址:https://www.cnblogs.com/Pedesis/p/11366439.html
Copyright © 2020-2023  润新知