Monkeys in the Emei Mountain -- UVA - 11167 (网络流)

本文介绍了一种通过离散化时间和构建二分图的方法解决猴子饮水问题的技术方案。该方案利用最大流算法判断是否所有猴子能在限制条件下饮足所需水量,并提供一种复杂但有效的输出方案方法。

Monkeys in the Emei Mountain 
Input: Standard Input

Output: Standard Output

Xuexue is a pretty monkey living in the Emeimountain. She is extremely thirsty during time 2 and time 9 everyday, so she must drink 2 units water during this period. She may drink water more than once, as long as the total amount of water she drinks is exactly 2 - she never drinks more than she needs.  Xuexuedrinks 1 unit water in one time unit, so she may drinks from time 2 to time 4, or from 3 to 5, … , or from 7 to 9, or even drinks twice: first from 2 to 3, then from 8 to 9. But she can't drink from 1 to 3 since she's not thirsty at time 1, and she can't drink from 8 to 10, since she must finish at time 9.

 

There are many monkeys like Xuexue: we use a triple (vab) to describe a monkey who is thirsty during time a andb, and must drink exactly v units of water during that period. Every monkey drinks at the same speed (i.e. one unit water per unit time).

 

Unfortunately, people keep on doing something bad on the environment in Emei Mountain . Eventually, there are only one unpolluted places for monkeys to drink. Further more, the place is so small that at most m monkeys can drink water together. Monkeys like to help each other, so they want to find a way to satisfy all the monkeys' need. Could you help them?

 

Input

The input consists of several test cases. Each case contains two integers and (1 ££ 100, 1 ££ 5), followed by n lines of three integer numbers (va,b), where 0 £v, a, b £ 50,000, a < b, v £b - a. The last test case is followed by a single zero, which should not be processed. There are at most 50 test cases.

 

Output

For each test case, print the case number and whether there is a solution. If there is a solution, the following n lines describe the solution: one monkey on a separate line. The first number k means the monkey drinks water for k times. The following k pairs (aibi) means the monkey drinks from ai to b(aibi). The pairs should be sorted in ascending order, and ai should not be equal to ai+1 for 1£i£k-1 (otherwise these two drinking periods could be combined). If more than one solution exists, any one is acceptable. Note that there should be exactly one space between k and pairs (ai,bi), but no space within each pair.

Sample Input                               Output for Sample Input

3 1

2 2 9

2 3 5

3 5 8

2 1

4 5 9

4 8 12

5 2

2 1 3

2 3 5

2 5 7

2 1 7

4 2 6

0

Case 1: Yes

2 (2,3) (8,9)

1 (3,5)

1 (5,8)

Case 2: No

Case 3: Yes

1 (1,3)

1 (3,5)

1 (5,7)

2 (1,2) (6,7)

1 (2,6)


题目大意:

有N个猴子要在a-b的时间区间喝够v升的水。每只猴子喝水的速度都是一小时一升水。可以不需要连续喝水。但是必须以一小时为最小单位。

现在有一个地方 可以同时供m个猴子去喝水。问最终能否有喝水的方案,如果有输出方案。

思路:

由于时间跨度很大,但是N很小。所以可以将时间离散化。离散为不超过2*N个区间。

然后建立二分图,X部为猴子,源点向其连一条容量为a[i].v的边。Y部为每个时间区间。因为每个小时最多可以有m个猴子喝水 所以连接到汇点,容量为m*(区间长度)

然后判断每一个猴子的喝水区间,如果包含离散化出的时间区间。表示这只猴子可以在这个时间区间喝水。连边,容量为区间长度。

最大流的结果如果使得X部满流,说明每只猴子都可以喝到水。输出方案,否则无解。


输出方案:

我觉得这道题最麻烦的就是输出方案的说。

由于一个区间可能由多个猴子同时分享,使得简单的判断反向边的残量的做法失效。

用now数组表示每一个时间区间,如果某个区间消耗掉了f个时间,那么now[i]+= f;当now+f大于了这个区间的终点,说明已经是后面的猴子来喝水了,那么循环重置一下就好。这部分具体看代码。

#include <iostream>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define maxn 1111
#define maxm 1000000
#define INF 0x3f3f3f3f
using namespace std;


struct Edge
{
    int from,to,next,f;
}es[maxm];
struct Node
{
    int v,a,b;
}a[1111];

int b[1111];
int len,sum,ks;
int cnt,p[maxn];
int n,m,s,t,ans_f;
int dis[maxn],cur[maxn];


void add(int from,int to,int f)
{
    es[cnt].from = from;
    es[cnt].to = to;
    es[cnt].f = f;
    es[cnt].next = p[from];
    p[from] = cnt++;

    es[cnt].from = to;
    es[cnt].to = from;
    es[cnt].f = 0;
    es[cnt].next = p[to];
    p[to] = cnt++;
}

bool bfs()
{
    memset(dis,0,sizeof dis);
    dis[s] = 1;
    queue <int > q;
    q.push(s);
    while(!q.empty())
    {
        int u = q.front();
        if(u == t) return true;
        q.pop();
        for(int i = p[u];~i;i = es[i].next)
        {
            int v = es[i].to;
            if(dis[v] || !es[i].f) continue;
            dis[v] = dis[u]+1;
            q.push(v);
        }
    }
    return false;
}


int dfs(int x,int a)
{
    if(x == t) return a;
    int flow = 0;
    for(;cur[x]+1;cur[x] = es[cur[x]].next)
    {
        Edge &e = es[cur[x]];
        if(e.f && dis[e.to] == dis[x]+1)
        {
            int f = dfs(e.to,min(a-flow,e.f));
            flow += f;
            e.f -= f;
            es[cur[x]^1].f += f;
            if(flow == a) return flow;
        }
    }
    return flow;
}

void init()
{
    ks++;
    len = 0;
    sum = 0;
    memset(p,-1,sizeof p);
    memset(a,0,sizeof a);
    memset(b,0,sizeof b);
    cnt =0;
}

void Maxflow(int num)
{
    ans_f = 0;
    while(bfs())
    {
        for(int i = 0;i <= num;i++) cur[i] = p[i];
        ans_f += dfs(s,INF);
    }

}
int now[1010];
int ans[1010];

void put()
{
    int len = 0;
    for(int u = 1; u <= n; ++u)
    {
        len = 0;
        for(int i = p[u]; ~i; i = es[i].next)
        {
            int v = es[i].to, f = es[i^1].f;
            if(f)
            {
                ans[len++] = now[v - n];
                if(now[v-n] + f > b[v-n])
                {
                    ans[len++] = b[v-n];
                    ans[len++] = b[v-1-n];
                    ans[len++] = b[v-1-n] + f - (b[v-n] - now[v-n]);
                    now[v-n] = ans[len-1];
                }
                else now[v-n] += c, ans[len++] = now[v-n];
                if(now[v-n] == b[v-n]) now[v-n] = b[v-1-n];
            }
        }
        int _len = 0;
        for(int i = 1; i < len; ++i)
        {
            if(ans[i] == ans[_len])
                ans[_len] = ans[i+1], ++i;
            else
                ans[++_len] = ans[i];
        }
        len = _len + 1;
        printf("%d", len / 2);
        for(int i = 0; i < len; i += 2)
            printf(" (%d,%d)", ans[i], ans[i+1]);
        printf("\n");
    }
}



int main()
{
    while(scanf("%d",&n),n)
    {
        scanf("%d",&m);
        init();
        for(int i = 1;i <= n;i++)
        {
            scanf("%d %d %d",&a[i].v,&a[i].a,&a[i].b);
            b[len++] = a[i].a;
            b[len++] = a[i].b;
            sum += a[i].v;
        }
        sort(b,b+len);
        len = unique(b,b+len)-b;
        s = 0,t = n+len+1;
        for(int i = 1;i <= n;i++) add(s,i,a[i].v);
        for(int i = 1;i < len;i++) add(n+i,t,m*(b[i]-b[i-1]));
        for(int i = 1;i < len;i++) now[i] = b[i-1];
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j < len;j++)
            {
                if(a[i].a <= b[j-1] && a[i].b >= b[j])
                    add(i,n+j,b[j]-b[j-1]);
            }
        }
        Maxflow(t);

        if(sum == ans_f)
        {
            printf("Case %d: Yes\n",ks);
            put();
        }
        else
        {
            printf("Case %d: No\n",ks);
        }
    }
    return 0;
}



基于遗传算法的新的异构分布式系统任务调度算法研究(Matlab代码实现)内容概要:本文档围绕基于遗传算法的异构分布式系统任务调度算法展开研究,重点介绍了一种结合遗传算法的新颖优化方法,并通过Matlab代码实现验证其在复杂调度问题中的有效性。文中还涵盖了多种智能优化算法在生产调度、经济调度、车间调度、无人机路径规划、微电网优化等领域的应用案例,展示了从理论建模到仿真实现的完整流程。此外,文档系统梳理了智能优化、机器学习、路径规划、电力系统管理等多个科研方向的技术体系与实际应用场景,强调“借力”工具与创新思维在科研中的重要性。; 适合人群:具备一定Matlab编程基础,从事智能优化、自动化、电力系统、控制工程等相关领域研究的研究生及科研人员,尤其适合正在开展调度优化、路径规划或算法改进类课题的研究者; 使用场景及目标:①学习遗传算法及其他智能优化算法(如粒子群、蜣螂优化、NSGA等)在任务调度中的设计与实现;②掌握Matlab/Simulink在科研仿真中的综合应用;③获取多领域(如微电网、无人机、车间调度)的算法复现与创新思路; 阅读建议:建议按目录顺序系统浏览,重点关注算法原理与代码实现的对应关系,结合提供的网盘资源下载完整代码进行调试与复现,同时注重从已有案例中提炼可迁移的科研方法与创新路径。
【微电网】【创新点】基于非支配排序的蜣螂优化算法NSDBO求解微电网多目标优化调度研究(Matlab代码实现)内容概要:本文提出了一种基于非支配排序的蜣螂优化算法(NSDBO),用于求解微电网多目标优化调度问题。该方法结合非支配排序机制,提升了传统蜣螂优化算法在处理多目标问题时的收敛性和分布性,有效解决了微电网调度中经济成本、碳排放、能源利用率等多个相互冲突目标的优化难题。研究构建了包含风、光、储能等多种分布式能源的微电网模型,并通过Matlab代码实现算法仿真,验证了NSDBO在寻找帕累托最优解集方面的优越性能,相较于其他多目标优化算法表现出更强的搜索能力和稳定性。; 适合人群:具备一定电力系统或优化算法基础,从事新能源、微电网、智能优化等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于微电网能量管理系统的多目标优化调度设计;②作为新型智能优化算法的研究与改进基础,用于解决复杂的多目标工程优化问题;③帮助理解非支配排序机制在进化算法中的集成方法及其在实际系统中的仿真实现。; 阅读建议:建议读者结合Matlab代码深入理解算法实现细节,重点关注非支配排序、拥挤度计算和蜣螂行为模拟的结合方式,并可通过替换目标函数或系统参数进行扩展实验,以掌握算法的适应性与调参技巧。
本项目是一个以经典51系列单片机——STC89C52为核心,设计实现的一款高性价比数字频率计。它集成了信号输入处理、频率测量及直观显示的功能,专为电子爱好者、学生及工程师设计,旨在提供一种简单高效的频率测量解决方案。 系统组成 核心控制器:STC89C52单片机,负责整体的运算和控制。 信号输入:兼容多种波形(如正弦波、三角波、方波)的输入接口。 整形电路:采用74HC14施密特触发器,确保输入信号的稳定性和精确性。 分频电路:利用74HC390双十进制计数器/分频器,帮助进行频率的准确测量。 显示模块:LCD1602液晶显示屏,清晰展示当前测量的频率值(单位:Hz)。 电源:支持标准电源输入,保证系统的稳定运行。 功能特点 宽频率测量范围:1Hz至12MHz,覆盖了从低频到高频的广泛需求。 高灵敏度:能够识别并测量幅度小至1Vpp的信号,适合各类微弱信号的频率测试。 直观显示:通过LCD1602液晶屏实时显示频率值,最多显示8位数字,便于读取。 扩展性设计:基础版本提供了丰富的可能性,用户可根据需要添加更多功能,如数据记录、报警提示等。 资源包含 原理图:详细的电路连接示意图,帮助快速理解系统架构。 PCB设计文件:用于制作电路板。 单片机程序源码:用C语言编写,适用于Keil等开发环境。 使用说明:指导如何搭建系统,以及基本的操作方法。 设计报告:分析设计思路,性能评估和技术细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值