POJ3189 Steady Cow Assignment 最大流+枚举

建图,牛到源点弧的容量为1,牛到牛棚弧的容量为1,牛棚到汇点弧的容量为相应棚的容量。
枚举,每次枚举一个区间,如果该level区间内的图,所求出的最大流等于牛的数量,则输出区间的差值。
Steady Cow Assignment
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4127 Accepted: 1444

Description

Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which, of course, have limited capacity. Some cows really like their current barn, and some are not so happy.

FJ would like to rearrange the cows such that the cows are as equally happy as possible, even if that means all the cows hate their assigned barn.

Each cow gives FJ the order in which she prefers the barns. A cow's happiness with a particular assignment is her ranking of her barn. Your job is to find an assignment of cows to barns such that no barn's capacity is exceeded and the size of the range (i.e., one more than the positive difference between the the highest-ranked barn chosen and that lowest-ranked barn chosen) of barn rankings the cows give their assigned barns is as small as possible.

Input

Line 1: Two space-separated integers, N and B

Lines 2..N+1: Each line contains B space-separated integers which are exactly 1..B sorted into some order. The first integer on line i+1 is the number of the cow i's top-choice barn, the second integer on that line is the number of the i'th cow's second-choice barn, and so on.

Line N+2: B space-separated integers, respectively the capacity of the first barn, then the capacity of the second, and so on. The sum of these numbers is guaranteed to be at least N.

Output

Line 1: One integer, the size of the minumum range of barn rankings the cows give their assigned barns, including the endpoints.

Sample Input

6 4
1 2 3 4
2 3 1 4
4 2 3 1
3 1 2 4
1 3 4 2
1 4 2 3
2 1 3 2

Sample Output

2

Hint

Explanation of the sample:

Each cow can be assigned to her first or second choice: barn 1 gets cows 1 and 5, barn 2 gets cow 2, barn 3 gets cow 4, and barn 4 gets cows 3 and 6.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define MAXN 1500
#define MAXM 9999999
#define INF  0xFFFFFF

struct edge
{
	int to,c,next;
};

edge e[MAXM];
int que[MAXN*100],dis[MAXN],pre[MAXN],st,ed,maxflow;
int head[MAXN],head2[MAXN],en,n,m;
int rank[1100][30];
int c[30];

void add(int a,int b,int c)
{
	e[en].to=b;
	e[en].c=c;
	e[en].next=head[a];
	head[a]=en++;
	e[en].to=a;
	e[en].c=0;
	e[en].next=head[b];
	head[b]=en++;	
} 

bool bfs() 
{
	memset(dis,-1,sizeof(dis));
	que[0]=st,dis[st]=1;
	int t=1,f=0;
	while(f<t)
	{
		int j=que[f++];
		for(int k=head[j];k!=-1;k=e[k].next)
		{	
			int i=e[k].to;
			if(dis[i]==-1 && e[k].c)
			{
				que[t++]=i;
				dis[i]=dis[j]+1;
				if(i==ed) return true;
			}
		}
	}
	return false; 
} 

int update()  
{  
	int p,flow=INF;  
    for (int i=pre[ed];i!=-1;i=pre[i])
		if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;    
    for (int i=pre[ed];i!=-1;i=pre[i]) 
		e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;   
    maxflow+=flow; 
    return p;
}  

void dfs()  
{  
	memset(pre,-1,sizeof(pre));
	memcpy(head2,head,sizeof(head2));  
    for(int i=st,j;i!=-1;)  
    {  
        int flag=false;  
        for(int k=head[i];k!=-1;k=e[k].next)    
          if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )  
          {  
                pre[j]=i; 
				head2[i]=k;  
				i=j; 
				flag=true;  
                if(i==ed) 
					i=update();  
                if(flag) 
					break;  
          }  
        if (!flag) dis[i]=-1,i=pre[i];   
    }  
} 

int dinic()
{
	maxflow=0;
	while(bfs())
		dfs();
	return maxflow;
}

bool build(int l,int r)
{
	
	int sum=0;
	memset(head,-1,sizeof(head));
	en=0;
	for(int i=1;i<=n;i++)
		add(st,i,1);
	for(int i=1;i<=n;i++)
		for(int j=l;j<=r;j++)
			add(i,rank[i][j]+n,1);
	for(int i=1;i<=m;i++)
	{
		add(i+n,ed,c[i]);
		sum+=c[i];
	}
	if(sum<n)
		return false;
	return true;
}

void solve()
{
	int temp; 
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) 
			scanf("%d",&rank[i][j]);
	for(int i=1;i<=m;i++)
		scanf("%d",&c[i]);
	st=0,ed=n+m+1;
	for(int i=1;i<=m;i++)
	{
		for(int j=1;j+i-1<=m;j++)
		{
			build(j,j+i-1);
			int ret=dinic();
			if(ret==n)
			{
				printf("%d\n",i);
				return;
			}
		}
	}
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
		solve();		
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值