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

本文探讨了SteadyCow问题,目标是最小化牛群分配到不同畜棚后的满意度波动范围,通过调整分配策略来确保各畜棚内的满意度尽可能接近。采用二分查找与匹配算法解决此问题。
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;
}

【完美复现】面向配电网韧性提升的移动储能预布局与动态调度策略【IEEE33节点】(Matlab代码实现)内容概要:本文介绍了基于IEEE33节点的配电网韧性提升方法,重点研究了移动储能系统的预布局与动态调度策略。通过Matlab代码实现,提出了一种结合预配置和动态调度的两阶段优化模型,旨在应对电网故障或极端事件时快速恢复供电能力。文中采用了多种智能优化算法(如PSO、MPSO、TACPSO、SOA、GA等)进行对比分析,验证所提策略的有效性和优越性。研究不仅关注移动储能单元的初始部署位置,还深入探讨其在故障发生后的动态路径规划与电力支援过程,从而全面提升配电网的韧性水平。; 适合人群:具备电力系统基础知识和Matlab编程能力的研究生、科研人员及从事智能电网、能源系统优化等相关领域的工程技术人员。; 使用场景及目标:①用于科研复现,特别是IEEE顶刊或SCI一区论文中关于配电网韧性、应急电源调度的研究;②支撑电力系统在灾害或故障条件下的恢复力优化设计,提升实际电网应对突发事件的能力;③为移动储能系统在智能配电网中的应用提供理论依据和技术支持。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析,重点关注目标函数建模、约束条件设置以及智能算法的实现细节。同时推荐参考文中提及的MPS预配置与动态调度上下两部分,系统掌握完整的技术路线,并可通过替换不同算法或测试系统进一步拓展研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值