dfs,bfs的二分匹配模板(模板题hdu1150)
如果不懂匈牙利算法,请点击:该趣味算法http://blog.csdn.net/dark_scope/article/details/8880547
模板:
//DFS版本下的二分匹配算法
http://paste.ubuntu.net/16122581/
#include<cstdio>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<math.h>
#include<queue>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
//DFS版本下的二分匹配算法
const int MAXN = 300; //最大顶点数
bool bmap[MAXN][MAXN]; //二分图
bool bmask[MAXN]; //寻找增广路径的标志数组
int nx,ny; //nx为左顶点的个个数,ny为右顶点的个数
int cx[MAXN]; //cx[i]表示左集合i顶点所匹配到的右集合的顶点的序号。
int cy[MAXN]; //cy[i]表示右集合i顶点所匹配到的左集合的顶点的序号。
int k;
int findpath(int u)
{
int i;
for(i=0;i<ny;i++) //扫描每个妹子
{
//如果【有暧昧】并且【还没有标记过】
//这里标记的意思是这次查找【曾】试图改变过该妹子的归属问题
//但是没有成功,所以就不用瞎费工夫了
if(bmap[u][i]&&!bmask[i])
{
bmask[i]=1;
if(cy[i]==-1||findpath(cy[i])) //名花无主 或者 能腾出个位置来,这里使用递归
{
cy[i]=u;
cx[u]=i;
return 1;
}
}
}
return 0;
}
int MaxMatch()
{
int res=0; //有多少对
int i,j;
for(int i=0;i<nx;i++)
cx[i]=-1;
for(int i=0;i<ny;i++)
cy[i]=-1;
for(int i=0;i<nx;i++) //为男生找配偶
{
if(cx[i]==-1)
{
for(int j=0;j<ny;j++) //这个在每一步中清空
bmask[j]=0;
res+=findpath(i);
}
}
return res;
}
int main()
{
while(~scanf("%d",&nx)&&nx)
{
scanf("%d%d",&ny,&k);
int x;
memset(bmap,0,sizeof(bmap));
int a,b;
for(int i=0;i<k;i++)
{
scanf("%d%d%d",&x,&a,&b);
if(a>0&&b>0)
bmap[a][b]=1;
}
int ans=MaxMatch();
printf("%d\n",ans);
}
return 0;
}
/*
2 2 3
0 1 1
1 2 1
2 2 2
*/
//BFS版本下的二分匹配算法
http://paste.ubuntu.net/16122732/
#include<cstdio>
#include<string.h>
#include<iostream>
using namespace std;
typedef long long LL;
//BFS版本下的二分匹配算法
//我觉得DFS的很好理解,所以我觉得套用DFS的讲,可能有偏差;
const int MAXN = 1000; //最大顶点数
int bmap[MAXN][MAXN]; //二分图
int cx[MAXN]; //cx[i]表示左集合i顶点所匹配到的右集合的顶点的序号。
int cy[MAXN]; //cy[i]表示右集合i顶点所匹配到的左集合的顶点的序号。
int nx,ny,k; //nx为左顶点的个个数,ny为右顶点的个数
int bmask[MAXN]; //寻找增广路径的标志数组
int que[MAXN]; //队列保存扩展顶点
int pre[MAXN]; //记录前置顶点
int MaxMatch()
{
int res=0;
int qs,qe;
memset(cx,-1,sizeof(cx));
memset(cy,-1,sizeof(cy));
memset(bmask,-1,sizeof(bmask));
for(int i=0;i<nx;i++)
{
if(cx[i]==-1) //为男的寻找配偶
{
qs=qe=0; //队列初始化
que[qe++]=i;
pre[i]=-1;
bool flag=0;
while(qs<qe&&!flag)
{
int u=que[qs];
for(int v=0;v<ny&&!flag;v++)
{
if(bmap[u][v]&&bmask[v]!=i) //如果有关系,但是这个关系并不是给我们要配对的男生
{
bmask[v]=i; //但是...该男子强行拿过来作为妻子
que[qe++]=cy[v]; //所以女生本来的丈夫就很伤了
if(cy[v]>=0) //如果那个人有丈夫的话,就用pre记录前置节点
{
pre[cy[v]]=u;
}
else //但是该女子没有丈夫,那就是刚刚好
{
flag=1; //OK解决问题
int d=u,e=v;
while(d!=-1) //然后这就是类似于DFS算法中的回溯,这建立了改男子配对下的状态是什么样子的。
{
int t=cx[d];
cx[d]=e;cy[e]=d;
d=pre[d];e=t;
}
}
}
}
qs++;
}
if(cx[i]!=-1) //有多少配对的男子,就加几个
{
res++;
}
}
}
return res;
}
int main()
{
while(~scanf("%d",&nx)&&nx)
{
scanf("%d%d",&ny,&k);
int x;
memset(bmap,0,sizeof(bmap));
int a,b;
for(int i=0;i<k;i++)
{
scanf("%d%d%d",&x,&a,&b);
if(a>0&&b>0)
bmap[a][b]=1;
}
int ans=MaxMatch();
printf("%d\n",ans);
}
return 0;
}
dfs,bfs的二分匹配模板(模板题hdu1150)的更多相关文章
- hdu 5046 二分+DLX模板
http://acm.hdu.edu.cn/showproblem.php?pid=5046 n城市建k机场使得,是每个城市最近机场的距离的最大值最小化 二分+DLX 模板题 #include < ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- POJ-2251 Dungeon Master (BFS模板题)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量
POJ 1741. Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34141 Accepted: 11420 ...
- hdu1242 又又又是逃离迷宫(bfs模板题)
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...
- 用一道模板题理解多源广度优先搜索(bfs)
题目: //多元广度优先搜索(bfs)模板题详细注释题解(c++)class Solution { int cnt; //新鲜橘子个数 int dis[10][10]; //距离 int dir_x[ ...
- xdu_1048:二分匹配模板测试
二分匹配的模板题,这里用网络流模板(见刘汝佳<算法竞赛入门经典·训练指南>P359 Dinic算法)做. 将男女生均看做网络上的节点,题中给出的每个"关系"看做一条起点 ...
- HDU 4280:Island Transport(ISAP模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
随机推荐
- MySQL的字符编码体系(一)——数据存储编码
安装MySQL好多次了,每次都会纠结于数据库的字符编码配置,所以我决定这一次彻底把它理清. MySQL的字符编码结构比較细,它慷慨向分为两个部分:数据存储编码和传输数据编码.本篇讨论数据存储编码部分, ...
- 创建一个简单的 http 服务器
创建一个简单的 http 服务器 直接在 目录下运行 当前的目录即可是root 目录 默认端口8000 应该可以加参数修改端口号 Python2 python -m SimpleHTTPServer ...
- 安卓自带下拉刷新SwipeRefreshLayout加入上拉刷新功能
在项目里面要用到刷新库.曾经都是使用第三方的.只是看到官方出了 SwipeRefreshLayout之后就用SwipeRefreshLayout.可是不知道什么原因官方SwipeRefreshL ...
- find the longest of the shortest (hdu 1595 SPFA+枚举)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 用C++、Qt实现的小游戏2048
窗口布局与游戏截图: 实现思路: 1.使用二维数组模拟整个游戏网格,并将二维数组中每个数的大小用作游戏中每个网格中的数据. 2.将对游戏的数据及数据的操作(即玩家的操作对游戏数据的影响)作为一个类,游 ...
- arm-linux交叉编译环境搭建
1.解压交叉编译工具链包 tar jxvf EABI-4.3.3_EmbedSky_20100610.tar.bz2 (笔者将交叉编译工具链包放在”/home ”目录下) 解压后会生成两个目录: [r ...
- GDI泄露+改EXE名
CDC 应该是成对使用 GetDC and ReleaseDC(不用new and delete) 泄露 1.改变生产exe名称:工程->设置->连接->输出文件名:Release/ ...
- hibernate 的分页查询
hibernate的分页查询有个好处,就是不用管数据库方言.比如db2的分页查询很麻烦,但是用hibernate的方式,就完全不用管这些了 /* 使用HQL分页查询Customer信息 */ publ ...
- Pattern: Microservice Architecture
Microservice Architecture pattern http://microservices.io/patterns/microservices.html Context You ar ...
- SpringMVC 之 Controller、Service层职责
Controller层 1.接收httpRequest/requestDTO数据 ,检查接收数据参数与格式. 2.传递参数至Service层并接收返回responseDTO数据. 3.包装respon ...