[poj 1949]Chores 题解 [dp]
2016-08-21 17:18
423 查看
Chores
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
Sample Output
Hint
Source
USACO 2002 February
题目描述:
有n个任务,第i个任务需要时间xi来完成,并且第i个任务必须在它 “前面的” 某些任务完成之后才能开始。
给你任务信息,问你最短需要多少时间来完成任务。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int n;
int t[10001];
int dp[10001];
int ans = 0;
int main()
{
cin>>n;
//dp[i] = min(dp[i],dp[j]+w[i]);
int cnt,p;
for(int i=1;i<=n;i++)
{
scanf("%d",&t[i]);
scanf("%d",&cnt);
for(int j=1;j<=cnt;j++)
{
scanf("%d",&p);
if(dp[p]+t[i]>dp[i])dp[i] = dp[p]+t[i];
}
dp[i] = max(dp[i],t[i]);
ans = max(ans,dp[i]);
}
cout<<ans;
return 0;
}
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 5773 | Accepted: 2723 |
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
USACO 2002 February
题目描述:
有n个任务,第i个任务需要时间xi来完成,并且第i个任务必须在它 “前面的” 某些任务完成之后才能开始。
给你任务信息,问你最短需要多少时间来完成任务。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int n;
int t[10001];
int dp[10001];
int ans = 0;
int main()
{
cin>>n;
//dp[i] = min(dp[i],dp[j]+w[i]);
int cnt,p;
for(int i=1;i<=n;i++)
{
scanf("%d",&t[i]);
scanf("%d",&cnt);
for(int j=1;j<=cnt;j++)
{
scanf("%d",&p);
if(dp[p]+t[i]>dp[i])dp[i] = dp[p]+t[i];
}
dp[i] = max(dp[i],t[i]);
ans = max(ans,dp[i]);
}
cout<<ans;
return 0;
}
相关文章推荐
- POJ 1949 Chores(树状DP)
- POJ 1949 Chores(树形dp)
- (POJ DP1.1)POJ 1949 Chores(简单DP)
- POJ 1949 Chores(DP)
- POJ 1949 Chores
- POJ 3744 Scout YYF I 详细题解(矩阵优化概率DP)水题
- POJ 1949 Chores 拓扑排序
- poj 1949——Chores
- (POJ 1949)Chores DAG简单DP
- poj 1949 Chores
- POJ 2342 Anniversary Party题解 - 树形DP入门题目
- POJ 1947 Rebuilding Roads 题解【树形DP】
- POJ 1192 最优连通子集 最详细的题解 (无向树树形DP)
- 7_6_O题 Collecting Bugs题解[POJ 2906] (概率DP求期望)
- poj 1949 Chores
- poj 1949 Chores(基础题)
- POJ 1949 Chores 动态规划
- 【POJ1159】【DP】17.2.6 T1 强迫症 题解
- 【poj 1949】Chores
- ★Uva 1626 && POJ 1141 Brackets sequence 详细题解(区间DP+递归打印)