POJ 1949 Chores (很难想到的dp)
传送门:
http://poj.org/problem?id=1949
| Time Limit: 3000MS | Memory Limit: 30000K | |
| Total Submissions: 6368 | Accepted: 3013 |
Description
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
* 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
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.
]
Source
#include <iostream>
#include <stdio.h>
#include<memory>
#include<stack>
#include<string.h>
#include<algorithm>
using namespace std;
#define max_v 11000
int dp[max_v];//表示执行到第i个任务所需要的最小时间
//每次DP[i]应该等于完成之前优先任务的最大时间
int main()
{
int n,x,y,k,ans=;
dp[]=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d %d",&x,&k);
if(k>)
{
for(int j=;j<=k;j++)
{
scanf("%d",&y);
dp[i]=max(dp[i],dp[y]+x);
}
}else
{
dp[i]=x;
}
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
return ;
}
POJ 1949 Chores (很难想到的dp)的更多相关文章
- POJ 1949 Chores(DAG上的最长路 , DP)
题意: 给定n项任务, 每项任务的完成用时t和完成每项任务前需要的k项任务, 求把所有任务完成的最短时间,有当前时间多项任务都可完成, 那么可以同时进行. 分析: 这题关键就是每项任务都会有先决条件, ...
- POJ 1949 Chores
Farmer John's family pitches in with the chores during milking, doing all the chores as quickly as p ...
- poj 1949 Chores 最长路
题目链接 求出最长路..... #include <iostream> #include <vector> #include <cstdio> #include & ...
- HDU 3038 How Many Answers Are Wrong(带权并查集,真的很难想到是个并查集!!!)
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- poj 2411 Mondriaan's Dream(状态压缩dP)
题目:http://poj.org/problem?id=2411 Input The input contains several test cases. Each test case is mad ...
- POJ 3254 Corn Fields(状态压缩DP)
题目大意:给出一个M*N的矩阵,元素为0表示这个地方不能种玉米,为1表示这个地方能种玉米,现在规定所种的玉米不能相邻,即每行或者没列不能有相邻的玉米,问一共有多少种种植方法. 举个例子: 2 3 1 ...
- 函数式编程很难,这正是你要学习它的原因 | 外刊IT评论网
函数式编程很难,这正是你要学习它的原因 | 外刊IT评论网 函数式编程很难,这正是你要学习它的原因 156 次分享 新浪微博 腾讯微博 Tweet 人人网 QQ空间 很奇怪不是,很少有人每天都使用函数 ...
- 0302IT行业虽吃香,能完全享受这块“香"的也很难
面对现今严峻的就业形势,越来越多的人希望通过职业技能培训或者学历提升来提高自己的综合技能以便能够顺利地应聘到自己理想中的工作. 在2014年十大最热门行业和职业排行榜中IT行业最吃香.在十大行业里,I ...
- POJ P1185 炮兵阵地 【状压dp】
炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29502 Accepted: 11424 Description 司令 ...
随机推荐
- Binder or AIDL的最简单实践
1.前言: 在Android开发中多进程的合理使用+进程间通信的IPC是一个比较难的点.特别是Android特有的Binder机制,非常复杂,对于应用层开发的初级开发工程师强求深入理解Binder机制 ...
- Effective C++ .10,11 operator=的约定与注意
1. 返回一个reference to *this 返回一个指向自身的引用符合惯例,可以进行如(a=c).modify()类似的操作,即可以形成链式操作,否则修改的只是一个临时对象.这个和Java中常 ...
- div中嵌套div中使用margin-top失效问题
div中嵌套div中使用margin-top失效问题
- python中循环删除list和dict类型注意事项
列表和字典在循环操作(增删)时,其长度会改变 # 删除 li = [11, 22, 33, 44, 'rock']中索引为单数的元素 # 方法一 del li[1::2] print(li) # [1 ...
- select下拉框选择字体大小
效果: 结合Bootstrap.jQuery和ES6字符串模板与箭头函数使用JavaScript DOM操作动态添加option,随着option:selected选中的字号而改变相应的字体大小 代码 ...
- 无法远程访问Mysql的解决方案
现在在很多的互联网公司对于mysql数据库的使用已经是不可阻挡的趋势了,所以经常我们在项目开始的时候就会做的事情就是找一台Linux服务器,到上面去安装个mysql,然后在开始我们的数据表的导入工作, ...
- FCKeditor文本编辑器的使用方法
FCKeditor是一个功能强大支持所见即所得功能的文本编辑器,可以为用户提供微软office软件一样的在线文档编辑服务. 它不需要安装任何形式的客户端,兼容绝大多数主流浏览器,支持ASP.Net.A ...
- VC6.0开发OCX按钮控件
原文:http://www.cnblogs.com/joinclear/archive/2013/05/21/3091934.html 0前言 1.OCX是典型的ActiveX控件,常见的OCX控件有 ...
- MySQL数据库、表的字符编码
用MySQL命令行新建数据库和表时默认的字符编码是latin1,但是在实际开发过程中一般都是使用utf8格式的编码.操作如下: 1.修改数据库字符编码 mysql> alter database ...
- 【Machine Learning】监督学习、非监督学习及强化学习对比
Supervised Learning Unsupervised Learning Reinforced Learning Goal: How to apply these methods How t ...