POJ - 3189 Steady Cow Assignment二分图多重匹配+二分
题意: 一共有n头牛,并给出了对m个大棚的喜爱程度,问在满足容量的情况下,对大棚的喜爱程度之差最小为多少
做法: 一开始我读错题,后来发现可以二分区间长度,当所有牛对大棚的喜爱程度都在 [ i , i + m i d − 1 ] [i,i+mid-1] [i,i+mid−1]这个区间内,最大匹配数可以达到n,就符合条件
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<queue>
#include<map>
#define ll long long
#define pb push_back
#define rep(x,a,b) for (int x=a;x<=b;x++)
#define repp(x,a,b) for (int x=a;x<b;x++)
#define W(x) printf("%d\n",x)
#define WW(x) printf("%lld\n",x)
#define pi 3.14159265358979323846
#define mem(a,x) memset(a,x,sizeof a)
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
using namespace std;
const int maxn=1e3+7;
const int INF=1e9;
const ll INFF=1e18;
int cnt[maxn],V[maxn];
int mapp[maxn][25],a[maxn][25];
int linker[maxn][25],n,m,sx,ex,x;
bool vis[maxn];
bool dfs(int u)
{
for (int i=1;i<=m;i++)
{
if (mapp[u][i]<=ex&&mapp[u][i]>=sx&&!vis[i])
{
vis[i]=true;
if (cnt[i]<V[i])
{
linker[i][cnt[i]++]=u;
return true;
}
for (int j=0;j<V[i];j++)
{
if (dfs(linker[i][j]))
{
linker[i][j]=u;
return true;
}
}
}
}
return false;
}
bool solve()
{
mem(cnt,0);
for (int i=1;i<=n;i++)
{
mem(vis,false);
if (!dfs(i))return false;
}
return true;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
rep(i,1,n)rep(j,1,m)scanf("%d",&x),mapp[i][x]=j;
rep(i,1,m)scanf("%d",&V[i]);
int l=1,r=m;
while(l<r)
{
int mid=(l+r)>>1;
bool mark=false;
for (int q=1;q<=m-mid+1;q++)
{
sx=q,ex=q+mid-1;
if (solve())mark=true;
}
if (mark)r=mid;
else l=mid+1;
}
W(r);
}
}
我真想要石页老板的心态。。。