BZOJ1433 [ZJOI2009]假期的宿舍 二分图匹配 匈牙利算法

原文链接http://www.cnblogs.com/zhouzhendong/p/8372785.html


题目传送门 - BZOJ1433


题解

  我们理一理题目。

  在校的学生,有自己的床,还可以睡朋友的床。

  离校的学生,不占床。

  外来的学生,只能睡朋友的床。

  然后就是一个裸的二分图匹配了。


代码

#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=55;
int T,n,g[N][N],a[N],b[N];
int match[N],vis[N];
int Match(int x){
	for (int i=1;i<=n;i++)
		if (!vis[i]&&g[x][i]){
			vis[i]=1;
			if (!match[i]||Match(match[i])){
				match[i]=x;
				return 1;
			}
		}
	return 0;
}
int hungray(){
	memset(match,0,sizeof match);
	for (int i=1;i<=n;i++)
		if (!b[i]){
			memset(vis,0,sizeof vis);
			if (!Match(i))
				return 0;
		}
	return 1;
}
int main(){
	scanf("%d",&T);
	while (T--){
		scanf("%d",&n);
		memset(g,0,sizeof g);
		for (int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		for (int i=1;i<=n;i++)
			scanf("%d",&b[i]);
		for (int i=1;i<=n;i++)
			for (int j=1;j<=n;j++){
				scanf("%d",&g[i][j]);
				if (!a[j])
					g[i][j]=0;
			}
		for (int i=1;i<=n;i++){
			if (a[i])
				g[i][i]=1;
			else
				b[i]=0;
		}
		puts(hungray()?"^_^":"T_T");
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/zhouzhendong/p/8372785.html

时间: 2024-07-30 11:50:24

BZOJ1433 [ZJOI2009]假期的宿舍 二分图匹配 匈牙利算法的相关文章

[BZOJ1433][ZJOI2009]假期的宿舍 二分图匹配

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1433 首先留在学校的学生向自己的床连边. 要住在学校里的人向认识的学生的床连边. 跑二分图匹配,看匹配的数量是否等于住在学校的人数. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int inline readint(){ 6 int N

USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John r

HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49    Accepted Submission(s): 14 Problem Description There is a kindom of obsession, so people in this kingdom do things very

HDU1507 Uncle Tom&#39;s Inherited Land* 二分图匹配 匈牙利算法 黑白染色

原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1*2的矩形覆盖所有的不废的点,并且不重叠,问最多可以覆盖多少个1*2的矩形,输出方案,有SPJ. 输入描述: 多组数据,每组首先两个数n,m(如果n和m为0,则结束程序) 然后给出k 然后给出k个二元组(x,y)表示废点的坐标. 题解 按照前两片博文的算法已经不行了,因为方案不对了. 所以我们要进行

bzoj1433[ZJOI2009]假期的宿舍(匈牙利)

1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2544  Solved: 1074 [Submit][Status][Discuss] Description Input Output Sample Input 1 3 1 1 0 0 1 0 0 1 1 1 0 0 1 0 0 Sample Output ˆ ˆ HINT 对于30% 的数据满足1 ≤ n ≤ 12.对于100% 的数据满足1 ≤ n

bzoj1433: [ZJOI2009]假期的宿舍

1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2286  Solved: 969[Submit][Status][Discuss] Description Input Output Sample Input 131 1 00 1 00 1 11 0 01 0 0 Sample Output ˆ ˆ HINT 对于30% 的数据满足1 ≤ n ≤ 12.对于100% 的数据满足1 ≤ n ≤ 50,1 ≤

bzoj1433 [ZJOI2009]假期的宿舍(最大流)

1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1717  Solved: 754[Submit][Status][Discuss] Description Input Output Sample Input 1 3 1 1 0 0 1 0 0 1 1 1 0 0 1 0 0 Sample Output ˆ ˆ HINT 对于30% 的数据满足1 ≤ n ≤ 12. 对于100% 的数据满足1 ≤ n ≤

bzoj1433 [ZJOI2009]假期的宿舍 最大流

[ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3429  Solved: 1459[Submit][Status][Discuss] Description Input Output Sample Input 1 3 1 1 0 0 1 0 0 1 1 1 0 0 1 0 0 Sample Output ˆ ˆ HINT 对于30% 的数据满足1 ≤ n ≤ 12. 对于100% 的数据满足1 ≤ n ≤ 50,1

P2756 飞行员配对方案问题 二分图匹配 匈牙利算法

题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中,每一名外籍飞行员都可以与其他若干名英国飞行员很好地配合.如何选择配对飞行的飞行员才能使一次派出最多的飞机.对于给定的外籍飞行员与英国飞行员的配合情况,试设计一个算法找出最佳飞行员配对方案,使皇家空军一次能派出最多的飞机. 对于给定的外籍飞行员与英国飞行员的配合情况,编程找