HDU 3605 Escape(状压+最大流)

Escape

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 12620    Accepted Submission(s): 3129


 

Problem 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

思路:很简单的网络流模型,但边的数目很多,会T,但每个人总共有2的10方的选择,把相同的选择的人利用状态压缩合并一下就行了

#include <bits/stdc++.h>
using namesp ace std;
const int MAXN = 2000;
const int MAXM = 11000;
const int INF = 0x3f3f3f3f;
struct Edge1
{
	int from,to,cap,flow;
};
struct Dinic
{
	int n,m,s,t;
	vector<Edge1> edges;
	vector<int> G[MAXN];
	bool vis[MAXN];
	int d[MAXN];
	int cur[MAXN];
	void init(int n) 
	{
		this -> n = n;
		for(int i = 0; i <= n + 1; i++){
			G[i].clear();
		}
		edges.clear();
	}
	void AddEdge(int from,int to,int cap) 
	{
		edges.push_back((Edge1){from,to,cap,0});	
		edges.push_back((Edge1){to,from,0,0});
		m = edges.size();
		G[from].push_back(m - 2);
		G[to].push_back(m - 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++) {
				Edge1& 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++) {
			Edge1& 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;
	}
}din;
int vis[1500];
int main(void)
{
	int n,m,src,des,sa,temp;
	while(scanf("%d %d",&n,&m) != EOF) {
		memset(vis,0,sizeof(vis));
		din.init((1 << m) + m + 2);
		//0 src   1 ------ 1 << m - 1  condition   (1 << m) ----- (1 << m) + m - 1 plant
		//1 << m + m des 
		src = 0,des = (1 << m) + m;
		for(int i = 0; i < n; i++) {
			sa = 0;
			for(int j = 0; j < m; j++) {
				scanf("%d",&temp);
				if(temp) sa |= (1 << j);	
			}
			vis[sa]++;
		}
		for(int i = 1; i < (1 << m); i++) {
			if(vis[i]) {
				din.AddEdge(src,i,vis[i]);
				for(int j = 0; j < m; j++) {
					if(i & (1 << j)) {
						din.AddEdge(i,(1 << m) + j,vis[i]);
					}
				}
			}
		}
		for(int j = 0; j < m; j++) {
			scanf("%d",&temp);
			din.AddEdge((1 << m) + j,des,temp);
		}
		printf("%s\n",din.Maxflow(src,des) == n ? "YES" : "NO");
	}
	return 0;
}
/*
1 1
1
1

2 2
1 0
1 0
1 1
*/

猜你喜欢

转载自blog.csdn.net/GYH0730/article/details/81783650