Steady Cow Assignment
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6414 | Accepted: 2199 |
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.
题意:有n头牛和b个牛棚,每头牛对于每个牛棚有个喜爱度,比如样例,第一头牛对于1号牛棚的喜爱度是1,对于4号牛棚的喜爱度是4. 每个牛棚有容量限制,最多只能呆几头牛,输入保证每头牛都可以有牛棚呆。问你这群牛的喜爱度之差(最大喜爱度-最小喜爱度+1)是多少
思路:构图很简单,二分法枚举喜爱度,然后再枚举下界,用最大流判断是否每头牛都有牛棚住。最后求出的mid就是最小喜爱度之差
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define N 5500
#define INF 1<<28
struct Edge
{
int to,next,cap;
} edge[N*100];
int cnt,head[N],d[N];
int ma[N][N],a[N];
void addedge(int from,int to,int cap)
{
edge[cnt].to=to;
edge[cnt].cap=cap;
edge[cnt].next=head[from];
head[from]=cnt++;
edge[cnt].to=from;
edge[cnt].cap=0;
edge[cnt].next=head[to];
head[to]=cnt++;
}
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
int bfs(int s,int t)
{
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==-1&&edge[i].cap>0)
{
d[v]=d[u]+1;
q.push(v);
}
}
}
return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
if(s==t||f==0) return f;
int flow=0;
for(int i=head[s]; i!=-1&&flow<f; i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==d[s]+1&&edge[i].cap>0)
{
int x=min(f-flow,edge[i].cap);
x=dfs(v,t,x);
flow+=x;
edge[i].cap-=x;
edge[i^1].cap+=x;
if(f==0) break;
}
}
if(!flow) d[s]=-2;
return flow;
}
int Dinic(int s,int t)///起点s终点t
{
int flow=0,f;
while(bfs(s,t))
{
while(f=dfs(s,t,INF))
flow+=f;
}
return flow;
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
int S=0,T=n+m+2;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%d",&ma[i][j]);
for(int i=1; i<=m; i++)
scanf("%d",&a[i]);
int low=0,high=m+1,mid,ans=-1;
while(low<high)
{
int mid=(low+high)>>1;
for(int i=1; i<=m; i++)
{
init();
for(int i=1; i<=n; i++)
addedge(m+i,T,1);
for(int i=1; i<=m; i++)
addedge(S,i,a[i]);
for(int j=i; j<=i+mid-1&&j<=m; j++)
for(int k=1;k<=n;k++)
addedge(ma[k][j],m+k,INF);
if(Dinic(S,T)==n) {
ans=high=mid;
break;
}
else continue;
}
if(high!=mid) low=mid+1;
}
printf("%d\n",ans);
}
return 0;
}