HDU 3605Escape(缩点+网络流之最大流)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3605
本来打算昨天写两道题的,结果这个题卡住了,最后才发现是最后的推断条件出错了,推断满流的条件应该是与n的比較,居然写成与全部星球总容量的比較了。(近期大脑短路。。)
这题也不是全然自己想的,没想到缩点这一技巧,由于n的数据范围太大,普通的建图方法会超时超内存,须要缩点,由于对于每一个点来说,一共仅仅有2^10种方法,而最多一共同拥有10W个点,显然有非常多点是反复的,这时能够採取缩点的方法,将反复的当成一个点来处理。这样数据范围就缩小到了1024个点,速度大大提升。
建图思路是建一源点与汇点,将每种方法与源点相连,权值为这样的方法反复的次数,将每一个星球与汇点相连,权值为每一个星球的最大容量,再将每种方法与星球相连,权值为INF,最后推断是否满流。
代码例如以下:
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #include <ctype.h>
- #include <queue>
- #include <map>
- #include <algorithm>
- using namespace std;
- const int INF=1e9;
- int head[2000], s, t, nv, n, cnt;
- int num[2000], d[2000], pre[2000], cur[2000], q[2000], fei[2000];
- struct node
- {
- int u, v, next, cap;
- }edge[1000000];
- void add(int u, int v, int cap)
- {
- edge[cnt].v=v;
- edge[cnt].cap=cap;
- edge[cnt].next=head[u];
- head[u]=cnt++;
- edge[cnt].v=u;
- edge[cnt].cap=0;
- edge[cnt].next=head[v];
- head[v]=cnt++;
- }
- void bfs()
- {
- memset(num,0,sizeof(num));
- memset(d,-1,sizeof(d));
- int f1=0, f2=0, i;
- q[f1++]=t;
- d[t]=0;
- num[0]=1;
- while(f1>=f2)
- {
- int u=q[f2++];
- for(i=head[u];i!=-1;i=edge[i].next)
- {
- int v=edge[i].v;
- if(d[v]==-1)
- {
- d[v]=d[u]+1;
- num[d[v]]++;
- q[f1++]=v;
- }
- }
- }
- }
- int isap()
- {
- memcpy(cur,head,sizeof(cur));
- int flow=0, i, u=pre[s]=s;
- bfs();
- while(d[s]<nv)
- {
- if(u==t)
- {
- int f=INF, pos;
- for(i=s;i!=t;i=edge[cur[i]].v)
- {
- if(f>edge[cur[i]].cap)
- {
- f=edge[cur[i]].cap;
- pos=i;
- }
- }
- for(i=s;i!=t;i=edge[cur[i]].v)
- {
- edge[cur[i]].cap-=f;
- edge[cur[i]^1].cap+=f;
- }
- flow+=f;
- if(flow>=n)
- return flow;
- u=pos;
- }
- for(i=cur[u];i!=-1;i=edge[i].next)
- {
- if(d[edge[i].v]+1==d[u]&&edge[i].cap)
- {
- break;
- }
- }
- if(i!=-1)
- {
- cur[u]=i;
- pre[edge[i].v]=u;
- u=edge[i].v;
- }
- else
- {
- if(--num[d[u]]==0) break;
- int mind=nv;
- for(i=head[u];i!=-1;i=edge[i].next)
- {
- if(mind>d[edge[i].v]&&edge[i].cap)
- {
- mind=d[edge[i].v];
- cur[u]=i;
- }
- }
- d[u]=mind+1;
- num[d[u]]++;
- u=pre[u];
- }
- }
- return flow;
- }
- int main()
- {
- int m, x, i, j, top, y, z, num, a[20];
- while(scanf("%d%d",&n,&m)!=EOF)
- {
- memset(head,-1,sizeof(head));
- memset(fei,0,sizeof(fei));
- cnt=0;
- s=0;
- top=0;
- num=0;
- for(i=1;i<=n;i++)
- {
- x=0;
- for(j=1;j<=m;j++)
- {
- scanf("%d",&y);
- x=x*2+y;
- }
- fei[x]++;
- }
- for(i=1;i<=1100;i++)
- {
- if(fei[i])
- {
- num++;
- }
- }
- t=num+m+1;
- nv=t+1;
- for(i=1;i<=1100;i++)
- {
- if(fei[i])
- {
- //printf("--%d %d\n", i, fei[i]);
- top++;
- add(s,top,fei[i]);
- x=i;
- z=m+1;
- while(x)
- {
- y=x%2;
- z--;
- if(y)
- {
- add(top,z+num,INF);
- }
- //printf("--%d %d %d %d--",y, top, z, num);
- x=x/2;
- }
- //printf("\n");
- }
- }
- for(i=1;i<=m;i++)
- {
- scanf("%d",&x);
- add(i+num,t,x);
- }
- x=isap();
- if(x>=n)
- printf("YES\n");
- else
- printf("NO\n");
- }
- return 0;
- }
HDU 3605Escape(缩点+网络流之最大流)的更多相关文章
- HDU 3605 Escape (网络流,最大流,位运算压缩)
HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...
- HDU 4289 Control (网络流,最大流)
HDU 4289 Control (网络流,最大流) Description You, the head of Department of Security, recently received a ...
- HDU 4292 Food (网络流,最大流)
HDU 4292 Food (网络流,最大流) Description You, a part-time dining service worker in your college's dining ...
- HDU 4280Island Transport(网络流之最大流)
题目地址:pid=4280">http://acm.hdu.edu.cn/showproblem.php? pid=4280 这个题是一个纯最大流模板题..就是用来卡时间的.. 还好我 ...
- HDU 4183Pahom on Water(网络流之最大流)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4183 这题题目意思非常难看懂..我看了好长时间也没看懂..终于是从网上找的翻译. .我就在这翻译一下吧 ...
- HDU 3549 Flow Problem 网络流(最大流) FF EK
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- HDU 3416 Marriage Match IV (最短路径,网络流,最大流)
HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...
- HDU 3338 Kakuro Extension (网络流,最大流)
HDU 3338 Kakuro Extension (网络流,最大流) Description If you solved problem like this, forget it.Because y ...
- POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...
随机推荐
- 使用 STL 辅助解决算法问题
不要重复制造轮子,而且你造的轮子未必比得上别人的: <numeric>⇒ accumulate,累积容器中区间的和,可以指定初值: 为什么 STL 中的容器和算法一定关于区间的操作一定是左 ...
- 1.4 Python基础知识 - 代码书写格式及条件判断"if ... else ..."
一.代码的书写规则 在所有的开发语言中,代码之间都是有关联的关系,有的是包含关系,有的是上下级关系,有的是代表语句的结束.在python中也是有相应的规则的: 1.在没有上下级关系的代码中,代码要顶行 ...
- 非常全的linux面试笔试题及参考答案
一.填空题: 1. 在Linux系统中,以 文件 方式访问设备 . 2. Linux内核引导时,从文件/etc/fstab 中读取要加载的文件系统. 3. Linux文件系统中每个文件用 i节点来标识 ...
- amazeui页面分析3
amazeui页面分析3 一.总结 1. 本质是list列表,是ul套li的形式,只不过li里面是图片 <li class="am-g am-list-item-desced am-l ...
- 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 ...
- 如何获取已经安装到苹果手机上的App信息
//如何获取已经安装到苹果手机上的App信息? Is it possible to get the information (app icon, app name, app location) abo ...
- 幻灯展示jQuery插件supersized
主要特性: 能够自动修改图片大小适合浏览器的页面大小 通过幻灯展示的循环背景可以动态加载并且可以设置变化方式 核心版本可以支持仅仅需要背景变化大小的需要 键盘导航 整合Flickr - 可以从用户,组 ...
- complex query几个原则
1.一般来说in比exists更有利(更容易变成join). 2.尽量避免union,使用union all代替,避免sort. 3,绝对不能在没有on条件下使用join(除非有特殊目的). 4.ou ...
- [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 ...
- 使用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 ...