HDU 3605 Escape 最大流or二分图多重匹配 2010 ACM-ICPC Multi-University Training Contest(17)——Host by ZSTU

 

Escape

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1122 Accepted Submission(s): 321


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
Source
2010 ACM-ICPC Multi-University Training Contest(17)——Host by ZSTU

Recommend
lcy
 
 
一看就是一个网络流,建图套上模板提交,RE...
边算小了,改大变成TLE,然后用尽各种方法还是TLE
开始怀疑不是网络流了,因为点太多了。
换成二分图多重匹配,TLE。。。
杯具的以为今晚要0AC了(又是一次练习赛)。。。
于是忍不住去搜了一下解题报告,
确实有网络流解法,不过n=10^6肯定TLE,不过m<=10很小,
那么所有人的选择只有2^m种,统计这个2^m中选择的数量,
这2^m种选择作为节点连接到m个星球节点边权为统计的数量,
源点s到这些节点边权同上(我写的inf一直WA,为什么会错呢),
m个星球节点到汇点t连边,边权为星球容量。
这样10^6的节点减少到10^3就能不TLE了。
其实下午才刚做了一道类似的缩小数据范围的题,怎么晚上就忘了呢。。。
ACMDIY第二届群赛 Ahui Writes Word :一个01背包问题,可是n=100000,v=10000
肯定TLE,不过体积和价值都是《=10,可以把它转换为100类物品的多重背包,
再用二进制压缩转换为01背包求解。
-------------------------------------------------------------------------------------------------------
本题还可以用二分图多重匹配解,刚开始把标记数组开大了,导致一直TLE...
标记数组开10就够了,我弄了个10万,不超时才怪。。。
 
还有这个要用C++提交才不超时,不过可以用输入外挂G++、C++都非常快
 
最大流代码:
#include<cstdio>
#include<cstring>
#define N 2005
#define M 30005
#define inf 999999999
#define min(a,b) ((a)<(b)?(a):(b))

int n,m,s,t,num,adj[N],dis[N],q[N];
struct edge
{
	int v,w,pre;
	edge(){}
	edge(int vv,int ww,int p){v=vv;w=ww;pre=p;}
}e[M];
void insert(int u,int v,int w)
{
	e[num]=edge(v,w,adj[u]);
	adj[u]=num++;
	e[num]=edge(u,0,adj[v]);
	adj[v]=num++;
}
int bfs()
{
	int i,x,v,head=0,tail=0;
	memset(dis,0,sizeof(dis));
	dis[s]=1;
	q[++tail]=s;
	while(head!=tail)
	{
		x=q[head=(head+1)%N];
		for(i=adj[x];~i;i=e[i].pre)
			if(e[i].w&&!dis[v=e[i].v])
			{
				dis[v]=dis[x]+1;
				if(v==t)
					return 1;
				q[tail=(tail+1)%N]=v;
			}
	}
	return 0;
}
int dfs(int x,int limit)
{
	if(x==t)
		return limit;
	int i,v,tmp,cost=0;
	for(i=adj[x];~i&&cost<limit;i=e[i].pre)
		if(e[i].w&&dis[x]==dis[v=e[i].v]-1)
		{
			tmp=dfs(v,min(limit-cost,e[i].w));
			if(tmp)
			{
				e[i].w-=tmp;
				e[i^1].w+=tmp;
				cost+=tmp;
			}
			else
				dis[v]=-1;
		}
	return cost;
}
int Dinic()
{
	int ans=0;
	while(bfs())
		ans+=dfs(s,inf);
	return ans;
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		int i,j,k,a,w,x[2005]={0};
		memset(adj,-1,sizeof(adj));
		num=0;
		s=0;
		t=2000;
		for(i=0;i<n;i++)
		{
			k=0;
			for(j=0;j<m;j++)
			{
				scanf("%d",&a);
				if(a)
					k|=1<<j;
			}
			x[k]++;
		}
		for(i=0;i<(1<<m);i++)
			if(x[i])
			{
				insert(s,i+1,x[i]);
				for(j=0;j<m;j++)
					if(i&(1<<j))
						insert(i+1,j+10+(1<<m),x[i]);
			}
		for(j=0;j<m;j++)
		{
			scanf("%d",&w);
			insert(j+10+(1<<m),t,w);
		}
		puts(Dinic()>=n?"YES":"NO");
	}
}


二分图多重匹配代码:
#include<cstdio>
#include<cstring>
#include<iostream>

int n,m,w[15],cnt[15];
int adj[100010][12],mat[12][100010];
bool f[15];
bool dfs(int x)
{
	int i,j,v;
	for(i=0;i<m;i++)
		if(!f[i]&&adj[x][i])
		{
			f[i]=1;
			if(cnt[i]<w[i])
			{
				mat[i][cnt[i]++]=x;
				return true;
			}
			for(j=0;j<cnt[i];j++)
				if(dfs(mat[i][j]))
				{
					mat[i][j]=x;
					return true;
				}
		}
	return false;
}
bool ok()
{	
	int i;
	memset(cnt,0,sizeof(cnt));
	for(i=0;i<n;i++)
	{
		memset(f,0,sizeof(f));
		if(!dfs(i))
			return false;
	}
	return true;
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		int i,j;
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
				scanf("%d",&adj[i][j]);
		for(j=0;j<m;j++)
			scanf("%d",&w[j]);
		puts(ok()?"YES":"NO");
	}
}

你可能感兴趣的:(insert,360,each,output,2010,Training)