USACO 40 The Perfect Stall

Description

Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall.
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.

Input

The input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.

Output

For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.

Sample Input

5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2 

Sample Output

4
解析

这道题需要建图。用一个超级源和超级汇,然后求最大流。

需要注意的问题有两点 1.点的序号 超级源0号 奶牛1~N号 牛栏N+1~N+M号 超级汇N+M+1号。特别是奶牛和牛栏的序号特别容易搞混。然后图要开成400*400的,而不是200*200的。2.超级汇和超级源的流量不是INF而是1。因为题上说了每个奶牛和牛栏只能用一次

//dinic
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

#define INF ~0u>>2

int M,N,Map[410][410],dis[410],SuperS,SuperT;

void readdata()
{
	memset(Map,0,sizeof(Map));
	//memset(dis,0,sizeof(dis));

	SuperS=0; SuperT=N+M+1;
	for(int i=1;i<=N;i++)
	{
		Map[0][i]=1;
		int si;	scanf("%d",&si);
		for(int j=1;j<=si;j++)
		{
			int a; scanf("%d",&a);
			Map[i][N+a]=1;
		}
	}
	for(int i=N+1;i<=N+M;i++) Map[i][SuperT]=1;
}

bool bfs()
{
	memset(dis,0,sizeof(dis));
	queue<int> q;
	dis[SuperS]=1; q.push(SuperS);
	while(!q.empty())
	{
		int v=q.front(); q.pop();
		for(int u=SuperS;u<=SuperT;u++)
			if(!dis[u] && Map[v][u]>0)
			{
				dis[u]=dis[v]+1;
				q.push(u);
				if(u==SuperT) return 1;
			}
	}
	return 0;
}

int dfs(int v,int flow)
{
	if(v==SuperT || flow==0) return flow;

	int cap=flow;
	for(int u=SuperS;u<=SuperT;u++)
		if(dis[u]==dis[v]+1 && Map[v][u]>0)
		{
			int tmp=dfs(u,min(cap,Map[v][u]));
			cap-=tmp;
			Map[v][u]-=tmp;Map[u][v]+=tmp;
		}
	return flow-cap;
}

void dinic()
{
	int ans=0;
	while(bfs()) 
		ans+=dfs(SuperS,INF);
	printf("%d",ans);
}

int main()
{
	freopen("poj1274.in","r",stdin);

	while(scanf("%d%d",&N,&M)==2)
	{
		readdata();
		dinic();
		printf("\n");
	}

	while(1);
	return 0;
}

//sap
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define INF ~0u>>2

int N,M,SuperS,SuperT,Map[410][410],dis[410],vd[410];

void readdata()
{
	memset(Map,0,sizeof(Map));
	memset(dis,0,sizeof(dis));
	memset(vd,0,sizeof(vd));
	
	SuperS=0; SuperT=N+M+1;
	for(int i=1;i<=N;i++)
	{
		Map[SuperS][i]=1;
		int si;scanf("%d",&si);
		for(int j=1;j<=si;j++)
		{
			int a;scanf("%d",&a);
			Map[i][N+a]=1;
		}
	}
	for(int i=N+1;i<=N+M;i++)Map[i][SuperT]=1;
}

int dfs(int v,int flow)
{
	if(v==SuperT || flow==0) return flow;

	int ans=0;
	for(int u=SuperS;u<=SuperT;u++)
		if(dis[v]==dis[u]+1 && Map[v][u]>0)
		{
			int tmp=dfs(u,min(Map[v][u],flow-ans));
			ans+=tmp;
			Map[v][u]-=tmp;Map[u][v]+=tmp;		
			if(ans==flow) return ans;
		}
	if(dis[SuperS]>=N+M+2) return ans;
	//--vd[dis[v]];
	if(--vd[dis[v]]<=0) dis[SuperS]=N+M+2;
	dis[v]++; vd[dis[v]]++;
	return ans;
}

void sap()
{
	vd[0]=N+M+2;
	int ans=0;
	while(dis[SuperS]<N+M+2) 
		ans+=dfs(SuperS,INF);
	printf("%d",ans);
}

int main()
{
	freopen("poj1274.in","r",stdin);
	while(scanf("%d%d",&N,&M)==2)
	{
		readdata();
		sap();
		printf("\n");
	}

	while(1);
	return 0;
}

当前弧优化,cur[x]存放的是一条连着x的弧,最新发现的允许弧,也就是最近刚刚更新过残余流量的弧。

更新dis[x]时当前弧更新为第一条弧;查找x连着的边(有一说是更新残量网络的时候,我不明白)的时候同时更新cur[x];

百度百科有算法介绍,某blog

关于cur为什么能提高效率……


红边表示流满了,绿边是还没流满。中间的点是x

当扫了A点之后,A和D流满了,E没满。所以cur[x]=E。

那么下一次B又走到x的时候就直接从E开始,而不是从0开始。

//sap+gap+cur
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>

using namespace std;
#define INF 0x3f3f3f3f
struct Edge
{
	int from,to,cap;
	Edge(int a,int b,int c)
	{from=a; to=b; cap=c;}
};
int N,M;
vector<int> Chain[410];
vector<Edge> edge;
int S,T;
int vd[410],dis[410],cur[410];
void AddEdge(int f,int to,int cap)
{
	edge.push_back(Edge(f,to,cap));
	Chain[f].push_back(edge.size()-1);
	edge.push_back(Edge(to,f,0));
	Chain[to].push_back(edge.size()-1);
}
void initial()
{
	S=0; T=N+M+1;
	for(int i=1;i<=N;i++) AddEdge(S,i,1);
	for(int i=1;i<=M;i++) AddEdge(N+i,T,1);
}
int dfs(int u,int f)
{
	if(u==T) return f;
	int ans=0;
	for(int i=cur[u];i<Chain[u].size();i++)
	{
		cur[u]=i;
		int k=Chain[u][i];
		int v=edge[k].to;
		if(edge[k].cap>0 && dis[v]+1==dis[u])
		{
			int tmp=dfs(v,min(f-ans,edge[k].cap));
			edge[k].cap-=tmp; edge[k^1].cap+=tmp;
			ans+=tmp;
			if(ans==f) return ans;
		}
	}
	if(dis[S]==N+M+2) return ans;
	vd[dis[u]]--;
	if(vd[dis[u]]==0) dis[T]=N+M+2;
	dis[u]++; vd[dis[u]]++;
	cur[u]=0;
	return ans;
}

int MaxFlow()
{
	vd[0]=N+M+2;
	int ans=0;
	while(dis[S]<N+M+2)
		ans+=dfs(S,INF);
	return ans;
}
void PrintV(vector<int> &a)
{
	for(int i=0;i<a.size();i++) printf("%d ",a[i]);
	printf("\n");
}
int main()
{
	freopen("G.in","r",stdin);
	scanf("%d%d",&N,&M);
	initial();
	for(int i=1;i<=N;i++)
	{
		int t; scanf("%d",&t);
		for(int j=1;j<=t;j++)
		{
			int a; scanf("%d",&a);
			AddEdge(i,N+a,1);
		}
	}
	printf("%d\n",MaxFlow());
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值