Steady Cow Assignment
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5092 | Accepted: 1766 |
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.
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.
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.
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.
Source
有n头牛,m个牛棚,每头牛对每个牛棚的喜欢程度都有一个等级,每个牛棚都有一个容量。
在牛棚能够承受的范围内,求使得所有牛中最高兴的与最不高兴的差值最小。
建图的话肯定是源点与牛相连,容量为1,牛棚与汇点相连,容量为牛棚能够容纳的数量。那么问题就出现在牛与牛棚之间如何相连了,每头牛对每个牛棚都有一个喜欢程度,总不能都相连,也不能只连一部分,而此题求的是差值最小,那么我们可以二分来枚举。
既然是差值最小,那么二分的时候,也是越分越小,所以一直分到不能再分的时候,此时求出的差值肯定是最小的。
//1000K 47MS
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 9999999
#define M 1007
#define MIN(a,b) a>b?b:a;
using namespace std;
int cow[M][21],cap[21],s,t,nn;
struct E
{
int v,w,next;
}edg[500000];
int dis[2000],gap[2000],head[2000],nodes;
int sourse,sink,n,m;
void addedge(int u,int v,int w)
{
edg[nodes].v=v;
edg[nodes].w=w;
edg[nodes].next=head[u];
head[u]=nodes++;
edg[nodes].v=u;
edg[nodes].w=0;
edg[nodes].next=head[v];
head[v]=nodes++;
}
int dfs(int src,int aug)
{
if(src==sink)return aug;
int left=aug,mindis=nn;
for(int j=head[src];j!=-1;j=edg[j].next)
{
int v=edg[j].v;
if(edg[j].w)
{
if(dis[v]+1==dis[src])
{
int minn=MIN(left,edg[j].w);
minn=dfs(v,minn);
edg[j].w-=minn;
edg[j^1].w+=minn;
left-=minn;
if(dis[sourse]>=nn)return aug-left;
if(left==0)break;
}
if(dis[v]<mindis)
mindis=dis[v];
}
}
if(left==aug)
{
if(!(--gap[dis[src]]))dis[sourse]=nn;
dis[src]=mindis+1;
gap[dis[src]]++;
}
return aug-left;
}
int sap(int s,int e)
{
int ans=0;
nn=e+1;
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
gap[0]=nn;
sourse=s;
sink=e;
while(dis[sourse]<nn)
ans+=dfs(sourse,inf);
return ans;
}
bool build(int mid)
{
for(int start=1;start<=m-mid+1;start++)
{
nodes=0;
s=0,t=n+m+1;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
{
addedge(s,i,1);
for(int j=start;j<=start+mid-1;j++)
addedge(i,n+cow[i][j],1);
}
for(int i=1;i<=m;i++)addedge(n+i,t,cap[i]);
if(sap(s,t)==n)return true;
}
return false;
}
int solve()
{
int l=1,r=m,mid,ans=-1;
while(l<=r)
{
mid=(l+r)>>1;
if(build(mid)){ans=mid;r=mid-1;}
else l=mid+1;
}
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&cow[i][j]);
for(int i=1;i<=m;i++)
scanf("%d",&cap[i]);
int ans=solve();
printf("%d\n",ans);
}
return 0;
}