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

  

最新文章

  1. qq加好友加群限制ip怎么解决
  2. 用sql查询当天,一周,一个月的数据
  3. JavaScript变量的作用域和函数的作用域的区别
  4. php数组的创建及操作
  5. &lt;Araxis Merge&gt;保存文件
  6. Educational Codeforces Round 10 D. Nested Segments (树状数组)
  7. linux下使用mutt发送带附件的邮件
  8. Spring--AOP--面向切面编程
  9. list后台转化为JSON的方法ajax
  10. 新概念英语(1-115)Knock! Knock!
  11. Entity Framework 之存储过程篇
  12. 【Java每日一题】20170216
  13. 不同数据库下的web.config中数据库连接字符串
  14. 20155208徐子涵Vim编辑器学习经验
  15. 使用JQuery反向选择checkbox
  16. HDU 1234 (浙大计算机研究生复试上机考试-2005年) 开门人和关门人 (水)
  17. Django的基本开发环境配置和MTV模型
  18. set的一些数学运算
  19. python并发编程之threading线程(一)
  20. String中的equals方法解析 jdk1.7

热门文章

  1. map的线程安全问题
  2. 十、GAP
  3. 50.Qt-QJsonDocument读写json
  4. mysql--时区表问题(Windows环境下)
  5. Acquistion Location Confidence for accurate object detection
  6. EasyCode实现数据库到Swagger全自动化
  7. Linux01机和Linux02机的切换 和秘钥的配置
  8. Linux job control
  9. LESSON 2-Discrete Source Encoding
  10. Zookeeper 应用实现-配置中心