POJ 3189 Steady Cow Assignment (枚举+二分图的多重匹配)

Steady Cow Assignment
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 4567Accepted: 1606

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
 
一开始没看懂这句话:one more than the positive difference between the the highest-ranked barn chosen and that lowest-ranked barn chosen...以为要求的是每一头牛都尽可能高兴,就是把他们尽可能往他们喜欢的barn里住,然后求最少要用到哪个barn...直接用二分答案来做,WA了数次。然后网上搜报告,刚瞄到别人写的大致题意就顿时跪了。题意要求的其实是不管牛高不高兴,而是同个barn里的牛的高兴程度要尽可能接近。所以做法就是从高兴差距为1开始枚举到B,每次求匹配,如果能匹配成功取最小值。
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define SIZE 1024

using namespace std;

int N,B,ans,low,high;
int need[SIZE][32];
bool cnt[SIZE][32],vis[32];
int link[32][SIZE],num[32];
int limit[32];

bool dfs(int lt)
{
    for(int r=1; r<=high; r++) 
    {
        if(!vis[need[lt][r]] && cnt[lt][need[lt][r]])
        {
            vis[need[lt][r]] = true;
            if(num[need[lt][r]] < limit[need[lt][r]])
            {
                link[need[lt][r]][++num[need[lt][r]]] = lt;
                return true;
            }
            for(int i=1; i<=limit[need[lt][r]]; i++)
            {
                if(dfs(link[need[lt][r]][i]))
                {
                    link[need[lt][r]][i] = lt;
                    return true;
                }
            }
        }
    }
    return false;
}

bool match()
{
    memset(link,0,sizeof(link));
    memset(num,0,sizeof(num));
    for(int i=1; i<=N; i++)
    {
        memset(vis,0,sizeof(vis));
        if(!dfs(i))
            return false;
    }
    return true;
}

void Search()
{
    low = 1, high = 1; //low和high是喜欢程度,从最喜欢的开始枚举
    while(low <= high && high <= B)
    {
        memset(cnt,0,sizeof(cnt));
        for(int i=1; i<=N; i++)
            for(int j=low; j<=high; j++) //把当前匹配的范围连边
                cnt[i][need[i][j]] = true;
        if(match())
        {
            if(high - low + 1 < ans)
                ans = high - low + 1;
            low ++; //可匹配,将匹配范围缩小,继续看能不能匹配(无视喜欢程度)
        }
        else
            high ++; //不可匹配,扩大当前喜欢的范围
    }
}

int main()
{
    while(~scanf("%d%d",&N,&B))
    {
        for(int i=1; i<=N; i++)
            for(int j=1; j<=B; j++)
                scanf("%d",&need[i][j]);
        for(int i=1; i<=B; i++)
            scanf("%d",&limit[i]);
        ans = 0x7fffffff;
        Search();
        printf("%d\n",ans);
    }
    return 0;
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值