POJ 1949

Chores

Time Limit: 3000MS

Memory Limit: 30000K

Total Submissions: 5258

Accepted: 2468

 

 

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 cannotbe started until others have been completed, e.g., it is impossible to wash thecows until they are in the stalls. 

 

Farmer John has a list of N (3 <= N <= 10,000) chores thatmust be completed. Each chore requires an integer time (1 <= length of time<= 100) to complete and there may be other chores that must be completedbefore this chore is started. We will call these prerequisite chores. At leastone chore has no prerequisite: the very first one, number 1. Farmer John's listof chores is nicely ordered, and chore K (K > 1) can have only chores 1,.K-1as prerequisites. Write a program that reads a list of chores from 1 to N withassociated times and all perquisite chores. Now calculate the shortest time itwill take to complete all N chores. Of course, chores that do not depend oneach 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 containsthe 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 timerequired 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.

]


这题看穿了就是有向无环图的关键路径,思路就是dp,那样比较自然。(原先我想消耗时间取反,然后求最短路,发现显然是错误的)


/*
 * Author: tender
 * Created Time:  2014/12/31 14:34:15
 * File Name: 1949.cpp
 */
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <string>
#include <vector>
#include <set>

using namespace std;
const double pi = acos(-1.0);
const int maxn = 10000;
const int INF = 0x1FFFFFFF;
int dp[maxn + 5], cost[maxn + 5];
vector<int> pre[maxn + 5];
int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
       for (int i = 1; i <= n; i++) {
           dp[i] = INF;
           pre[i].clear();
       }
       int m, v;
       for (int i = 1; i <= n; i++) {
           scanf("%d%d", &cost[i], &m);
           while (m--) {
               scanf("%d", &v);
               pre[i].push_back(v);
           }
       }
       dp[1] = cost[1];
       for (int i = 2; i <= n; i++) {
           int _max = 0;
           for (int j = 0; j < pre[i].size(); j++) {
               _max = max(_max, dp[pre[i][j]]);
           }
           dp[i] = _max + cost[i];
       }
       int res = 0;
       for (int i = 1; i <= n; i++)
           res = max(res, dp[i]);
       printf("%d\n", res);
    }
    return 0;
}


另外,这题有一个唯一特殊的地方,就是拓扑序在前的一定编号比较小,这也导致了最终在dp的时候直接使用了for (int i = 1; i <= n; i++)。如果是更一般的有向无环图,那么遍历拓扑队列即可。


另外一个很傻的问题是,如果任务不能并行,那完成的最短时间是?那如果是有向无环图,就是所有任务的总和了。问题就显得很没有意义了。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值