Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3611 | Accepted: 1245 |
Description
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
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
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
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.
問的是牛滿意度最小和最大之間的差值最小是多少
枚舉+最大流 (kb模板)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
//POJ 3189
//design by hl
//orz kb
using namespace std;
const int MAXN = 100010 ; //點數最大值
const int MAXM = 400010 ; //邊數最大值
const int INF = 0x3f3f3f3f;
int S,V,F,P,ISUM,N,B;
struct Edge{
int to,next,cap,flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];
int imap[1111][22],IMAX;
int rongliang[22];
void init(){
tol = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw=0){
edge[tol].to = v;
edge[tol].cap = w;
edge[tol].next = head[u];
edge[tol].flow = 0;
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = rw;
edge[tol].next = head[v];
edge[tol].flow = 0;
head[v] = tol++;
}
//最大流開始
int sap(int start,int end,int N){
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -1;
gap[0] = N;
int ans = 0;
while(dep[start] < N){
if(u==end){
int Min = INF;
for(int i=pre[u];i!= -1; i=pre[edge[i^1].to])
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
for(int i=pre[u];i!=-1;i=pre[edge[i^1].to]){
edge[i].flow += Min;
edge[i^1].flow -=Min;
}
u=start;
ans +=Min;
continue;
}
bool flag = false;
int v;
for(int i= cur[u];i!=-1;i=edge[i].next){
v=edge[i].to;
if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){
flag=true;
cur[u]=pre[v]=i;
break;
}
}
if(flag){
u=v;
continue;
}
int Min = N;
for(int i=head[u];i!= -1;i=edge[i].next)
if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min){
Min=dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if(!gap[dep[u]]) return ans;
dep[u] = Min +1;
gap[dep[u]]++;
if(u!=start) u = edge[pre[u]^1].to;
}
return ans;
}
//最大流結束
bool build(int l,int r){
int i,j,k,m,n;
init();
for(i=1;i<=N;i++){
addedge(S,i,1);
}
for(i=1;i<=B;i++){
addedge(i+N,V,rongliang[i]);
}
for(i=1;i<=N;i++){
for(j=l;j<=r;j++){
addedge(i,imap[i][j]+N,1);
}
}
int orz=sap(S,V,V+1);
//printf("orz=%d\n",orz);
return orz==N;
}
int solve(){
int i,j,k,orz;
for(i=1;i<=B;i++){
for(j=1;i+j-1<=B;j++){
orz =build(j,i+j-1);
if(orz){
return i;
}
}
}
}
int main(){
int m,n,q,p;
int i,j,k,a,b,c;
//讀入
scanf("%d%d",&N,&B);
for(i=1;i<=N;i++){
for(j=1;j<=B;j++){
scanf("%d",&imap[i][j]);
}
}
for(i=1;i<=B;i++) scanf("%d",&rongliang[i]);
//初始化S V
S=0;
V=N+B+1;
ISUM=0;
int orz=solve();
printf("%d\n",orz);
return 0;
}