Steady Cow Assignment
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5033 | Accepted: 1740 |
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.
法一:多重匹配:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
using namespace std;
const int maxn=1005;
const int maxm=30;
int map[maxn][maxm],val[maxm];
bool vis[maxm];
int match[maxm][maxn],cnt[maxm];
int n,b,low,high;
bool find(int x)
{
for(int i=1; i<=b; i++)
if(!vis[i]&&map[x][i]>=low&&map[x][i]<=high)
{
vis[i]=true;
if(cnt[i]<val[i])
{
match[i][cnt[i]++]=x;
return true;
}
for(int j=0;j<cnt[i];j++)
if(find(match[i][j]))
{
match[i][j]=x;
return true;
}
}
return false;
}
int MMG()
{
int ans=0;
memset(cnt,0,sizeof(cnt));
for(int i=1; i<=n; i++)
{
memset(vis,false,sizeof(vis));
if(find(i)) ans++;
}
return ans;
}
int main()
{
int k;
while(~scanf("%d%d",&n,&b))
{
for(int i=1;i<=n;i++)
for(int j=1;j<=b;j++)
{
scanf("%d",&k);
map[i][k]=j;
}
for(int i=1;i<=b;i++)
scanf("%d",&val[i]);
low=high=1;
int ans=b;
while(low<=high&&high<=b)
{
int t=MMG();
if(t==n)
{
if(high-low+1<ans)
ans=high-low+1;
low++;
}
else high++;
}
printf("%d\n",ans);
}
return 0;
}
法二:二分最大流:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
#define eps 1e-6
using namespace std;
const int INF=1000000000;
const int maxn=1100;
struct Edge{
int u,v,cap,flow,next;
}et[maxn*maxn];
int low[maxn],cnt[maxn],dis[maxn],cur[maxn],pre[maxn],eh[maxn];
int rank[maxn][25],capa[25];
int n,m,s,t,num;
void init(){
memset(eh,-1,sizeof(eh));
num = 0;
}
void add(int u,int v,int cap,int flow){
Edge e = {u,v,cap,flow,eh[u]};
et[num] = e;
eh[u] = num ++;
}
void addedge(int u,int v,int cap){
add(u,v,cap,0);
add(v,u,0,0);
}
void build_graph(int l,int r)
{
init();
for(int i = 1; i <= n; i ++)
{
addedge(s,i,1);
for(int j = l; j <= r; j ++)
addedge(i,rank[i][j]+n,1);
}
for(int i = 1; i <= m; i ++)
addedge(i+n,t,capa[i]);
}
int isap(int s,int t,int nv)
{
int u,v,now,flow = 0;
memset(cnt,0,sizeof(cnt));
memset(dis,0,sizeof(dis));
memset(low,0,sizeof(low));
for(u = 0; u <= nv; u ++) cur[u] = eh[u];
low[s] = INF, cnt[0] = nv, u = s;
while(dis[s]<nv)
{
for(now = cur[u]; now != -1; now = et[now].next)
if(et[now].cap-et[now].flow&&dis[u] == dis[v = et[now].v]+1) break;
if(now != -1)
{
cur[u] = pre[v] = now;
low[v] = min(et[now].cap-et[now].flow,low[u]);
u = v;
if(u == t)
{
for(;u != s; u = et[pre[u]].u)
{
et[pre[u]].flow += low[t];
et[pre[u]^1].flow -= low[t];
}
flow += low[t];
low[s] = INF;
}
}
else
{
if(--cnt[dis[u]] == 0) break;
dis[u] = nv, cur[u] = eh[u];
for(now = eh[u]; now != -1; now = et[now].next)
if(et[now].cap-et[now].flow&&dis[u] > dis[et[now].v]+1)
dis[u] = dis[et[now].v]+1;
cnt[dis[u]] ++;
if(u != s) u = et[pre[u]].u;
}
}
return flow;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
s = 0;
t = n+m+1;
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",&capa[i]);
int low = 1,high = 1,ans = m;
while(low <= high&&high <= m)
{
build_graph(low,high);
int tmp = isap(s,t,t+1);
if(tmp == n)
{
if(ans > high-low+1) ans = high-low+1;
low ++;
}
else high ++;
}
printf("%d\n",ans);
}
return 0;
}