[USACO13FEB]Milk Scheduling S

本文介绍了一种算法来帮助农夫John快速完成挤奶任务,考虑了牛之间的先后顺序约束,通过关系矩阵计算最小总时间。涉及到数据结构、图论和动态规划技巧,探讨了递归优化和内存使用策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

Farmer John's N cows (1 <= N <= 10,000) are conveniently numbered 1..N. Each cow i takes T(i) units of time to milk. Unfortunately, some cows must be milked before others, owing to the layout of FJ's barn. If cow A must be milked before cow B, then FJ needs to completely finish milking A before he can start milking B.

In order to milk his cows as quickly as possible, FJ has hired a large number of farmhands to help with the task -- enough to milk any number of cows at the same time. However, even though cows can be milked at the same time, there is a limit to how quickly the entire process can proceed due to the constraints requiring certain cows to be milked before others. Please help FJ compute the minimum total time the milking process must take.

输入格式

* Line 1: Two space-separated integers: N (the number of cows) and M (the number of milking constraints; 1 <= M <= 50,000).

* Lines 2..1+N: Line i+1 contains the value of T(i) (1 <= T(i) <= 100,000).

* Lines 2+N..1+N+M: Each line contains two space-separated integers A and B, indicating that cow A must be fully milked before one can start milking cow B. These constraints will never form a cycle, so a solution is always possible.

输出格式

* Line 1: The minimum amount of time required to milk all cows.

说明

There are 3 cows. The time required to milk each cow is 10, 5, and 6, respectively. Cow 3 must be fully milked before we can start milking cow 2.

Cows 1 and 3 can initially be milked at the same time. When cow 3 is finished with milking, cow 2 can then begin. All cows are finished milking after 11 units of time have elapsed.

Try_1

#include <bits/stdc++.h>
using namespace std;
int number_cows[10001], number_constraints[50000];
int M, N;
int matrix[10001][10001] = {0};
void compute(int &t, int num)
{
    int i = 1;
    for (; i <= N; i++)
    {
        if (!matrix[num][i])
            continue;
        t += number_cows[i];
        break;
    }
    if (i != N + 1)
        compute(t, i);
}
int main()
{
    cin >> N >> M;
    for (int i = 1; i <= N; i++)
        cin >> number_cows[i];
    for (int i = 0; i < M; i++)
    {
        int a, b;
        cin >> a >> b;
        matrix[a][b] = 1;
    }
    int max = 0;
    for (int i = 1; i <= N; i++)
    {
        int time = number_cows[i];
        compute(time, i);
        if (time > max)
            max = time;
    }
    cout << max;
    return 0;
}

测试结果

笔记本没电了,今天肯定是写不出来了,等啥时候想写了再来,防止自己忘记,决定记录一下当下的想法。

每头牛挤奶所需时间用一维数组表示,索引下标是牛的编号。

在读入constraint时,借用关系矩阵的思想来表达谁前谁后。

读取完所有数据后,遍历每一头牛,并且设置一个整型用来记录最长时间。

关系矩阵大致是用在图结构中,可以利用关系矩阵来求图的路径问题(从头到尾),具体是啥算法,名字忘记了。每求出一个路径长度就和max作比较,然后把max赋值成大的那个。一直到最后一头牛被遍历完。

WA不知道哪错了,估计是下限没考虑清楚,下次试试。

MLE是内存超了,因为在程序里面用了递归,我寻思着是递归占内存超了,因为上学期学数据结构的时候学到过,虽然递归好理解,但是容易超内存,所以可以把递归改成循环试试。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗马尼亚硬拉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值