题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3605

本来打算昨天写两道题的,结果这个题卡住了,最后才发现是最后的推断条件出错了,推断满流的条件应该是与n的比較,居然写成与全部星球总容量的比較了。(近期大脑短路。。)

这题也不是全然自己想的,没想到缩点这一技巧,由于n的数据范围太大,普通的建图方法会超时超内存,须要缩点,由于对于每一个点来说,一共仅仅有2^10种方法,而最多一共同拥有10W个点,显然有非常多点是反复的,这时能够採取缩点的方法,将反复的当成一个点来处理。这样数据范围就缩小到了1024个点,速度大大提升。

建图思路是建一源点与汇点,将每种方法与源点相连,权值为这样的方法反复的次数,将每一个星球与汇点相连,权值为每一个星球的最大容量,再将每种方法与星球相连,权值为INF,最后推断是否满流。

代码例如以下:

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <ctype.h>
  7. #include <queue>
  8. #include <map>
  9. #include <algorithm>
  10.  
  11. using namespace std;
  12. const int INF=1e9;
  13. int head[2000], s, t, nv, n, cnt;
  14. int num[2000], d[2000], pre[2000], cur[2000], q[2000], fei[2000];
  15. struct node
  16. {
  17. int u, v, next, cap;
  18. }edge[1000000];
  19. void add(int u, int v, int cap)
  20. {
  21. edge[cnt].v=v;
  22. edge[cnt].cap=cap;
  23. edge[cnt].next=head[u];
  24. head[u]=cnt++;
  25.  
  26. edge[cnt].v=u;
  27. edge[cnt].cap=0;
  28. edge[cnt].next=head[v];
  29. head[v]=cnt++;
  30. }
  31. void bfs()
  32. {
  33. memset(num,0,sizeof(num));
  34. memset(d,-1,sizeof(d));
  35. int f1=0, f2=0, i;
  36. q[f1++]=t;
  37. d[t]=0;
  38. num[0]=1;
  39. while(f1>=f2)
  40. {
  41. int u=q[f2++];
  42. for(i=head[u];i!=-1;i=edge[i].next)
  43. {
  44. int v=edge[i].v;
  45. if(d[v]==-1)
  46. {
  47. d[v]=d[u]+1;
  48. num[d[v]]++;
  49. q[f1++]=v;
  50. }
  51. }
  52. }
  53. }
  54. int isap()
  55. {
  56. memcpy(cur,head,sizeof(cur));
  57. int flow=0, i, u=pre[s]=s;
  58. bfs();
  59. while(d[s]<nv)
  60. {
  61. if(u==t)
  62. {
  63. int f=INF, pos;
  64. for(i=s;i!=t;i=edge[cur[i]].v)
  65. {
  66. if(f>edge[cur[i]].cap)
  67. {
  68. f=edge[cur[i]].cap;
  69. pos=i;
  70. }
  71. }
  72. for(i=s;i!=t;i=edge[cur[i]].v)
  73. {
  74. edge[cur[i]].cap-=f;
  75. edge[cur[i]^1].cap+=f;
  76. }
  77. flow+=f;
  78. if(flow>=n)
  79. return flow;
  80. u=pos;
  81. }
  82. for(i=cur[u];i!=-1;i=edge[i].next)
  83. {
  84. if(d[edge[i].v]+1==d[u]&&edge[i].cap)
  85. {
  86. break;
  87. }
  88. }
  89. if(i!=-1)
  90. {
  91. cur[u]=i;
  92. pre[edge[i].v]=u;
  93. u=edge[i].v;
  94. }
  95. else
  96. {
  97. if(--num[d[u]]==0) break;
  98. int mind=nv;
  99. for(i=head[u];i!=-1;i=edge[i].next)
  100. {
  101. if(mind>d[edge[i].v]&&edge[i].cap)
  102. {
  103. mind=d[edge[i].v];
  104. cur[u]=i;
  105. }
  106. }
  107. d[u]=mind+1;
  108. num[d[u]]++;
  109. u=pre[u];
  110. }
  111. }
  112. return flow;
  113. }
  114. int main()
  115. {
  116. int m, x, i, j, top, y, z, num, a[20];
  117. while(scanf("%d%d",&n,&m)!=EOF)
  118. {
  119. memset(head,-1,sizeof(head));
  120. memset(fei,0,sizeof(fei));
  121. cnt=0;
  122. s=0;
  123. top=0;
  124. num=0;
  125. for(i=1;i<=n;i++)
  126. {
  127. x=0;
  128. for(j=1;j<=m;j++)
  129. {
  130. scanf("%d",&y);
  131. x=x*2+y;
  132. }
  133. fei[x]++;
  134. }
  135. for(i=1;i<=1100;i++)
  136. {
  137. if(fei[i])
  138. {
  139. num++;
  140. }
  141. }
  142. t=num+m+1;
  143. nv=t+1;
  144. for(i=1;i<=1100;i++)
  145. {
  146. if(fei[i])
  147. {
  148. //printf("--%d %d\n", i, fei[i]);
  149. top++;
  150. add(s,top,fei[i]);
  151. x=i;
  152. z=m+1;
  153. while(x)
  154. {
  155. y=x%2;
  156. z--;
  157. if(y)
  158. {
  159. add(top,z+num,INF);
  160. }
  161. //printf("--%d %d %d %d--",y, top, z, num);
  162. x=x/2;
  163. }
  164. //printf("\n");
  165. }
  166. }
  167. for(i=1;i<=m;i++)
  168. {
  169. scanf("%d",&x);
  170. add(i+num,t,x);
  171. }
  172. x=isap();
  173. if(x>=n)
  174. printf("YES\n");
  175. else
  176. printf("NO\n");
  177. }
  178. return 0;
  179. }

HDU 3605Escape(缩点+网络流之最大流)的更多相关文章

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

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

  2. HDU 4289 Control (网络流,最大流)

    HDU 4289 Control (网络流,最大流) Description You, the head of Department of Security, recently received a ...

  3. HDU 4292 Food (网络流,最大流)

    HDU 4292 Food (网络流,最大流) Description You, a part-time dining service worker in your college's dining ...

  4. HDU 4280Island Transport(网络流之最大流)

    题目地址:pid=4280">http://acm.hdu.edu.cn/showproblem.php? pid=4280 这个题是一个纯最大流模板题..就是用来卡时间的.. 还好我 ...

  5. HDU 4183Pahom on Water(网络流之最大流)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4183 这题题目意思非常难看懂..我看了好长时间也没看懂..终于是从网上找的翻译. .我就在这翻译一下吧 ...

  6. HDU 3549 Flow Problem 网络流(最大流) FF EK

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  7. HDU 3416 Marriage Match IV (最短路径,网络流,最大流)

    HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...

  8. HDU 3338 Kakuro Extension (网络流,最大流)

    HDU 3338 Kakuro Extension (网络流,最大流) Description If you solved problem like this, forget it.Because y ...

  9. POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)

    POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...

随机推荐

  1. 使用 STL 辅助解决算法问题

    不要重复制造轮子,而且你造的轮子未必比得上别人的: <numeric>⇒ accumulate,累积容器中区间的和,可以指定初值: 为什么 STL 中的容器和算法一定关于区间的操作一定是左 ...

  2. 1.4 Python基础知识 - 代码书写格式及条件判断"if ... else ..."

    一.代码的书写规则 在所有的开发语言中,代码之间都是有关联的关系,有的是包含关系,有的是上下级关系,有的是代表语句的结束.在python中也是有相应的规则的: 1.在没有上下级关系的代码中,代码要顶行 ...

  3. 非常全的linux面试笔试题及参考答案

    一.填空题: 1. 在Linux系统中,以 文件 方式访问设备 . 2. Linux内核引导时,从文件/etc/fstab 中读取要加载的文件系统. 3. Linux文件系统中每个文件用 i节点来标识 ...

  4. amazeui页面分析3

    amazeui页面分析3 一.总结 1. 本质是list列表,是ul套li的形式,只不过li里面是图片 <li class="am-g am-list-item-desced am-l ...

  5. Android java.lang.IllegalArgumentException: Object returned from onCreateLoader must not be a non-static inn

    AsyncTaskLoader: http://developer.Android.com/intl/zh-CN/reference/android/content/AsyncTaskLoader.h ...

  6. 如何获取已经安装到苹果手机上的App信息

    //如何获取已经安装到苹果手机上的App信息? Is it possible to get the information (app icon, app name, app location) abo ...

  7. 幻灯展示jQuery插件supersized

    主要特性: 能够自动修改图片大小适合浏览器的页面大小 通过幻灯展示的循环背景可以动态加载并且可以设置变化方式 核心版本可以支持仅仅需要背景变化大小的需要 键盘导航 整合Flickr - 可以从用户,组 ...

  8. complex query几个原则

    1.一般来说in比exists更有利(更容易变成join). 2.尽量避免union,使用union all代替,避免sort. 3,绝对不能在没有on条件下使用join(除非有特殊目的). 4.ou ...

  9. [Vue] Create Vue.js Layout and Navigation with Nuxt.js

    Nuxt.js enables you to easily create layout and navigation by replacing the default App.vue template ...

  10. 使用LAMP创建基于wordpress的个从博客网站 分类: B3_LINUX 2014-07-15 16:45 800人阅读 评论(0) 收藏

    参考: http://blog.csdn.net/ck_boss/article/details/27866117 一.mysql配置 1.安装mysql yum install mysql-serv ...