Chores
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 3435 Accepted: 1622
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
题目描述:
有n个任务,第i个任务需要时间xi来完成,并且第i个任务必须在它 “前面的” 某些任务完成之后才能开始。
给你任务信息,问你最短需要多少时间来完成任务。
解题报告:
“前面的”,知道这些任务已经是top排序好的了。
dp[i] = 完成第i个任务时所需的最短的时间。
dp[i] = max(dp[j]) xi, j是需要在它前面完成的任务的序号。
max(dp[i])就是答案。
#include<iostream>
#include<fstream>
using namespace std;
int dp[10001];
void read(){
// ifstream cin("in.txt");
int i,j,k,s,t,p;
int n;
cin>>n;
int ans=0;
for(i=1;i<=n;i++)
{
cin>>j>>k;
p=0;
for(s=1;s<=k;s++)
{
cin>>t;
p=max(p,dp[t]);
}
dp[i]=p+j;
ans=max(dp[i],ans);
}
cout<<ans<<endl;
}
int main(){
read();
return 0;
}