Steady Cow Assignment(二分图多重匹配+二分)(网络流)

Steady Cow Assignment

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ
3189

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.

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.

网友的不错,仿一下。。。,其实网络流大同小异

题目描述:

  有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度。当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚子的最大容量了,棚子是拒绝的。现在要给每个奶牛安家,本宝宝是一个公正无私的人类,所以要找一个奶牛喜爱程度差值最小的方案(虽然这样大家的喜爱程度可能普遍偏低,因为绝对公平并不代表合理啊),问喜爱程度的区间最小为多大?

解题思路:

  每个棚子并不是住一个奶牛,所以是多重匹配咯。匹配的时候二分枚举喜爱程度的区间大小,根据区间大小来枚举区间的起点和终点,然后跑多重匹配判断是否合法即可。注意咯,求得是区间大小。还有啊,还有啊,数据读入的时候maps[i][j]并不是i_th奶牛对j_th棚子的喜爱值,而是i_th奶牛对maps[i][j]棚子的喜爱程度为j。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 1010;
int maps[maxn][22], vis[22], used[22][maxn];
int link[22], limit[22], n, m, s, e, mid;
bool Find (int u)
{
    for (int i=1; i<=m; i++)
    {//多重匹配
        if (!vis[i] && maps[u][i]<e && s<=maps[u][i])
        {
            vis[i] = 1;
            if (link[i] < limit[i])
            {//i棚子未满
                used[i][link[i] ++] = u;
                return true;
            }
            for (int j=0; j<limit[i]; j++)//i棚子已满,寻找增广路
                if (Find(used[i][j]))
                {
                    used[i][j] = u;
                    return true;
                }
        }
    }
    return false;
}
bool hungry ()
{
    for (s=1; s<=m+1-mid; s++)
    {//枚举区间起点与终点
        int ans = 0;
        e = s + mid;
        memset (link, 0, sizeof(link));
        for (int i=1; i<=n; i++)
        {
            memset (vis, 0, sizeof(vis));
            if (Find (i))
                ans ++;
        }
        if (ans == n)
            return true;
    }
    return false;
}
int main ()
{
    while (scanf ("%d %d", &n, &m) != EOF)
    {
        for (int i=1; i<=n; i++)
            for (int j=1; j<=m; j++)
            {
                scanf ("%d", &mid);
                maps[i][mid] = j;
            }
        for (int i=1; i<=m; i++)
            scanf ("%d", &limit[i]);
        int left = 1, right = m, ans = 0;
        while (left<=right)
        {//二分枚举区间
            mid = (right+left)/2;
            if (hungry())
                {
                    ans = mid;
                    right = mid - 1 ;
                }
            else
                left = mid + 1 ;
        }
        printf ("%d\n", ans);
    }
    return 0;
}
时间: 2024-07-29 09:41:07

Steady Cow Assignment(二分图多重匹配+二分)(网络流)的相关文章

POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分

题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6979   Accepted: 2418 Description Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns wh

POJ3189_Steady Cow Assignment(二分图多重匹配/网络流)

解题报告 http://blog.csdn.net/juncoder/article/details/38340447 题目传送门 题意: B个猪圈,N头猪,每头猪对每个猪圈有一个满意值,要求安排这些猪使得最大满意和最小满意的猪差值最小 思路: 二分图的多重匹配问题; 猪圈和源点连边,容量为猪圈容量,猪与汇点连边,容量1; 猪圈和猪之间连线取决所取的满意值范围; 二分查找满意值最小差值的范围. #include <iostream> #include <cstring> #incl

POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)

解题报告 http://blog.csdn.net/juncoder/article/details/38340447 题目传送门 题意: B个猪圈,N头猪.每头猪对每一个猪圈有一个惬意值.要求安排这些猪使得最大惬意和最小惬意的猪差值最小 思路: 二分图的多重匹配问题; 猪圈和源点连边,容量为猪圈容量.猪与汇点连边,容量1; 猪圈和猪之间连线取决所取的惬意值范围; 二分查找惬意值最小差值的范围. #include <iostream> #include <cstring> #inc

Steady Cow Assignment---poj3189(多重匹配+二分)

题目链接:http://poj.org/problem?id=3189 题意:有n头牛,B个牛棚,每头牛对牛棚都有一个喜欢度,接下来输入N*B的矩阵第i行第j列的数x表示:第i头牛第j喜欢的是x; 第i个牛棚最多存Max[i]头牛,最后求牛棚的排名区间,意思就是假如一个牛棚中有最喜欢这个牛棚的牛(那么就是第一喜欢1)和也有最不喜欢这个牛棚的牛(那么就是第B喜欢B),答案就是1--B的区间大小B-1+1,问怎么安排能让这个区间值最小,求最小值: 由于B的取值范围较小,所以可以枚举所有的区间,求符合

Optimal Milking(二分图多重匹配+二分)(网络流)

Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2112 Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 20

Jamie&#39;s Contact Groups(二分图多重匹配+二分)(网络流)

Jamie's Contact Groups Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2289 Description Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list

POJ--2289--Jamie&#39;s Contact Groups【二分图多重匹配+二分答案】

链接:http://poj.org/problem?id=2289 题意:有n个人,m个分组,每个人可以分配到一些组别,问如何分能使得人数最多的组别人数最少. 思路:这道题二分+网络流也可以做,我这里是二分图多重匹配的做法.因为一个组别是一对多的关系,所以是多重匹配,我们二分多重匹配的限制,得到最小的限制可使二分图匹配,这个限制就是答案. 网上找的模板 #include<cstring> #include<string> #include<fstream> #inclu

POJ2112:Optimal Milking(Floyd+二分图多重匹配+二分)

Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 20262   Accepted: 7230 Case Time Limit: 1000MS Description: FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) c

hiho 第117周 二分图多重匹配,网络流解决

描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小Ho作为班上的班干部,统计分配比赛选手的重任也自然交到了他们手上. 已知小Hi和小Ho所在的班级一共有N名学生(包含小Hi和小Ho),编号依次为1..N. 运动会一共有M项不同的比赛,编号为1..M.第i项比赛每个班需要派出m[i]名选手参加. 根据小Hi和小Ho的统计,编号为i的学生表示最多同时参加a[i]项比赛,并且给出他所擅长的b[i]项比赛的编号. 小Hi和小Ho希望将每个学生都安排到他所擅长的比赛项目,