Poj 3189 Steady Cow Assignment【二分+多重匹配】

原创 2016年08月28日 16:27:46

Steady Cow Assignment

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 6589

 

Accepted: 2260

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.

Source

USACO 2006 February Gold

 

题目大意:有n头牛,b个牛棚,接下来一个n*b的一个矩阵,Aij表示第i头牛第j个喜欢的牛棚编号。为了公平起见,尽可能的使得每头牛的喜欢程度相近,使得最终全部牛都有了牛棚之后,使得最高兴的牛(比如是选择了第j个喜欢的牛棚)-最不高兴的牛(比如是选择了第y个喜欢的牛棚)+1尽可能的小(j-y+1)。


思路:


1、因为b是一个比较小的数据范围,那么我们其实可以直接两层for枚举左右区间,确定没头牛可以选择的第几喜欢的牛棚需要在l,r之间。然后进行多重匹配判定,如果所有牛都能匹配到牛棚,那么当前区间就是一个可行区间。维护记录最小值即可。


2、这里可以有一个二分查找的优化,没加优化是260+ms,加了优化之后大约60+ms。

因为随着区间的增大,可以匹配上的牛的数量会随着增加,那么其满足递增性,那么我们一层for枚举左区间,然后二分查找一个右区间,然后进行多重匹配的判定即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int vis[1500];
int a[1500][50];
int match[1500][50];
int num[1500];
int c[1500];
int n,m;
int find(int u,int l,int r)
{
    for(int i=l;i<=r;i++)
    {
        int v=a[u][i];
        if(vis[v]==1)continue;
        vis[v]=1;
        if(num[v]<c[v])
        {
            match[v][num[v]]=u;
            num[v]++;
            return 1;
        }
        else
        {
            for(int j=0;j<c[v];j++)
            {
                if(find(match[v][j],l,r)==1)
                {
                    match[v][j]=u;
                    return 1;
                }
            }
        }
    }
    return 0;
}
int Slove(int l,int r)
{
    int flag=0;
    memset(match,-1,sizeof(match));
    memset(num,0,sizeof(num));
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        if(find(i,l,r)==1)continue;
        else
        {
            flag=1;break;
        }
    }
    if(flag==1)return 0;
    else return 1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&c[i]);
        }
        int ans=0x3f3f3f3f;
        for(int i=1;i<=m;i++)
        {
            int l=i;
            int r=m;
            int mid;
            while(r-l>=0)
            {
                int mid=(l+r)/2;
                if(Slove(i,mid)==1)
                {
                    r=mid-1;
                    ans=min(ans,(mid-i+1));
                }
                else
                {
                    l=mid+1;
                }
            }
        }
        printf("%d\n",ans);
    }
}




版权声明:0.0小白有写的不好不对的地方请指出~

相关文章推荐

poj3189Steady Cow Assignment【二分图多重匹配】

Description Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns wh...

poj 3189 Steady Cow Assignment(多重匹配or二分最大流)

Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5033   Acce...

POJ 3189--Steady Cow Assignment【二分图多重匹配 && 最大流求解 && 枚举 && 经典】

Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6023   Acce...

poj 3189 Steady Cow Assignment 二分图多重匹配

题意: 有ndou

poj3189 Steady Cow Assignment(枚举+多重匹配)

有n头牛和b个棚,每个棚有容量上限。每头牛对每个棚有个喜欢程度,现在给牛分配住棚且棚中牛的数目不能超过该棚的上限。 先输入n,b。然后n行,每行有b个数字x[j],表示该头牛对棚子x[j]的喜欢程...

POJ 3189 Steady Cow Assignment(网络流之最大流+二分构图)

题目地址:POJ 3189 我晕啊。。。飞快的把白天的任务完成又有什么用。。。节省下来的时间活生生的被我的手残给全浪费掉了。。。又调了一整天,问题居然是一个地方的n和m写反了!!!反思。。反思。。。...

【最大流+dinic+二分枚举】北大 poj 3189 Steady Cow Assignment

/* THE PROGRAM IS MADE BY PYY */ /*-----------------------------------------------------------------...

poj 3189 Steady Cow Assignment(二分+最大流)

题意:N头牛(1000),B个农场(20),每个农场可以容纳一定数量的牛。 每头牛对每个农场都有一个排名(排名从1~B)。每头牛都会在B个农场中的某一个,这头牛的高兴程度是它对这个农场的排名。为...

【POJ】3189 Steady Cow Assignment 二分最大流

传送门:【POJ】Steady Cow Assignment

POJ3189 Steady Cow Assignment 二分枚举+dinic最大流

但是不幸的是,一直超时。查不出来是不是我哪写错了。还是难道非得sap? #include using namespace std; #define MIN(a,b) (ab?a:b) const i...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)