HDU-3605 Escape (网络流 + 状态压缩)

HDU-3605 Escape (网络流 + 状态压缩)

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

题目大意:

有n个人,m个星球,每个人都有适合居住的星球,每个星球都有一个最大容量。
问,是否能让所有的人都有星球可以居住。

解题思路:

乍一看就是裸的网络流
建一个原点,连每个人流量为1,每个人连适合的星球,每个星球连超级汇点,流量是最大容量。然后建图跑网络流。
一开始我也是这么写的,T了。
因为n是1e5,所以这样建图会T,然后想办法简化建图的方法。
我们发现m最大只有10,所以想到状态压缩1-1024之间的每一个数都代表一个状态,原点连这每个状态,流量是这个状态出现的次数,然后每个状态连它对应的星球(二进制为1的位置) 表示这个状态和这个星球有联系,流量也是为出现的次数,最后是m个星球连超级汇点,流量是星球容量
这样大大简化了建图,最多只有 1+1024+10+1 个点。

在这里插入图片描述

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <algorithm> 
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+1000;
int head[maxn],cnt = 0;
int mp[2000];
int n,m;
int sp,tp;
struct node{
	int to;
	int w;
	int next;
}side[maxn*10];
int deep[maxn];
int cur[maxn];
void init(){
	memset(head,-1,sizeof(head));
	memset(mp,0,sizeof(mp));
	cnt = 0;
}
void add(int x,int y,int w){
	side[cnt].to = y;
	side[cnt].w = w;
	side[cnt].next = head[x];
	head[x] = cnt++;
}
bool bfs(){
	memset(deep,-1,sizeof(deep));
	queue<int> q;
	q.push(sp);
	deep[sp] = 0;
	while(q.size()){
		int now = q.front();
		q.pop();
		for(int i=head[now];i!=-1;i=side[i].next){
			int to = side[i].to;
			if(deep[to]==-1&&side[i].w>0){
				deep[to] = deep[now]+1;
				q.push(to);
				if(to == tp) break;
			}
		}
	}
	return deep[tp]!=-1;
}
int dfs(int u,int cap){
	if(u ==tp||cap ==0) return cap;
	int res = 0;
	int f;
	for(int &i=cur[u];i!=-1;i=side[i].next){
		int to = side[i].to;
		if(deep[to] ==deep[u]+1&&(f=dfs(to,min(cap-res,side[i].w)))>0){
			side[i].w-=f;
			side[i^1].w+=f;
			res+=f;
			if(res == cap) return cap;
		}
	}
	if(!res) deep[u]=-1;
	return res;
}
int Dinic(){
	int ans = 0;
	while(bfs()){
		for(int i=1;i<=tp;i++){
			cur[i] = head[i];
		}
		ans +=dfs(sp,inf);
	}
	return ans;
}
int main(){
	while(~scanf("%d%d",&n,&m)){
		int tmp;
		init();
		int u,v1,v2;
		sp = 1;
		tp = m+1026;
		for(int i=1;i<=n;i++){
			int sum = 0;
			int ff = 1;
			for(int j=1;j<=m;j++){
				scanf("%d",&tmp);
				sum += tmp*ff;
				ff*=2;
			}
			mp[sum]++;
		} 
		for(int i=1;i<=1024;i++){
			add(1,i+1,mp[i]);
			add(i+1,1,0);
		}
		for(int i=1;i<=1024;i++){
			int ff =1;
			int tmp = i;
			while(tmp){
				if(tmp%2){
					u = ff+1025;
					add(i+1,u,mp[i]);
					add(u,i+1,0);
				}
				tmp/=2;
				ff++;
			}
		}
		for(int i=1;i<=m;i++){
			scanf("%d",&tmp);
			add(i+1025,tp,tmp);
			add(tp,i+1025,0);
		}
		int ans = Dinic();
		if(ans>=n) puts("YES");
		else puts("NO");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值