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

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;
}
相关推荐
棒棒AIT13 小时前
mac 苹果电脑 Intel 芯片(Mac X86) 安卓虚拟机 Android模拟器 的救命稻草(下载安装指南)
android·游戏·macos·安卓·mac
Deng94520131417 小时前
24点数学游戏(穷举法求解表达式)
游戏·穷举法·递归回溯算法
用户61204149221319 小时前
C语言做的井字棋小游戏
c语言·后端·游戏
★YUI★1 天前
学习游戏制作记录(剑投掷技能)7.26
学习·游戏·unity·c#
我要学习别拦我~2 天前
kaggle分析项目:steam付费游戏数据分析
python·游戏·数据分析
Sui_Network2 天前
探索 Sui 上 BTCfi 的各类资产
大数据·人工智能·科技·游戏·区块链
★YUI★2 天前
学习游戏制作记录(克隆技能)7.25
学习·游戏·unity·c#
王火火(DDoS CC防护)2 天前
什么是游戏盾(高防版)?
游戏·游戏盾
不绝1912 天前
ARPG开发流程第一章——方法合集
算法·游戏·unity·游戏引擎
UWA3 天前
UWA DAY 2025 游戏开发者大会|全议程
游戏·unity·性能优化·游戏开发·uwa·unreal engine