博客
关于我
[SCOI2015]小凸玩矩阵 (匈牙利+二分)
阅读量:294 次
发布时间:2019-03-03

本文共 1293 字,大约阅读时间需要 4 分钟。

description

题目描述

小凸和小方是好朋友,小方给小凸一个 N×M(N≤M)的矩阵 A,要求小凸从其中选出 N 个数,其中任意两个数字不能在同一行或同一列,现小凸想知道选出来的 N 个数中第 K大的数字的最小值是多少。

输入格式

第一行给出三个整数 N、M、K。接下来 N 行,每行 M个数字,用来描述这个矩阵。
输出格式
输出选出来的 N个数中第 K大的数字的最小值。

样例

Input
3 4 2
1 5 6 6
8 3 4 3
6 8 6 3
Output
3

数据范围与提示

1≤K≤N≤M≤250,1≤Ai,j≤10^9

solution

K K K大最小值,明显套路往二分上面思考

不妨二分最后的答案
那么问题转化为选择边的边权不超过答案的情况下的最大匹配数 ≥ n − k + 1 \ge n -k+1 nk+1

code

#include 
#include
#include
using namespace std;#define maxn 255int n, m, k, l, r;bool vis[maxn];int match[maxn];int matrix[maxn][maxn];bool find( int x, int lim ) { for( int i = 1;i <= m;i ++ ) { if( ! vis[i] && matrix[x][i] <= lim ) { vis[i] = 1; if( ! match[i] || find( match[i], lim ) ) { match[i] = x; return 1; } } } return 0;}int check( int x ) { memset( match, 0, sizeof( match ) ); int ans = 0; for( int i = 1;i <= n;i ++ ) { memset( vis, 0, sizeof( vis ) ); if( find( i, x ) ) ans ++; } return ans;}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", &matrix[i][j] ); r = max( r, matrix[i][j] ); } int ans; while( l <= r ) { int mid = ( l + r ) >> 1; if( check( mid ) >= n - k + 1 ) ans = mid, r = mid - 1; else l = mid + 1; } printf( "%d\n", ans ); return 0;}

转载地址:http://eail.baihongyu.com/

你可能感兴趣的文章
Mysql启动失败解决过程
查看>>
MySQL启动失败:Can't start server: Bind on TCP/IP port
查看>>
mysql启动报错
查看>>
mysql启动报错The server quit without updating PID file几种解决办法
查看>>
MySQL命令行登陆,远程登陆MySQL
查看>>
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MYSQL和ORACLE的一些操作区别
查看>>
mysql和redis之间互相备份
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>
Mysql基本操作
查看>>