二分图多重匹配

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

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

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. using namespace std;
  7. int init(){
  8. int rv=0,fh=1;
  9. char c=getchar();
  10. while(c<'0'||c>'9'){
  11. if(c=='-') fh=-1;
  12. c=getchar();
  13. }
  14. while(c>='0'&&c<='9'){
  15. rv=(rv<<1)+(rv<<3)+c-'0';
  16. c=getchar();
  17. }
  18. return fh*rv;
  19. }
  20. const int MAXN=100005;
  21. int n,m,cnt[12],match[12][MAXN],g[MAXN][12],lim[12];
  22. bool f[12];
  23. bool hungarian(int u){
  24. for(int i=1;i<=g[u][0];i++){
  25. int v=g[u][i];
  26. if(!f[v]){
  27. f[v]=1;
  28. if(cnt[v]<lim[v]){
  29. match[v][++cnt[v]]=u;
  30. return 1;
  31. }
  32. for(int j=1;j<=cnt[v];j++){
  33. if(hungarian(match[v][j])){
  34. match[v][j]=u;
  35. return 1;
  36. }
  37. }
  38. }
  39. }
  40. return 0;
  41. }
  42. int main(){
  43. while(~scanf("%d%d",&n,&m)){
  44. memset(g,0,sizeof(g));
  45. //memset(match,0,sizeof(match));
  46. memset(cnt,0,sizeof(cnt));
  47. memset(lim,0,sizeof(lim));
  48. for(int i=1;i<=n;i++){
  49. for(int j=1;j<=m;j++){
  50. int t=init();
  51. if(t) g[i][++g[i][0]]=j;
  52. }
  53. }
  54. for(int i=1;i<=m;i++) lim[i]=init();
  55. int ans=0;
  56. for(int i=1;i<=n;i++){
  57. memset(f,0,sizeof(f));
  58. if(!hungarian(i)) {ans=1;break;}//一旦搜不到增广路就退出
  59. }
  60. if(!ans) cout<<"YES"<<endl;
  61. else cout<<"NO"<<endl;
  62. }
  63. }

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. 线段树入门HDU_1754

    题目链接:点击打开链接 I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. HDU2008

    数值统计 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  3. JS验证两次输入密码是否相同

    js中 <script>function check(){ with(document.all){if(input1.value!=input2.value){alert("fa ...

  4. [国嵌攻略][153][I2C裸机驱动设计]

    eeprom简介 eeprom电可擦除可编程只读存储器,是一种类似于flash的固态存储器,但是与flash相比又存在一些区别: 1.eeprom可以按位擦写,而flash只能大片擦除. 2.eepr ...

  5. SpringBoot Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

    SpringBoot Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFact ...

  6. nginx重启几种方法

    http://blog.csdn.net/zqinghai/article/details/71125045 ps -ef|grep nginx 平滑重启命令: kill -HUP 住进称号或进程号文 ...

  7. 4种常用扒站工具(webzip、ha_TeleportPro、Offline Explorer、wget)

    许多开始做lead,EMU的新手,需要一个英文网站,找人定制费用太贵自己又不会技术的话,可以通过扒站来获得英文站,今天给大家介绍几种扒站工具: 1.HA_TeleportPro: Teleport P ...

  8. Linux 中出现的-bash: syntax error near unexpected token `

    版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux 5中导入数据时,出现下面的错误. -bash: syntax error near unexpected token `(' 检查了 ...

  9. 使用Flink的SavePoint功能

    Flink通过SavePoint功能可以做到程序升级后,继续从升级前的那个点开始执行计算,保证数据不中断. Flink中CheckPoint用于保存状态,是自动执行的,SavePoint是指向Chec ...

  10. python中强大的format函数

    自python2.6开始,新增了一种格式化字符串的函数str.format(),此函数可以快速处理各种字符串.语法 它通过{}和:来代替%. 请看下面的示例,基本上总结了format函数在python ...