http://acm.hdu.edu.cn/showproblem.php?pid=3605

题意:

有n个人和m个星球,每个人可以去某些星球和不可以去某些星球,并且每个星球有最大居住人数,判断是否所有人都能去这m个星球之中。

思路:

这道题建图是关键,因为n的数量很大,如果直接将源点和人相连,是会超时的,因为m≤10,所以每个人的选择可以用二进制来存储,这样最多也就1<<m-1种状态,相同的就可以放在一起处理了。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std; const int maxn=;
const int INF=0x3f3f3f3f; struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int w,int f):from(u),to(v),cap(w),flow(f){}
}; struct Dinic
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int cur[maxn];
int d[maxn]; void init(int n)
{
this->n=n;
for(int i=;i<n;++i) G[i].clear();
edges.clear();
} void AddEdge(int from,int to,int cap)
{
edges.push_back( Edge(from,to,cap,) );
edges.push_back( Edge(to,from,,) );
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS()
{
queue<int> Q;
memset(vis,,sizeof(vis));
vis[s]=true;
d[s]=;
Q.push(s);
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=;i<G[x].size();++i)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a)
{
if(x==t || a==) return a;
int flow=, f;
for(int &i=cur[x];i<G[x].size();++i)
{
Edge &e=edges[G[x][i]];
if(d[e.to]==d[x]+ && (f=DFS(e.to,min(a,e.cap-e.flow) ) )>)
{
e.flow +=f;
edges[G[x][i]^].flow -=f;
flow +=f;
a -=f;
if(a==) break;
}
}
return flow;
} int Maxflow(int s,int t)
{
this->s=s; this->t=t;
int flow=;
while(BFS())
{
memset(cur,,sizeof(cur));
flow +=DFS(s,INF);
}
return flow;
}
}DC; int n,m;
int num[maxn];
int src,dst; int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(num,,sizeof(num));
for(int i=;i<=n;i++)
{
int count=;
for(int j=m-;j>=;--j)
{
int p;
scanf("%d",&p);
if(p) count |= <<j;
}
++num[count];
} src=(<<m)+m;
dst=(<<m)++m;
DC.init((<<m)++m);
for(int i=;i<(<<m);++i)
if(num[i])
{
DC.AddEdge(src,i,num[i]);
for(int j=;j<m;++j)if(i&(<<j))
DC.AddEdge(i,(<<m)+j,INF);
}
for(int i=;i<m;++i)
{
int p;
scanf("%d",&p);
DC.AddEdge((<<m)+i,dst,p);
} printf("%s\n",DC.Maxflow(src,dst)==n?"YES":"NO");
}
return ;
}

HDU 3605 Escape(状态压缩+最大流)的更多相关文章

  1. Escape(状态压缩+最大流,好题)

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

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

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

  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 (最大流 + 缩点)

    题目链接: Hdu 3605  Escape 题目描述: 有n个人要迁移到m个星球,每个星球有最大容量,每个人有喜欢的星球,问是否所有的人都能迁移成功? 解题思路: 正常情况下建图,不会爆内存,但是T ...

  5. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  6. hdu 5724 SG+状态压缩

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

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

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

  8. HDU 3605 Escape 最大流+状压

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

  9. HDU 3605 Escape 最大流

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

  10. hdu 4272 LianLianKan 状态压缩

      LianLianKan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

随机推荐

  1. Unity3D笔记五 快捷键

    一.近距离查看游戏对象 在Hierarchy视图中选择游戏对象,然后在Scene视图中按快捷键“F”来近距离查看该游戏对象. 二.游戏对象不在主摄像头中? Hierarchy中双击选择需要显示的游戏对 ...

  2. shell爬虫简易脚本(线程数可控)

    1.介绍 以机电之家网站为例 经过初步分析,机电之家的数据量较大,并且数据组织规则较为统一,适合以代码方式进行全量爬取. 企业列表URL统一为http://www.jdzj.com/yp_vlist_ ...

  3. 详解spring boot mybatis全注解化

    本文重点介绍spring boot mybatis 注解化的实例代码 1.pom.xml //引入mybatis <dependency> <groupId>org.mybat ...

  4. beans有无状态

    Spring Bean Scopes https://www.tutorialspoint.com/spring/spring_bean_scopes.htm When defining a < ...

  5. ES_DEVOPS-1

    When dealing with a large number of parallel operations in elasticsearch, such as search requests or ...

  6. 大规模Docker平台自动化监控之路

    本文介绍了通过Monitor,如何实现大规模容器运维平台的自动化监控需求. 尽管Docker技术目前还处于不稳定的发展与标准制定阶段,但这门技术已经呈现了极其火热的增长状态,却已经是不争的实事.到底有 ...

  7. django的framework优化

    1.优化framework的性能,解决restapi调用慢的问题 ①预加载,关联查询时做缓存,序列化前简单调用setup_eager_loading ,这个需要确定sql查询调用情况(根据数据库结构确 ...

  8. WPF自定义代码启动应用程序启动方式

    删除app.xaml 添加App.cs类 第一种方式: class App    {        [STAThread]        static void Main()        {     ...

  9. android 错误收集

    2. is not translated in Eclipse > Preference > Android > Lint Error Checking的Correctness: M ...

  10. Spark UI (基于Yarn) 分析与定制

    转载自:https://yq.aliyun.com/articles/60194 摘要: 这篇文章的主旨在于让你了解Spark UI体系,并且能够让你有能力对UI进行一些定制化增强.在分析过程中,你也 ...