二分图多重匹配

改进版的匈牙利,加入了一个cnt数组作为找到增广路的标志

本题有一个重要的优化见注释

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
int init(){
int rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return fh*rv;
}
const int MAXN=100005;
int n,m,cnt[12],match[12][MAXN],g[MAXN][12],lim[12];
bool f[12];
bool hungarian(int u){
for(int i=1;i<=g[u][0];i++){
int v=g[u][i];
if(!f[v]){
f[v]=1;
if(cnt[v]<lim[v]){
match[v][++cnt[v]]=u;
return 1;
}
for(int j=1;j<=cnt[v];j++){
if(hungarian(match[v][j])){
match[v][j]=u;
return 1;
}
}
}
}
return 0;
}
int main(){
while(~scanf("%d%d",&n,&m)){
memset(g,0,sizeof(g));
//memset(match,0,sizeof(match));
memset(cnt,0,sizeof(cnt));
memset(lim,0,sizeof(lim));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int t=init();
if(t) g[i][++g[i][0]]=j;
}
}
for(int i=1;i<=m;i++) lim[i]=init();
int ans=0;
for(int i=1;i<=n;i++){
memset(f,0,sizeof(f));
if(!hungarian(i)) {ans=1;break;}//一旦搜不到增广路就退出
}
if(!ans) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}

HDU [P3605] Escape的更多相关文章

  1. HDU 3533 Escape(大逃亡)

    HDU 3533 Escape(大逃亡) /K (Java/Others)   Problem Description - 题目描述 The students of the HEU are maneu ...

  2. HDU 3605 Escape (网络流,最大流,位运算压缩)

    HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...

  3. Hdu 3605 Escape (最大流 + 缩点)

    题目链接: Hdu 3605  Escape 题目描述: 有n个人要迁移到m个星球,每个星球有最大容量,每个人有喜欢的星球,问是否所有的人都能迁移成功? 解题思路: 正常情况下建图,不会爆内存,但是T ...

  4. HDU 3605 Escape(状压+最大流)

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

  5. HDU 3605 Escape 最大流+状压

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 2000/1000 MS (Java/Others)    ...

  6. hdu 3605 Escape 二分图的多重匹配(匈牙利算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    ...

  7. HDU 3533 Escape bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=3533 一道普通的bfs,但是由于代码实现出了bug还是拖了很久甚至对拍了 需要注意的是: 1.人不能经过炮台 2 ...

  8. HDU 3533 Escape (BFS + 预处理)

    Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  9. HDU 3533 Escape(bfs)

    Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. DP入门

    数塔HDU2084 #include <iostream> #include <algorithm> #include <cstdio> #include < ...

  2. 为什么vertical-align不起作用

    先看一段代码 <style> .title { margin:50px; color:blue; } .title span { font-size:24px; } .title span ...

  3. POJ 1422 Air Raid

    题目链接: http://poj.org/problem?id=1422 Description Consider a town where all the streets are one-way a ...

  4. Python 使用 virtualenvwrapper 安装虚拟环境

    装载于https://www.jianshu.com/p/9f47a9801329 Python 使用 virtualenvwrapper 安装虚拟环境 Tim_Lee 关注 2017.05.04 2 ...

  5. NGINX 配置404错误页面转向

    什么是404页面 如果碰巧网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是 ...

  6. 宝塔linux面板.txt

    安装命令: yum -y install screen wget && screen -S bt wget -O install.sh http://103.224.251.79:58 ...

  7. Oracle问题之ORA-01609、ORA-00362

    Oracle问题之ORA-01609: 日志 4 是线程 1 的当前日志 - 无法删除成员 Oracle问题之ORA-00362: 要求输入成员以组成组 4 中的有效日志文件

  8. Oracle临时表空间组

    Oracle 10g之前,同一用户的多个会话只可以使用同一个临时表空间,因为在给定的时间只有一个临时表空间默认给用户,为了解决这个潜在的瓶颈,Oracle支持临时表空间组即包含多个临时表空间的集合.临 ...

  9. python基础7之python3的内置函数

    官方介绍: python3:https://docs.python.org/3/library/functions.html?highlight=built#ascii python2:https:/ ...

  10. vim编辑操作

    vim    插入模式        a    光标后        A    行尾        o    光标所在行下一行        O    光标所在行上一行        i    光标前 ...