「SCOI2015」小凸玩矩阵

题目链接

问题分析

题目给了充足的暗示,我们只需要二分答案然后跑匈牙利即可。要相信匈牙利的速度

参考程序

#include <bits/stdc++.h>
using namespace std;

const int Maxn = 310;
const int INF = 2147483647;
int N, M, K, A[ Maxn ][ Maxn ], Max, Min;
int Map[ Maxn ][ Maxn ], Vis[ Maxn ], Mx[ Maxn ], My[ Maxn ];

int Dfs( int x ) {
	for( int i = 1; i <= M; ++i ) {
		if( Map[ x ][ i ] == 0 ) continue;
		if( Vis[ i ] ) continue;
		Vis[ i ] = 1;
		if( My[ i ] == 0 || Dfs( My[ i ] ) ) {
			Mx[ x ] = i; My[ i ] = x;
			return 1;
		}
	}
	return 0;
}

bool Check( int Mid ) {
	memset( Map, 0, sizeof( Map ) );
	memset( Mx, 0, sizeof( Mx ) );
	memset( My, 0, sizeof( My ) );
	for( int i = 1; i <= N; ++i ) 
		for( int j = 1; j <= M; ++j ) 
			if( A[ i ][ j ] <= Mid ) Map[ i ][ j ] = 1;
	int Ans = 0;
	for( int i = 1; i <= N; ++i ) {
		memset( Vis, 0, sizeof( Vis ) );
		Ans += Dfs( i );
	}
	return Ans >= ( N - K + 1 );
}

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", &A[ i ][ j ] );
	Max = 0, Min = INF;
	for( int i = 1; i <= N; ++i ) 
		for( int j = 1; j <= M; ++j ) 
			Max = max( A[ i ][ j ], Max ), Min = min( A[ i ][ j ], Min );
	int Ans = 0;
	while( Max >= Min ) {
		int Mid = ( Max + Min ) >> 1;
		if( Check( Mid ) ) Ans = Mid, Max = Mid - 1; else Min = Mid + 1;
	}
	printf( "%d\n", Ans );
	return 0;
}

posted @ 2019-07-25 09:16  chy_2003  阅读(118)  评论(0编辑  收藏  举报