ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[kuangbin] POJ3189 Steady Cow Assignment 二分图多重匹配+二分

2019-09-21 16:06:02  阅读:268  来源: 互联网

标签:二分 ch Cow cow int Assignment 区间 cows barn


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.

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.

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.


题目大意:每个牛对于每个牛棚都有一个喜好值,现在要重新分配牛棚,问怎样分配牛,才可以使所有牛的喜好值 在一个最小的区间呢.

题目中有两个坑:

第一 mp[i][j]不代表是 i牛对j牛棚的喜好值,代表i牛对mp[i][j]牛棚的喜好值为j

第二 求的是区间长度 而不是区间最大值-最小值

题目思路:

首先很明显,是个二分的题目,二分当前这个区间长度,看在这个区间长度内 ,是否可以把牛都分配完.

怎样二分这个区间长度?

我们假设当前区间长度为 mid

因为喜好值 最大 为 m ,所以我们没救 [i,i+mid-1]这个区间,只有在这个区间的喜好值 这个牛才可以选这个牛棚,就这样跑一遍多重匹配,如果检测成功,就变小,否则变大;

AC:

//#include<bits/stdc++.h>
#include<queue>
#include<algorithm>
#include <string.h>
using namespace std;
typedef long long ll;
const int maxn=1505;
const ll INF=1e9+7;
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
ll n,m,p;
vector<int>v[1500];
char str[1500];
bool used[1500];
vector<int>nR[1500];
bool dfs(int x,int limit)
{
    int sz=v[x].size();
    for(int i=0;i<sz;i++)
    {
        int e=v[x][i];
        if(!used[e])
        {
            used[e]=true;
            int sz=nR[e].size();
            if(sz<limit)
            {
                nR[e].push_back(x);
                return true;
            }
            else
            {
                for(int k=0;k<sz;k++)
                {
                    if(dfs(nR[e][k],limit))
                    {
                        nR[e][k]=x;
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
bool judge(int x)
{
    for(int i=1;i<=n;i++)
    {
        memset(used,false,sizeof(used));
        if(!dfs(i,x)) return false;
    }
    return true;
}
ll AC()
{
    ll ans=0;
    ll l=1,r=n;
    while(l<=r)
    {
        for(int i=0;i<=m;i++) nR[i].clear();
        ll mid=(l+r)/2;
        if(judge(mid))
        {
            ans=mid;
            r=mid-1;
        }
        else
            l=mid+1;
    }
    return ans;
}
int main()
{
    while(scanf("%lld%lld",&n,&m),n+m)
    {
        for(int i=0;i<=n;i++) v[i].clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",str);
            while(getchar()!='\n')
            {
               int x;scanf("%d",&x);
               v[i].push_back(x);
            }
        }
        printf("%lld\n",AC());
    }
    return 0;
}

总结:

1.二分区间长度可以通过枚举!!

标签:二分,ch,Cow,cow,int,Assignment,区间,cows,barn
来源: https://blog.csdn.net/qq_43857314/article/details/101108909

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有