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

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最大有100000,不过星球数m最大只有10,所以这么1e5的人中一定有重复的情况,所以可以缩点,将人分为1024种状态,并记录每个状态的人数,然后将源点与每个状态建边,容量为这个状态的人数,每个状态与对应的几个星球建边,容量为这个状态人数,每个星球与汇点建边,容量为这个星球的容量。

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e6+5;
const int maxm=5e6+5;
int head[maxn],cur[maxn],dep[maxn],ne,pre[maxn],num[maxn],n,m,S,T,maxflow;
queue<int> q;
struct edge
{
	int next,to,cost;
}e[maxm*2];
void add(int u,int v,int w)
{
	e[ne].to=v;
	e[ne].cost=w;
	e[ne].next=head[u];
	head[u]=ne++;
	e[ne].to=u;
	e[ne].cost=0;
	e[ne].next=head[v];
	head[v]=ne++;
}
void bfs(int t)
{
	while(!q.empty()) q.pop();
	for(int i=0;i<=T;i++) cur[i]=head[i];//这里要修改 
	for(int i=0;i<=T;i++) dep[i]=T+1;//这里要修改
	dep[t]=0;
	q.push(t);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			if(dep[e[i].to]==T+1&&e[i^1].cost)//这里要修改
			{
				dep[e[i].to]=dep[u]+1;
				q.push(e[i].to);
			}
		}
	}
}
int addflow(int s,int t)
{
	int ans=INF,u=t;
	while(u!=s)
	{
		ans=min(ans,e[pre[u]].cost);
		u=e[pre[u]^1].to;
	}
	u=t;
	while(u!=s)
	{
		e[pre[u]].cost-=ans;
		e[pre[u]^1].cost+=ans; 
		u=e[pre[u]^1].to;
	}
	return ans;
}
void ISAP(int s,int t)
{
	int u=s;
	bfs(t);
	for(int i=0;i<=T;i++) num[dep[i]]++;//这里要修改
	while(dep[s]<T+1)//这里要修改
	{
		if(u==t) 
		{
			maxflow+=addflow(s,t);
			u=s;
		}
		int flag=0;
		for(int &i=cur[u];i!=-1;i=e[i].next)
		{
			if(dep[u]==dep[e[i].to]+1&&e[i].cost)
			{
				flag=1;
				pre[e[i].to]=i;
				u=e[i].to;
				break;
			}
		}
		if(!flag)
		{
			int minn=T;//这里要修改
			for(int i=head[u];i!=-1;i=e[i].next)
			{
				if(e[i].cost)
				{
					minn=min(minn,dep[e[i].to]);
				}
			}
			if((--num[dep[u]])==0) break;
			dep[u]=minn+1;
			num[dep[u]]++;
			cur[u]=head[u];
			if(u!=s) u=e[pre[u]^1].to;
		}
	}
}
map<int,int> M;
void init()
{
	memset(head,-1,sizeof(head));
	ne=0;
	memset(num,0,sizeof(num));
	maxflow=0;
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		init();
		M.clear();
		int cnt=0;
		S=0;
		for(int i=1;i<=n;i++)
		{
			int tmp=0;
			for(int j=1;j<=m;j++)
			{
				int x;
				scanf("%d",&x);
				tmp+=(x<<j);
			}
			M[tmp]++;
		}
		T=M.size()+1+m;
		map<int,int>::iterator it;
		for(it=M.begin();it!=M.end();it++)
		{
			cnt++;
			for(int i=1;i<=m;i++)
			{
				if((it->first)&(1<<i))
				{
					add(cnt,M.size()+i,it->second);
				}
			}
			add(S,cnt,it->second);
		}
		for(int i=1;i<=m;i++)
		{
			int x;
			scanf("%d",&x);
			add(M.size()+i,T,x);
		}
		ISAP(S,T);
		if(maxflow==n) puts("YES");
		else puts("NO");
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值