2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.

题意:有N个人,每个人能适应若干个星球(共10个星球),每个星球能够容纳若干人,问是否能让所有人都有星球住。

其实应该是一个二分图多重匹配的题目。

不过一开始当成最大流的题目了,做的时候TLE了,然后将对10个星球的适应性的情况用状压变成了2^10种情况,即2^10种人,并且加上读入优化在vj上也卡过去了,不过在杭电上始终还是TLE了。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. const int maxn=;
  8. const int maxm=;
  9. int match[maxn][maxm],g[maxn][maxm],cnt[maxm],lim[maxn],n,m;
  10. bool vis[maxm];
  11.  
  12. bool dfs(int s){
  13. for(int i=;i<m;++i){
  14. if(g[s][i]==||vis[i]==)continue;
  15. vis[i]=;
  16. if(cnt[i]<lim[i]){
  17. match[i][cnt[i]++]=s;
  18. return ;
  19. }
  20. else{
  21. for(int j=;j<lim[i];++j){
  22. if(dfs(match[i][j])==){
  23. match[i][j]=s;
  24. return ;
  25. }
  26. }
  27. }
  28. }
  29. return ;
  30. }
  31. bool hungary(int n){
  32. for(int i=;i<n;++i){
  33. memset(vis,,sizeof(vis));
  34. if(dfs(i)==)return ;
  35. }
  36. return ;
  37. }
  38. int main(){
  39. while(scanf("%d%d",&n,&m)!=EOF){
  40. memset(cnt,,sizeof(cnt));
  41. for(int i=;i<n;++i)
  42. for(int j=;j<m;++j)scanf("%d",&g[i][j]);
  43. for(int i=;i<m;++i)scanf("%d",&lim[i]);
  44. if(hungary(n))printf("YES\n");
  45. else printf("NO\n");
  46. }
  47. return ;
  48. }

多重匹配

  1. #pragma comment(linker,"/STACK:16777216")
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<vector>
  5. #include<queue>
  6. #include<algorithm>
  7. using namespace std;
  8. const int maxm=;
  9. const int INF=0x3f3f3f3f;
  10.  
  11. struct edge{
  12. int from,to,c,f;
  13. edge(int a,int b,int m,int n):from(a),to(b),c(m),f(n){}
  14. };
  15.  
  16. struct dinic{
  17. int s,t,m;
  18. vector<edge>e;
  19. vector<int>g[maxm];
  20. bool vis[maxm];
  21. int cur[maxm],d[maxm];
  22.  
  23. void init(int n){
  24. for(int i=;i<=n;i++)g[i].clear();
  25. e.clear();
  26. }
  27.  
  28. void add(int a,int b,int c){
  29. e.push_back(edge(a,b,c,));
  30. e.push_back(edge(b,a,,));
  31. m=e.size();
  32. g[a].push_back(m-);
  33. g[b].push_back(m-);
  34. }
  35.  
  36. bool bfs(){
  37. memset(vis,,sizeof(vis));
  38. queue<int>q;
  39. q.push(s);
  40. vis[s]=;
  41. d[s]=;
  42. while(!q.empty()){
  43. int u=q.front();
  44. q.pop();
  45. for(int i=;i<g[u].size();i++){
  46. edge tmp=e[g[u][i]];
  47. if(!vis[tmp.to]&&tmp.c>tmp.f){
  48. d[tmp.to]=d[u]+;
  49. vis[tmp.to]=;
  50. q.push(tmp.to);
  51. }
  52. }
  53. }
  54. return vis[t];
  55. }
  56.  
  57. int dfs(int x,int a){
  58. if(x==t||a==)return a;
  59. int flow=,f;
  60. for(int& i=cur[x];i<g[x].size();i++){
  61. edge& tmp=e[g[x][i]];
  62. if(d[tmp.to]==d[x]+&&(f=dfs(tmp.to,min(a,tmp.c-tmp.f)))>){
  63. tmp.f+=f;
  64. e[g[x][i]^].f-=f;
  65. flow+=f;
  66. a-=f;
  67. if(a==)break;
  68. }
  69. }
  70. if(!flow)d[x]=-;
  71. return flow;
  72. }
  73.  
  74. int mf(int s,int t){
  75. this->s=s;
  76. this->t=t;
  77. int flow=;
  78. while(bfs()){
  79. memset(cur,,sizeof(cur));
  80. flow+=dfs(s,INF);
  81. }
  82. return flow;
  83. }
  84. };
  85.  
  86. int num[];
  87.  
  88. int main(){
  89. int n,m;
  90. while(scanf("%d%d",&n,&m)!=EOF){
  91. memset(num,,sizeof(num));
  92. dinic d;
  93. d.init(m+(<<m)+);
  94. int i,j;
  95. for(i=;i<=n;i++){
  96. int tmp=;
  97. for(j=;j<=m;j++){
  98. int a=;
  99. char c=getchar();
  100. while(c>''||c<'')c=getchar();
  101. a=c-'';
  102. tmp=tmp*+a;
  103. }
  104. num[tmp]++;
  105. }
  106. for(i=;i<=(<<m)-;i++){
  107. if(num[i]){
  108. d.add(,i,num[i]);
  109. int tmp=i;
  110. for(j=m;j>=;j--){
  111. if(tmp&){
  112. d.add(i,(<<m)+j,INF);
  113. }
  114. tmp>>=;
  115. }
  116. }
  117. }
  118. for(j=;j<=m;j++){
  119. int a=;
  120. char c=getchar();
  121. while(c>''||c<'')c=getchar();
  122. while(c>=''&&c<=''){
  123. a=a*+c-'';
  124. c=getchar();
  125. }
  126. d.add((<<m)+j,m+(<<m)+,a);
  127. }
  128. if(n==d.mf(,m+(<<m)+))printf("YES\n");
  129. else printf("NO\n");
  130. }
  131. return ;
  132. }

最大流

hdu3605 Escape 二分图多重匹配/最大流的更多相关文章

  1. HDU3605 Escape —— 二分图多重匹配

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

  2. POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65 ...

  3. POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

  4. POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 6 ...

  5. HDU - 3605 Escape (缩点+最大流/二分图多重匹配)

    题意:有N(1<=N<=1e5)个人要移民到M(1<=M<=10)个星球上,每个人有自己想去的星球,每个星球有最大承载人数.问这N个人能否移民成功. 分析:可以用最大流的思路求 ...

  6. 【网络流24题】No.7 试题库问题 (最大流,二分图多重匹配)

    [题意] 假设一个试题库中有 n 道试题. 每道试题都标明了所属类别. 同一道题可能有多个类别属性.现要从题库中抽取 m 道题组成试卷.并要求试卷包含指定类型的试题. 试设计一个满足要求的组卷算法. ...

  7. 网络流24题 第五题 - PowerOJ1740 CodeVS1905 圆桌问题 二分图多重匹配 网络最大流

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - PowerOJ1740 - 有SPJ - 推荐 题目传送门 - CodeVS1905 - 无SPJ - 0% ...

  8. HDU 3605 Escape(二分图多重匹配问题)

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

  9. poj 2289 Jamie's Contact Groups【二分+最大流】【二分图多重匹配问题】

    题目链接:http://poj.org/problem?id=2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K ...

随机推荐

  1. laravel 查询指定字段的值

    $this->model->where('id',$id)->value('user');

  2. break&&continue

    break和continue的区别: 1. 当它们用在循环语句的循环体时,break用于立即退出本层循环,而continue仅仅结束本次循环(本次循环体内不执行continue语句后的其它语句,但下一 ...

  3. OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/django'

    http://blog.csdn.net/qq_34078897/article/details/50821553 权限问题,sudo

  4. day05 数据类型

    一.整形int 基本使用: 1,用途:记录年龄\等级\各种号码 2定义方式: age=18     age =int(18) x =int(‘123’)#只能将纯数字的字符串转换成整形 print(t ...

  5. ubuntu14.04 解析不了域名—ubuntu的DNS配置

    问题描述: 电脑系统为ubuntu14.04,连上无线后,火狐浏览器打开www.baidu.com,提示找不到服务器,以及终端ping www.baidu.com,提示unkown host,但是浏览 ...

  6. 玩转X-CTR100 l STM32F4 l MPU6050加速度陀螺仪传感器

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      本文介绍X-CTR100控制器 板载加速度 ...

  7. matlabR2017安装

    安装教程参考: https://blog.csdn.net/m0_37638031/article/details/78982498

  8. JavaWeb:脚本标识

    脚本标识 一.JSP表达式 1.介绍 用于向页面中输出信息 2.语法格式 <%= 表达式%> 3.注意 在"<%"和"="之间不允许有空格,但 ...

  9. vue 手写组件 集合

    Num.1 :  链接 向右滑动, 显示删除按钮,  根据touchStart touchEnd 的 clientX 差距 > 30; 说明是向左滑动, 显示; 改变 e.currentTarg ...

  10. 前端框架VUE

    Vue Vue近几年来特别的受关注,三年前的时候angularJS霸占前端JS框架市场很长时间,接着react框架横空出世,因为它有一个特性是虚拟DOM,从性能上碾轧angularJS,这个时候,vu ...