fafu 1568 Matrix(二分匹配+二分)

Description:

You are given a matrix which is n rows m columns (1 <= n <= m <= 100). You are supposed to choose n elements, there is no more than 1 element in the same row and no more than 1 element in the same column. What is the minimum value of the K_th largest in the n elements you have chosen.

Input:

First line is an integer T (T ≤ 15), the number of test cases. At the beginning of each case is three integers n, m, K, 1 <= K <= n <= m <= 100.
      Then n lines are given. Each line contains m integers, indicating the matrix.
      All the elements of the matrix is in the range [1, 10^9]. 

Output:

For each case, output Case #k: one line first, where k means the case number count from 1. Then output the answer.

Sample Input:

2
2 3 1
1 2 4
2 4 1

3 4 2
1 5 6 6
8 3 4 3
6 8 6 3

Sample Output:

Case #1: 1
Case #2: 3

二分枚举答案,如果a[i][j]<=mid则建一条xi->yj的边,然后对二分图跑最大匹配,如果最大匹配数>=n-k+1,则ans<=mid,否则ans>mid

  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<cmath>
  6 #include<math.h>
  7 #include<algorithm>
  8 #include<queue>
  9 #include<set>
 10 #include<bitset>
 11 #include<map>
 12 #include<vector>
 13 #include<stdlib.h>
 14 #include <stack>
 15 using namespace std;
 16 #define PI acos(-1.0)
 17 #define max(a,b) (a) > (b) ? (a) : (b)
 18 #define min(a,b) (a) < (b) ? (a) : (b)
 19 #define ll long long
 20 #define eps 1e-10
 21 #define MOD 1000000007
 22 #define N 106
 23 #define inf 1e10
 24 int n,m,k;
 25 int mp[N][N];
 26 int cnt[N][N];
 27 int vis[N];
 28 int link[N];
 29 vector<int> v;
 30
 31 void build_map(int mid){
 32    memset(cnt,0,sizeof(cnt));
 33     for(int i=0;i<n;i++){
 34         for(int j=0;j<m;j++){
 35             if(mp[i][j]<=mid){
 36                cnt[i][j]=1;
 37             }
 38         }
 39     }
 40 }
 41
 42 bool dfs(int u){
 43    for(int i=0;i<m;i++){
 44        if(!vis[i] && cnt[u][i]){
 45            vis[i]=1;
 46            if(link[i]==-1 || dfs(link[i])){
 47               link[i]=u;
 48               return true;
 49            }
 50        }
 51    }
 52    return false;
 53 }
 54 bool solve(int mid){
 55    build_map(mid);
 56       memset(link,-1,sizeof(link));
 57       int ans=0;
 58       for(int i=0;i<n;i++){
 59           memset(vis,0,sizeof(vis));
 60           if(dfs(i)){
 61              ans++;
 62           }
 63       }
 64
 65       if(ans>=(n-k+1)) return true;
 66       return false;
 67
 68
 69
 70 }
 71 int main()
 72 {
 73    int ac=0;
 74    int t;
 75    scanf("%d",&t);
 76    while(t--){
 77        memset(mp,0,sizeof(mp));
 78        scanf("%d%d%d",&n,&m,&k);
 79        for(int i=0;i<n;i++){
 80            for(int j=0;j<m;j++){
 81                scanf("%d",&mp[i][j]);
 82            }
 83        }
 84        int low=0;
 85        int high=inf;
 86        while(low<high){
 87            int mid=(low+high)>>1;
 88            if(solve(mid)){
 89                high=mid;
 90                //ans=mid;
 91            }else{
 92                low=mid+1;
 93            }
 94        }
 95        printf("Case #%d: ",++ac);
 96        printf("%d\n",low);
 97
 98
 99
100    }
101     return 0;
102 }

时间: 2024-10-23 06:37:18

fafu 1568 Matrix(二分匹配+二分)的相关文章

ZOJ 3156 Taxi (二分匹配+二分查找)

题目链接:Taxi Taxi Time Limit: 1 Second      Memory Limit: 32768 KB As we all know, it often rains suddenly in Hangzhou during summer time.I suffered a heavy rain when I was walking on the street yesterday, so I decided to take a taxi back school. I foun

二分匹配 二分+网络流 未完成

奶牛分配(stall4.pas/in.out) 描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶.上个星期,农夫约翰刚刚收集到了奶牛们的爱好的信息(每头奶牛喜欢在哪些牛栏产奶).一个牛栏只能容纳一头奶牛,当然,一头奶牛只能在一个牛栏中产奶.  给出奶牛们的爱好的信息,计算最大分配方案.   输入格式 第一行 两个整数,N (0

HDU 2119 Matrix 简单二分匹配

行做x集,列做y集,1就给该行该列连一条边,输出最大匹配边即可 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<set> using namespace std; #define N 105 int lef[N], pn;//lef[v]表示Y集的点v 当前连接的点 , pn为x点集的

poj 2226 Muddy Fields(合理建图+二分匹配)

1 /* 2 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 3 思路: 4 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1,然后让我们通过选择一行,或者 5 是一列将其所在行的或者所在列的 1全部删掉,求出最少需要几步? 6 7 这道题的思路就是:将行标 和 列标值为1的建立一条边!通过匈牙利算法可以得到这个二分图的最大匹配数 8 最大匹配数==最小顶点覆盖数!最小顶点覆盖就是用最少的点覆盖了这个二分图

hdu 1498 50 years, 50 colors 二分匹配

50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1789    Accepted Submission(s): 978 Problem Description On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating

hdu1498--50 years, 50 colors(二分匹配,题意。。。)

50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1617    Accepted Submission(s): 881 Problem Description On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating

Optimal Milking(二分图多重匹配+二分)(网络流)

Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2112 Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 20

hdu2119Matrix (二分匹配,最小顶点覆盖)

Matrix Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1819 Accepted Submission(s): 792 Problem Description Give you a matrix(only contains 0 or 1),every time you can select a row or a column and

HDU 3861 The King’s Problem (强连通+二分匹配)

题目地址:HDU 3861 这题虽然是两个算法结合起来的.但是感觉挺没意思的..结合的一点也不自然,,硬生生的揉在了一块...(出题者不要喷我QAQ.) 不过这题让我发现了我的二分匹配已经好长时间没用过了..都快忘了..正好在省赛之前又复习了一下. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm>