矩阵游戏--二分图的匈牙利算法

https://www.luogu.com.cn/problem/P1129

学习路线---https://blog.csdn.net/qq_39304630/article/details/108135381

1.二分图就是两个独立的两个集合,如这里是行和列

2.匈牙利匹配就是媒婆拉媒,没伴侣或者伴侣可以换就将当前的塞给她

3.最后true的个数就算最小点覆盖数或者最大匹配数

复制代码
#include<bits/stdc++.h>
#include<string>
using namespace std;
#define N 100011
typedef long long ll;
typedef pair<int,int> pii;
int t,n; 
int mp[201][201];
int mate[201];
bool bo[201];
bool metch(int x)///二分图--匈牙利算法 
{
	for(int j=1;j<=n;j++)
	{
		if(!bo[j]&&mp[x][j])///黑且没标记 
		{
			bo[j]=true;
			if(mate[j]==0||metch(mate[j]))///没伴侣或者j的伴侣可以换另一个 
			{
				mate[j]=x;///那j的伴侣现在就是x了 
				return true;///匹配成功 
			}
		}
	}
	return false;///一圈下来没有,失败qwq 
}
int main()
{
cin>>t;
while(t--)
{
	cin>>n;
	int s=0;
	memset(mate,0,sizeof(mate));///一定要清0!!!! 
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cin>>mp[i][j];
			if(mp[i][j]) s++;
		}
	}
	if(s<n) cout<<"No";
	else
	{
		s=0;
		for(int i=1;i<=n;i++)
		{
			memset(bo,false,sizeof(bo));
			if(metch(i)) 
			{
				s++;
			}
		}
		if(s==n) cout<<"Yes";///行列不重复的要够n个
		else cout<<"No"; ///不够就百搭 
	}
	cout<<endl;
}
return 0;
}
相关推荐
benben0449 小时前
Unity3D仿星露谷物语开发44之收集农作物
前端·游戏·unity·游戏引擎
Unity官方开发者社区2 天前
《Cryptical Path》开发诀窍:像玩游戏一样开发一款类Rogue游戏
java·游戏·玩游戏
二狗哈2 天前
制作一款打飞机游戏49:敌人抖动
游戏
Time Famine2 天前
射击游戏demo11
python·游戏·pygame
染指11103 天前
25.第二阶段x64游戏实战-分析物品相关数据
汇编·游戏·游戏逆向·x64dbg·x64游戏
web150854159353 天前
使用Python开发经典俄罗斯方块游戏
python·游戏·pygame
白露秋483 天前
C——五子棋小游戏
c语言·游戏
wgc2k4 天前
Java游戏服务器开发流水账(5)Spring 在游戏开发中的使用简介
java·服务器·游戏
passionSnail4 天前
《用MATLAB玩转游戏开发》推箱子游戏的MATLAB趣味实现
开发语言·游戏·matlab
海拥✘5 天前
CodeBuddy终极测评:中国版Cursor的开发革命(含安装指南+HTML游戏实战)
前端·游戏·html