POJ 1949 Chores (很难想到的dp)
传送门:
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.
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).
* 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=0; dp[0]=0; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d %d",&x,&k); if(k>0) { for(int j=1;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 0; }
心之所向,素履以往
分类:
ACM
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南