题目描述

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.

农民约翰有N头奶牛(1<=N<=10,000),编号为1...N。每一头奶牛需要T(i)单位的时间来挤奶。不幸的是,由于FJ的仓库布局,一些奶牛要在别的牛之前挤奶。比如说,如果奶牛A必须在奶牛B前挤奶,FJ就需要在给奶牛B挤奶前结束给奶牛A的挤奶。

为了尽量完成挤奶任务,FJ聘请了一大批雇工协助任务——同一时刻足够去给任意数量的奶牛挤奶。然而,尽管奶牛可以同时挤奶,但仍需要满足以上的挤奶先后顺序。请帮助FJ计算挤奶过程中的最小总时间。

输入输出格式

输入格式:

* 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.

输入输出样例

输入样例#1:

3 1
10
5
6
3 2
输出样例#1:

11

说明

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.

Solution:

  解释一手题意:本题就是一棵树(或者森林)中,从每个rd为0的点来走一条路径(需要时间),输出最长的时间。

  本题描述中有一句话很重要(直接得出算法):"若b在a前面,则b必须先挤奶,再去给a挤奶"。

  于是就有了上面的一句话题意,一个根节点要在它的多个儿子节点前被访问,然后找出它到各儿子节点的最长的时间,就是访问该树的最少时间。(画个图自己理解吧,语文太差,描述不好)

  于是想到拓扑排序。于是直接统计入度,在拓扑排序时加两条语句维护每条路径的最长时间就OK了。

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
using namespace std;
const int N=,M=;
int n,m,ans,t[N],a[N],cost[N],cnt,to[M],net[M],h[N],rd[N];
il void add(int x,int y)
{
to[++cnt]=y,net[cnt]=h[x],h[x]=cnt,rd[y]++;
}
il int gi()
{
int a=;char x=getchar();bool f=;
while((x<''||x>'')&&x!='-')x=getchar();
if(x=='-')x=getchar(),f=;
while(x>=''&&x<='')a=a*+x-,x=getchar();
return f?-a:a;
}
il void topsort()
{
queue<int>q;
for(int i=;i<=n;i++)
if(!rd[i])q.push(i);
while(!q.empty()){
int x=q.front();q.pop();
cost[x]=a[x]+t[x];
ans=max(ans,cost[x]);
for(int i=h[x];i;i=net[i]){
rd[to[i]]--;
a[to[i]]=max(a[to[i]],cost[x]);
if(!rd[to[i]])q.push(to[i]);
}
}
}
int main()
{
n=gi(),m=gi();
//cout<<n<<m<<endl;
for(int i=;i<=n;i++)t[i]=gi();
int u,v;
while(m--){
u=gi(),v=gi();
add(v,u);
}
topsort();
cout<<ans;
return ;
}

P3074 [USACO13FEB]牛奶调度Milk Scheduling的更多相关文章

  1. 洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling

    题目描述 Farmer John has N cows that need to be milked (1 <= N <= 10,000), each of which takes onl ...

  2. [USACO13DEC]牛奶调度Milk Scheduling

    原题链接https://www.lydsy.com/JudgeOnline/problem.php?id=4096 容易想到的一个测略就是,优先考虑结束时间小的牛.所以我们对所有牛按照结束时间排序.然 ...

  3. [USACO09OPEN] 工作调度Work Scheduling (贪心/堆)

    [USACO09OPEN] 工作调度Work Scheduling 题意翻译 约翰有太多的工作要做.为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. 他的工作日从0时刻开始,有10^ ...

  4. P1208 [USACO1.3]混合牛奶 Mixing Milk

    P1208 [USACO1.3]混合牛奶 Mixing Milk 题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业 ...

  5. [洛谷P2852] [USACO06DEC]牛奶模式Milk Patterns

    洛谷题目链接:[USACO06DEC]牛奶模式Milk Patterns 题目描述 Farmer John has noticed that the quality of milk given by ...

  6. 洛谷——P1208 [USACO1.3]混合牛奶 Mixing Milk

    P1208 [USACO1.3]混合牛奶 Mixing Milk 题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业 ...

  7. 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling

    P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...

  8. 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk

    P1208 [USACO1.3]混合牛奶 Mixing Milk 题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业 ...

  9. 题解 P2949 【[USACO09OPEN]工作调度Work Scheduling】

    P2949 [USACO09OPEN]工作调度Work Scheduling 题目标签是单调队列+dp,萌新太弱不会 明显的一道贪心题,考虑排序先做截止时间早的,但我们发现后面可能会出现价值更高却没有 ...

随机推荐

  1. 成都Uber优步司机奖励政策(4月8日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. [hdu 6184 Counting Stars(三元环计数)

    hdu 6184 Counting Stars(三元环计数) 题意: 给一张n个点m条边的无向图,问有多少个\(A-structure\) 其中\(A-structure\)满足\(V=(A,B,C, ...

  3. Android AOSP 单独编译某一模块

    由于AOSP 项目太大,我只修改了一个模块,比如设置. 那么只需要单独编译设置这个模块就可以了. 首先执行Source: source build/envsetup.sh 执行之后,就会有一些额外的命 ...

  4. 60帧的丝般顺畅 - QQ飞车手游优化点滴

    WeTest 导读 加入项目组的这段时间主要是承担性能优化这块的工作,同时也会去实现一些场景材质.特效材质以及工具.今天就性能优化这块分享一下个人的经验. 设备等级划分 设备等级划分是一切优化,LOD ...

  5. Selenium自动化测试第一天(上)

    如有任何学习问题,可以添加作者微信:lockingfree 目录 Selenium自动化测试基础 Selenium自动化测试第一天(上) Selenium自动化测试第一天(下) Selenium自动化 ...

  6. TPO-14 C2 Prepare for a career in journalism

    TPO-14 C2 Prepare for a career in journalism 第 1 段 1.Listen to a conversation between a student and ...

  7. (python)leetcode刷题笔记 02 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  8. 深入理解 Vuejs 组件

    本文主要归纳在 Vuejs 学习过程中对于 Vuejs 组件的各个相关要点.由于本人水平有限,如文中出现错误请多多包涵并指正,感谢.如果需要看更清晰的代码高亮,请跳转至我的个人站点的 深入理解 Vue ...

  9. LeetCode 108——将有序数组转化为二叉搜索树

    1. 题目 2. 解答 一棵高度平衡的二叉搜索树意味着根节点的左右子树包含相同数量的节点,也就是根节点为有序数组的中值. 因此,我们将数组的中值作为根节点,然后再递归分别得到左半部分数据转化的左子树和 ...

  10. 关于C#中如何使用wmi获得操作系统信息?

    最近项目中用到了windows server 2012操作系统中的存储池和ISCSI Disk的技术.前期,我们整个操作都是用power shell脚本去实现了.带来了不方便,后期要使用wmi API ...