题目链接:

  Hdu 3605  Escape

题目描述:

  有n个人要迁移到m个星球,每个星球有最大容量,每个人有喜欢的星球,问是否所有的人都能迁移成功?

解题思路:

  正常情况下建图,不会爆内存,但是TLE还是稳稳的。以前只遇到过网络流拆点建图,这个正好是缩点建图。吼吼吼~~~,建图的方式还是值得学习的。

  因为星球数目最多十个,那么无论有多少个人,其不同选择也就2^10种咯。把不同的选择作为节点,节点就从10^5减少到了2^10,整整缩小了一个数量级呢。建立源点和汇点,源点和选择链接,边权为这种选择的出现次数;每种选择再与可到达的星球相连,边权为INF;星球与汇点相连,边权为星球的最大容量。然后跑最大流,判断是否满流即可。

 #include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment (linker, "/STACK:1024000000,1024000000")
using namespace std; const int maxn = ;
const int INF = 0x3f3f3f3f; struct node
{
int to, next, w;
} edge[maxn**+];
int head[maxn+], Layer[maxn+], Hash[maxn+], tot; void Add (int from, int to, int w)
{
edge[tot].to = to;
edge[tot].w = w;
edge[tot].next = head[from];
head[from] = tot ++;
} int Scan ()
{
int res;
char ch;
res = ; while ((ch=getchar())<'' || ch>''); if (ch>='' && ch<='')
res = ch - ''; return res;
} bool CountLayer (int s, int e)
{
memset (Layer, , sizeof(Layer));
queue <int> Q;
Q.push (s);
Layer[s] = ;
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (int i=head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].to;
if (edge[i].w && !Layer[v])
{
Layer[v] = Layer[u] + ;
Q.push (v);
if (v == e)
return true;
}
}
}
return false;
} int Dfs (int u, int e, int maxflow)
{
if (u == e)
return maxflow; int uflow = ;
for (int i=head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].to;
if (!edge[i].w || Layer[v] != Layer[u]+)
continue; int flow = min (maxflow-uflow, edge[i].w);
flow = Dfs (v, e, flow);
uflow += flow;
edge[i].w -= flow;
edge[i^].w += flow;
if (maxflow == uflow)
break;
}
if (uflow == )
Layer[u] = ;
return uflow;
} int Dinic (int s, int e)
{
int maxflow = ;
while (CountLayer (s, e))
maxflow += Dfs (s, e, INF);
return maxflow;
} int main ()
{
int n, m, s, e, num;
while (scanf ("%d %d ", &n, &m) != EOF)
{
memset (head, -, sizeof(head));
memset (Hash, , sizeof(Hash));
tot = s = ;
e = (<<) + m + ;
for (int i=; i<=n; i++)
{
int y, x = ;
for (int j=; j<=m; j++)
{
int y = Scan();
x = x* + y;
}
Hash[x] ++;
} for (int i=; i<maxn; i++)
{
Add (s, i+, Hash[i]);
Add (i+, s, );
for (int j=; j<=m; j++)
if (i & <<(j-))
{
Add (i+, maxn+j, INF);
Add (maxn+j, i+, );
}
} for (int i=; i<=m; i++)
{
scanf ("%d", &num);
Add (maxn+i, e, num);
Add (e, maxn+i, );
} int ans = Dinic (s, e);
printf ("%s\n", ans == n ? "YES" : "NO");
}
return ;
}

Hdu 3605 Escape (最大流 + 缩点)的更多相关文章

  1. HDU 3605 Escape 最大流+状压

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

  2. HDU 3605 Escape 最大流

    题意: 如果这是2012年世界末日怎么办?我不知道该怎么做.但是现在科学家们已经发现,有些星球上的人可以生存,但有些人却不适合居住.现在科学家们需要你的帮助,就是确定所有人都能在这些行星上生活.输入多 ...

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

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

  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: 4000/2000 MS (Java/Others)    ...

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

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

  7. HDU 3605 Escape(状态压缩+最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意: 有n个人和m个星球,每个人可以去某些星球和不可以去某些星球,并且每个星球有最大居住人数,判断是否所 ...

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

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

  9. [hdu 3605]Escape

    这题的做法非常直观,却又非常不直观 先容许我吐一下槽吧~作者你的英语是读到火星上去了喵? 题目大体是说人类要移民,然后有 n 个人, m 个星球 每个人都有 m 个 0 . 1 数码表示他能否移民到该 ...

随机推荐

  1. jsoup 提取 html 中的所有链接、图片和媒体

    原文:http://www.open-open.com/code/view/1420729333515 package org.jsoup.examples; import org.jsoup.Jso ...

  2. google官方建议使用的网站性能测试工具

    转自:http://www.laokboke.net/2013/05/12/google-official-recommended-site-performance-testing-tools/ 最近 ...

  3. Hibernate复习之Hibernate基本介绍

    众所周知.眼下流行的面向对象的对象关系映射的Java持久层框架有MyBatis和Hibernate.他们都是对象关系映射ORM. 解决的主要问题就是对象-关系的映射.域模型和关系模型都分别建立在概念模 ...

  4. 问题解决:FFmpeg视频编解码库,无法解析的外部信号

    在编译FFmpeg相关项目时.可能会出现: error LNK2019: 无法解析的外部符号 "int __cdecl avpicture_fill(struct AVPicture *,u ...

  5. Exchanger使用

    Exchanger使用

  6. Hadoop在window上运行 user=Administrator, access=WRITE, inode="hadoop"

    win7下eclipse中错误的详细描述如下: org.apache.hadoop.security.AccessControlException: org.apache.hadoop.securit ...

  7. activiti自己定义流程之自己定义表单(二):创建表单

    注:环境配置:activiti自己定义流程之自己定义表单(一):环境配置 在上一节自己定义表单环境搭建好以后,我就正式開始尝试自己创建表单,在后台的处理就比較常规,主要是针对ueditor插件的功能在 ...

  8. 怎样更好的设计你的REST API之基于REST架构的Web Service设计及REST框架实现

    一.REST 含状态传输(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格. 眼下在 ...

  9. RAM、ROM和磁盘

     计算机存储数据的存储器主要分为RAM(随机訪问存储器).ROM.磁盘. RAM又分为SRAM和DRAM两种,SRAM用作快速缓存,DRAM用作主存. 1.SRAM SRAM又被称为静态RAM.利 ...

  10. api多版本方案(URL)

    api多版本方案(URL) 1.利用url https://www.taofen8.com/api/v2/getXXX 2.利用自定义请求头 api-version https://www.taofe ...