天天看点

杂物_洛谷1113_拓扑排序

题目描述

John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它。比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作。尽早将所有杂务完成是必要的,因为这样才有更多时间挤出更多的牛奶。当然,有些杂务必须在另一些杂务完成的情况下才能进行。比如:只有将奶牛赶进牛棚才能开始为它清洗乳房,还有在未给奶牛清洗乳房之前不能挤奶。我们把这些工作称为完成本项工作的准备工作。至少有一项杂务不要求有准备工作,这个可以最早着手完成的工作,标记为杂务1。John有需要完成的n个杂务的清单,并且这份清单是有一定顺序的,杂务k(k>1)的准备工作只可能在杂务1..k-1中。

写一个程序从1到n读入每个杂务的工作说明。计算出所有杂务都被完成的最短时间。当然互相没有关系的杂务可以同时工作,并且,你可以假定John的农场有足够多的工人来同时完成任意多项任务。

输入格式:

第1行:一个整数n,必须完成的杂务的数目(3<=n<=10,000);

第2 ~ n+1行: 共有n行,每行有一些用1个空格隔开的整数,分别表示:

工作序号(1..n,在输入文件中是有序的);

完成工作所需要的时间len(1<=len<=100);

一些必须完成的准备工作,总数不超过100个,由一个数字0结束。有些杂务没有需要准备的工作只描述一个单独的0,整个输入文件中不会出现多余的空格。

输出格式:

一个整数,表示完成所有杂务所需的最短时间。

题解

傻逼题,拓扑排然后对于每个点显然最早完成时间就是所有前提完成的最晚时间+本身耗时

最后要在所有答案中找最大值

Code

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define debug puts("-----")
#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)
#define drp(i, st, ed) for (int i = st; i >= ed; i -= 1)
#define fill(x, t) memset(x, t, sizeof(x))
#define min(x, y) x<y?x:y
#define max(x, y) x>y?x:y
#define PI (acos(-1.0))
#define EPS (1e-8)
#define INF (1<<30)
#define ll long long
#define db double
#define ld long double
#define N 10001
#define E N * 201 + 1
#define MOD 100000007
#define L 255
using namespace std;
struct edge{int x, y, w, next;}e[E];
int ind[N], len[N], ls[N], t[N];
int edgeCnt;
inline int read(){
    int x = , v = ;
    char ch = getchar();
    while (ch < '0' || ch > '9'){
        if (ch == '-'){
            v = -;
        }
        ch = getchar();
    }
    while (ch <= '9' && ch >= '0'){
        x = (x << ) + (x << ) + ch - '0';
        ch = getchar();
    }
    return x * v;
}
inline int addEdge(int &cnt, const int &x, const int &y, const int &w = ){
    e[++ cnt] = (edge){x, y, w, ls[x]}; ls[x] = cnt;
    ind[y] += ;
    return ;
}
inline int topSort(const int &st){
    queue<int>q;
    q.push(st);
    while (!q.empty()){
        int now = q.front(); q.pop();
        for (int i = ls[now]; i; i = e[i].next){
            t[e[i].y] = max(t[e[i].y], t[now] + len[now]);
            if (! -- ind[e[i].y]){
                q.push(e[i].y);
            }
        }
    }
}
int main(void){
    int n = read();
    rep(i, , n){
        int now = read();
        len[i] = read();
        while (int pre = read()){
            addEdge(edgeCnt, pre, now);
        }
    }
    int st = ;
    rep(i, , n){
        if (!ind[i]){
            st = i;
        }
    }
    topSort(st);
    int ans = ;
    rep(i, , n){
        ans = max(ans, t[i] + len[i]);
    }
    printf("%d\n", ans);
    return ;
}
           

继续阅读