传送门:

http://poj.org/problem?id=1949

Chores
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 6368   Accepted: 3013

Description

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.

]

Source

 
题意是,有很多项工作,每个工作有完成所需的时间,而每一个工作可能会有一些前提工作,前提工作没完成就不能做这项工作。可以有多个工作同时进行。
 
分析:
完成一项工作必然需要完成其所有的前提工作,因为工作是可以同时进行的,所以我可以在完成前提工作中最晚完成的那项之后,马上开始这项工作
意思是在完成第K个任务时只有前面1~K-1个任务能够成为它都优先任务,也就是说题目给定的序列已经拓扑排序完毕,则只需要从1一直DP到n就可以了,注
意这里求的是最小时间,每次DP[i]应该等于完成之前优先任务的最大时间
 
注意不能cin输入,会超时
 
code:
 
#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)的更多相关文章

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

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

  2. POJ 1949 Chores

    Farmer John's family pitches in with the chores during milking, doing all the chores as quickly as p ...

  3. poj 1949 Chores 最长路

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

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

  5. poj 2411 Mondriaan's Dream(状态压缩dP)

    题目:http://poj.org/problem?id=2411 Input The input contains several test cases. Each test case is mad ...

  6. POJ 3254 Corn Fields(状态压缩DP)

    题目大意:给出一个M*N的矩阵,元素为0表示这个地方不能种玉米,为1表示这个地方能种玉米,现在规定所种的玉米不能相邻,即每行或者没列不能有相邻的玉米,问一共有多少种种植方法. 举个例子: 2 3 1 ...

  7. 函数式编程很难,这正是你要学习它的原因 | 外刊IT评论网

    函数式编程很难,这正是你要学习它的原因 | 外刊IT评论网 函数式编程很难,这正是你要学习它的原因 156 次分享 新浪微博 腾讯微博 Tweet 人人网 QQ空间 很奇怪不是,很少有人每天都使用函数 ...

  8. 0302IT行业虽吃香,能完全享受这块“香"的也很难

    面对现今严峻的就业形势,越来越多的人希望通过职业技能培训或者学历提升来提高自己的综合技能以便能够顺利地应聘到自己理想中的工作. 在2014年十大最热门行业和职业排行榜中IT行业最吃香.在十大行业里,I ...

  9. POJ P1185 炮兵阵地 【状压dp】

    炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29502 Accepted: 11424 Description 司令 ...

随机推荐

  1. docker 安装ElasticSearch的中文分词器IK

    首先确保ElasticSearch镜像已经启动 安装插件 方式一:在线安装 进入容器 docker exec -it elasticsearch /bin/bash 在线下载并安装 ./bin/ela ...

  2. Java并发编程:volatile关键字解析(学习总结-海子)

    博文地址:Java并发编程:volatile关键字解析

  3. servlet中cookie和session操作

    1.1 软件中的会话 一次会话: 打开浏览器 -> 访问一些服务器内容 -> 关闭浏览器 登录场景: 打开浏览器 -> 浏览到登陆页面 -> 输入用户名和密码 -> 访问 ...

  4. 转:hive面试题

    有一张很大的表:TRLOG该表大概有2T左右TRLOG:CREATE TABLE TRLOG(PLATFORM string,USER_ID int,CLICK_TIME string,CLICK_U ...

  5. ArcGIS中国工具(ArcGISCTools)2.0正式发布

    ArcGIS中国工具,简称CTools,集成在ArcMap10.0, ArcMap10.1, ArcMap10.2,安装就可以直接使用.主要有以下功能 1.接合图表生成2.图框工具3.制图工具4.图形 ...

  6. RocketMQ读书笔记5——消息队列的核心机制

    [Broker简述] Broker是RocketMQ的核心,大部分“重量级”的工作都是由Broker完成的,包括: 1.接受Producer发过来的消息: 2.处理Consumer的消费信息请求: 3 ...

  7. Python爬虫教程-14-爬虫使用filecookiejar保存cookie文件(人人网)

    Python爬虫教程-14-爬虫使用filecookiejar保存cookie文件(人人网) 上一篇介绍了利用CookieJar访问人人网,本篇将使用filecookiejar将cookie以文件形式 ...

  8. SQL点点滴滴_聚集索引设计指南-转载

    聚集索引基于数据行的键值在表内排序和存储这些数据行, 每个表只能有一个聚集索引, 因为数据行本身只能按一个顺序存储. 有关聚集索引体系结构的详细信息, 请参阅 聚集索引结构. 每个表几乎都对列定义聚集 ...

  9. 生成证书,用于签名Android应用

    1. keytool 命令 1)使用JDK中的一个命令keytool,都有哪些命令呢,使用 keytool -help 进行查看 2)本次使用 keytool -genkeypair 命令生成签名,查 ...

  10. Docker的安装及加速

    使用 yum 安装(CentOS 7下) Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 uname ...