• BZOJ 4443 [Scoi2015]小凸玩矩阵(二分答案+二分图匹配)


    【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=4443

    【题目大意】

      从矩阵中选出N个数,其中任意两个数字不能在同一行或同一列
      求选出来的N个数中第K大的数字的最小值是多少。

    【题解】

      我们二分这个第k大数字的大小,将其以上的数字全部删除,
      在剩余的部分按行列连边,如果二分图匹配的数量大于n-k那么说明该答案可行。

    【代码】

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <vector> 
    using namespace std;
    const int MAX_V=1000;
    const int INF=0x3f3f3f3f;
    int V,match[MAX_V];
    vector<int> G[MAX_V];
    bool used[MAX_V];
    void add_edge(int u,int v){
        G[u].push_back(v);
        G[v].push_back(u);
    }
    bool dfs(int v){
        used[v]=1;
        for(int i=0;i<G[v].size();i++){
            int u=G[v][i],w=match[u];
            if(w<0||!used[w]&&dfs(w)){
                match[v]=u;
                match[u]=v;
                return 1;
            }
        }return 0;
    }
    int bipartite_matching(){
        int res=0;
        memset(match,-1,sizeof(match));
        for(int v=0;v<V;v++){
            if(match[v]<0){
                memset(used,0,sizeof(used));
                if(dfs(v))res++;
            }
        }return res;
    }
    const int N=300;
    int n,m,k,a[N][N];
    bool check(int x){
        V=n+m+1;
        for(int i=0;i<V;i++)G[i].clear();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++)if(a[i][j]<=x){
                add_edge(i,n+j);
            }
        }return bipartite_matching()>=n-k+1;
    }
    int main(){
        while(~scanf("%d%d%d",&n,&m,&k)){
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++)scanf("%d",&a[i][j]);
            }
            int l=1,r=1000000000,ans=r;
            while(l<=r){
                int mid=(l+r)>>1;
                if(check(mid))r=mid-1,ans=mid;
                else l=mid+1;
            }printf("%d
    ",ans);
        }return 0;
    }
  • 相关阅读:
    nyoj256-C小加之级数求和
    nyoj254-编号统计
    nyoj286-动物统计
    最长回文子串——manacher
    动态规划:Codeforces Round #427 (Div. 2) C Star sky
    水题:51Nod1432-独木舟
    水题:HDU1716-排列2
    水题:CF16C-Monitor
    数学基础:HUD1124-Factorial(N!末尾0的个数)
    并查集:POJ1182-食物链(并查集比较高端的应用)
  • 原文地址:https://www.cnblogs.com/forever97/p/bzoj4443.html
Copyright © 2020-2023  润新知