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

题意:现有n个人要移居到m个星球去,给定一个n*m的矩阵,第 i 行第 j 列如果为1,表示第 i 个人可以去第 j 个星球,如果为0,表示不可以去。然后给出这m个星球都最多分别能住多少人,问你n个人是不是都能找到星球住? (1 <= n <= 100000), (1 <= m<= 10)

思路:看到这个n的范围我震惊了...然后不知道怎么做了...

           明显的最大流问题,不过n数目太大,直接做肯定超时. 留意到m最多有10个,所以每个人对去哪个星球的选择显然有2^10=1024种选择方案,那么就已经大大简化了复杂度

           建图:

           左边结点表示选择方案(编号从0到1023),右边的节点表示m个星球(编号从1023+1到1023+m),源点s为1024+m,汇点t为1025+m.

           比如这个数为1110000000(二进制)时表示选择1号,2号,3号星球居住的人的数量

           源点s到每个选择方案i有边(s,i,用该方案的人数)

           方案i中如果有选择星球j,那么有边(i,j,INF)

           星球j到汇点t有边(j,t,can[j]) can[j]表示星球j能容纳的最大人数.

           最终我们看 max_flow 是否== n 即可.


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 1200
#define INF 1<<29
#define LL long long
int cas=1,T;
struct Edge
{
	int from,to,cap,flow;
	Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
int n,m;
struct Dinic
{
//	int n,m;
    int s,t;
	vector<Edge>edges;        //边数的两倍
	vector<int> G[maxn];      //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
	bool vis[maxn];           //BFS使用
	int d[maxn];              //从起点到i的距离
	int cur[maxn];            //当前弧下标
	void init()
	{
	   for (int i=0;i<=(1<<m)+m+1;i++)
		   G[i].clear();
	   edges.clear();
	}
	void AddEdge(int from,int to,int cap)
	{
		edges.push_back(Edge(from,to,cap,0));
		edges.push_back(Edge(to,from,0,0));        //反向弧
		int mm=edges.size();
		G[from].push_back(mm-2);
		G[to].push_back(mm-1);
	}
	bool BFS()
	{
		memset(vis,0,sizeof(vis));
		queue<int>q;
		q.push(s);
		d[s]=0;
		vis[s]=1;
		while (!q.empty())
		{
			int x = q.front();q.pop();
			for (int i = 0;i<G[x].size();i++)
			{
				Edge &e = edges[G[x][i]];
				if (!vis[e.to] && e.cap > e.flow)
				{
					vis[e.to]=1;
					d[e.to] = d[x]+1;
					q.push(e.to);
				}
			}
		}
		return vis[t];
	}

	int DFS(int x,int a)
	{
		if (x==t || a==0)
			return a;
		int flow = 0,f;
		for(int &i=cur[x];i<G[x].size();i++)
		{
			Edge &e = edges[G[x][i]];
			if (d[x]+1 == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0)
			{
				e.flow+=f;
				edges[G[x][i]^1].flow-=f;
				flow+=f;
				a-=f;
				if (a==0)
					break;
			}
		}
		return flow;
	}

	int Maxflow(int s,int t)
	{
		this->s=s;
		this->t=t;
		int flow = 0;
		while (BFS())
		{
			memset(cur,0,sizeof(cur));
			flow+=DFS(s,INF);
		}
		return flow;
	}
}dc;
int num[maxn];         //可以支持i方案数的人数 
int can[maxn];
int main()
{
	while (scanf("%d%d",&n,&m)!=EOF)
	{
		memset(num,0,sizeof(num));
		for (int i = 1;i<=n;i++)
		{
			int temp=0;
			for (int j = m-1;j>=0;j--)
			{
				int v;
				scanf("%d",&v);
				if (v)
                   temp |= 1<<j;
			}
			++num[temp];
		}
		for (int i = 0;i<m;i++)
			scanf("%d",&can[i]);
		dc.init();
		for (int i = 0;i<(1<<m);i++)
		{
			if (num[i])
			{
				dc.AddEdge((1<<m)+m,i,num[i]);
			}
			for (int j = 0;j<m;j++)
				if (i&(1<<j))
					dc.AddEdge(i,(1<<m)+j,INF);
		}
		for (int i = 0;i<m;i++)
			if (can[i])
				dc.AddEdge((1<<m)+i,(1<<m)+1+m,can[i]);
		printf("%s\n",dc.Maxflow((1<<m)+m,(1<<m)+m+1) == n?"YES":"NO");
	}
}


Description

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. 
 

Input

More set of test data, the beginning of each data is n (1 <= n <= 100000), m (1 <= m <= 10) n indicate there n people on the earth, m representatives m planet, planet and people labels are from 0. Here are n lines, each line represents a suitable living conditions of people, each row has m digits, the ith digits is 1, said that a person is fit to live in the ith-planet, or is 0 for this person is not suitable for living in the ith planet. 
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most.. 
0 <= ai <= 100000 
 

Output

Determine whether all people can live up to these stars 
If you can output YES, otherwise output NO. 
 

Sample Input

       
       
1 1 1 1 2 2 1 0 1 0 1 1
 

Sample Output

       
       
YES NO
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值