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的范围非常大,在网络流题中这种范围基本上正常套路是无解的,所以肯定是需要技巧的。

然后发现把N中重复的合并后只有2^10中情况,所以可以进行缩点。用10个位上的01来表示N的情况话就可以把N的情况转换成一个int。之后就是基础的最大流。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>

using namespace std;

const int MAXN = 1050;
const int INF = 0x3f3f3f3f;

struct Edge{
	int flow,to,rev;
	Edge(){}
	Edge(int a,int b,int c):to(a),flow(b),rev(c){}
};

vector<Edge> E[MAXN];

inline void Add(int from,int to,int flow){
	E[from].push_back(Edge(to,flow,E[to].size()));
	E[to].push_back(Edge(from,0,E[from].size()-1));
}

int deep[MAXN];

bool BFS(int from,int to){
	memset(deep,-1,sizeof deep);
	deep[from] = 0;
	queue<int> Q;
	Q.push(from);
	while(!Q.empty()){
		int t = Q.front();
		Q.pop();
		for(int i=0 ; i<E[t].size() ; ++i){
			Edge& e = E[t][i];
			if(e.flow > 0 && deep[e.to] == -1){
				deep[e.to] = deep[t] + 1;
				Q.push(e.to);
			}
		}
	}
	return deep[to] != -1;
}

int iter[MAXN];

int DFS(int from,int to,int flow){
	if(from == to || flow == 0)return flow;
	for(int& i=iter[from] ; i<E[from].size() ; ++i){
		Edge& e = E[from][i];
		if(e.flow > 0 && deep[e.to] == deep[from] + 1){
			int nowflow = DFS(e.to,to,min(flow,e.flow));
			if(nowflow > 0){
				e.flow -= nowflow;
				E[e.to][e.rev].flow += nowflow;
				return nowflow;
			}
		}
	}
	return 0;
}

int Dinic(int from,int to){
	int sumflow = 0;
	while(BFS(from,to)){
		memset(iter,0,sizeof iter);
		int mid;
		while((mid = DFS(from,to,INF)) > 0)sumflow += mid;
	}
	return sumflow;
}

int board[MAXN];

int main(){
	
	int N,M;
	while(scanf("%d %d",&N,&M)!=EOF){
		memset(board,0,sizeof board);
		int p = 0;
		for(int i=0 ; i<N ; ++i){
			int sum = 0,t;
			for(int j=0 ; j<M ; ++j){
				scanf("%d",&t);
				sum = sum * 2 + t;
			}
			board[sum]++;
		}
		for(int i=1 ; i<=M ; ++i){
			p <<= 1;
			++p; 
			int t;
			scanf("%d",&t);
			Add(i,MAXN-1,t);
		}
		for(int i=1 ; i<=p ; ++i){
			if(board[i]){
				Add(0,i+10,board[i]);
				int t = i;
				for(int j=M ; t ; --j){
					if(t&1){
						Add(i+10,j,INF);
					}
					t >>= 1;
				}
			}
		}
		if(Dinic(0,MAXN-1) == N)printf("YES\n");
		else printf("NO\n");
		for(int i=0 ; i<MAXN ; ++i)E[i].clear();
	}
	
	return 0;
} 

posted @ 2018-07-13 16:30  Assassin_poi君  阅读(113)  评论(0编辑  收藏  举报