Farmer John's family pitches in with the chores during milking, doing all the chores as quickly as possible. At FJ's house, some chores cannot be started until others have been completed, e.g., it is impossible to wash the cows until they are in the stalls.

Farmer John has a list of N (3 <= N <= 10,000) chores that must be completed. Each chore requires an integer time (1 <= length of time <= 100) to complete and there may be other chores that must be completed before this chore is started. We will call these prerequisite chores. At least one chore has no prerequisite: the very first one, number 1. Farmer John's list of chores is nicely ordered, and chore K (K > 1) can have only chores 1,.K-1 as prerequisites. Write a program that reads a list of chores from 1 to N with associated times and all perquisite chores. Now calculate the shortest time it will take to complete all N chores. Of course, chores that do not depend on each other can be performed simultaneously.

Input

* Line 1: One integer, N

* Lines 2..N+1: N lines, each with several space-separated integers. Line 2 contains chore 1; line 3 contains chore 2, and so on. Each line contains the length of time to complete the chore, the number of the prerequisites, Pi, (0 <= Pi <= 100), and the Pi prerequisites (range 1..N, of course).

Output

A single line with an integer which is the least amount of time required to perform all the chores. 

Sample Input

7
5 0
1 1 1
3 1 2
6 1 1
1 2 2 4
8 2 2 4
4 3 3 5 6

Sample Output

23

Hint

[Here is one task schedule:

Chore 1 starts at time 0, ends at time 5.

Chore 2 starts at time 5, ends at time 6.

Chore 3 starts at time 6, ends at time 9.

Chore 4 starts at time 5, ends at time 11.

Chore 5 starts at time 11, ends at time 12.

Chore 6 starts at time 11, ends at time 19.

Chore 7 starts at time 19, ends at time 23.

]
题解:树形DP入门题。从根节点往下依次更新出每一个节点的最短时间,则该最短时间的最大值即为:完成家务的最短时间。
参考代码为:
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=10005;
int c[maxn],n[maxn],dp[maxn]; int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int N,temp,sum=-maxn;
memset(dp,0,sizeof dp);
cin>>N;
for(int i=1;i<=N;i++)
{
cin>>c[i]>>n[i];
if(i==1) dp[i]=c[i];
else
{
int max=-maxn;
if(n[i]==0) dp[i]=c[i];
else
{
for(int j=0;j<n[i];j++)
{
cin>>temp;
if(dp[temp]>max) max=dp[temp];
}
dp[i]=max+c[i];
}
}
if(dp[i]>sum) sum=dp[i];
}
cout<<sum<<endl;
return 0;
} /*
7
5 0
1 1 1
3 1 2
6 1 1
1 2 2 4
8 2 2 4
4 3 3 5 6
*/

  

POJ 1949 Chores的更多相关文章

  1. POJ 1949 Chores (很难想到的dp)

    传送门: http://poj.org/problem?id=1949 Chores Time Limit: 3000MS   Memory Limit: 30000K Total Submissio ...

  2. poj 1949 Chores 最长路

    题目链接 求出最长路..... #include <iostream> #include <vector> #include <cstdio> #include & ...

  3. POJ 1949 Chores(DAG上的最长路 , DP)

    题意: 给定n项任务, 每项任务的完成用时t和完成每项任务前需要的k项任务, 求把所有任务完成的最短时间,有当前时间多项任务都可完成, 那么可以同时进行. 分析: 这题关键就是每项任务都会有先决条件, ...

  4. POJ 1949 DP?

    题意: 有n个家务,第i个家务需要一定时间来完成,并且第i个任务必须在它 "前面的" 某些任务完成之后才能开始. 给你任务信息,问你最短需要多少时间来完成任务. 输入: 第一行n个 ...

  5. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  6. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  9. poj 动态规划的主题列表和总结

    此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...

随机推荐

  1. cmake 编译安装mysql5.5.32

    1.安装cmake 上传tar包 rz cmake-2.8.8.tar.gz 解压tar包,并进入解压后的文件夹 tar xf cmake-2.8.8.tar.gz cd cmake-2.8.8 编译 ...

  2. ThreadLocal原理分析与代码验证

    ThreadLocal提供了线程安全的数据存储和访问方式,利用不带key的get和set方法,居然能做到线程之间隔离,非常神奇. 比如 ThreadLocal<String> thread ...

  3. H5之外部浏览器唤起微信分享

    最近在做一个手机站,要求点击分享可以直接打开微信分享出去.而不是jiathis,share分享这种的点击出来二维码.在网上看了很多,都说APP能唤起微信,手机网页实现不了.也找了很多都不能直接唤起微信 ...

  4. ffmpeg-3.1.4居然也有这么坑的bug

    近日自己用下载的ffmpeg-3.1.4代码自己编译来用,没想到会碰到这么一下低级坑.我用自己的编译出来的库总是会在用rtsp上传视频时崩掉,起初我还以为自己编译的x264出问题,因为我是绕开使用pk ...

  5. Qt Framework 问题之 framework/Versions/A:bundle format unrecognized, invalid, or unsuitable

    在解决标题提到的问题之后,先来介绍下Qt Framework一些基本知识. 基于QT的Mac端工程,在打包时需要对所有需要嵌入到APP的framework及dylib文件进行手动签名处理. 一.签名处 ...

  6. Centos7編譯安裝LAMP平臺

    什麽是LAMP? 拆開看 L 就是Linux系統 A是Apache的縮寫 M.P則是MySQL和PHP的简写. 其实就是把Apache, MySQL以及PHP安装在Linux系统上,组成一个环境来运行 ...

  7. linux 如何把一个装好的系统做成镜像(文件备份)

    linux 如何把一个装好的系统做成镜像(文件备份)  我来答 浏览 11851 次来自电脑网络类芝麻团 2016-01-19 案例1(命令式操作) 像'ghost'那些备份系统,系统出了问题就恢复 ...

  8. web前端面试题总结(html、css)

    1.对 WEB 标准以及 W3C 的理解与认识? 参考: 标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索几率.使用外 链 css 和 js 脚本. 结构行为表现的分离.文件下载与页面速度更快.内容能 ...

  9. kali linux 修改更新源和更新命令

    1.修改sources.list源文件: vim /etc/apt/sources.list #aliyun 阿里云 deb http://mirrors.aliyun.com/kali kali-r ...

  10. 音视频入门-14-JPEG文件格式详解

    * 音视频入门文章目录 * JPEG 文件格式解析 JPEG 文件使用的数据存储方式有多种.最常用的格式称为 JPEG 文件交换格式(JPEG File Interchange Format,JFIF ...