POJ 3189. Steady Cow Assignment

链接

http://poj.org/problem?id=3189

题意

n n n 头牛分配给 m m m 个谷仓,每个谷仓容量不同,给出每头牛对于 m m m 个谷仓喜欢程度的排名。

求最小区间(选择的最大喜欢程度的排名 - 最小喜欢程度的排名)。

思路

二分图多重匹配,尺取。

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <bitset>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef double DB;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
//head
const int N=1005;
const int M=25;
int n,m,f[N][M],cap[M],cnt[M],match[M][N];
bool st[M];
bool dfs(int u,int l,int r) {
    for(int i=l;i<=r;i++) {
        int v=f[u][i];
        if(st[v]) continue;
        st[v]=true;
        if(cnt[v]<cap[v]) {
            match[v][++cnt[v]]=u;
            return true;
        }
        for(int i=1;i<=cnt[v];i++)
            if(dfs(match[v][i],l,r)) {
                match[v][i]=u;
                return true;
            }
    }
    return false;
}
bool check(int l,int r) {
    for(int i=1;i<=m;i++) cnt[i]=0;
    int res=0;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) st[j]=false;
        if(dfs(i,l,r)) res++;
    }
    if(res==n) return true;
    return false;
}
int main() {
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%d",&f[i][j]);
    for(int i=1;i<=m;i++) scanf("%d",&cap[i]);
    int l=1,r=1,res=0x3f3f3f3f;
    while(l<=r&&r<=m) {
        if(check(l,r)) res=min(res,r-l+1),l++;
        else r++;
    }
    printf("%d\n",res);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值