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;
	int re = 0;
	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;
				flow -= nowflow;
				re += nowflow;
				if(!flow)break;
			}
		}
	}
	return re;
}
 
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;
} 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值