【奇怪的动态规划】洛谷P-1113杂务

翻了翻题解
我被惊艳了
水一发博客
因为题目保证了杂务k(k>1)k(k>1)的准备工作只可能在杂务11至k-1k−1中。
所以到达杂务k时,前k-1个杂务一定是已经可以求出其最晚时间的
于是这就变成了一道动态规划题目,可这明摆着是拓扑排序啊

DP属实强大

题解代码:(写了对他的解释)

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,l,t,ans[10005],maxans;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%d",&i);
        scanf("%d",&l);
        int tmp=0;
        while(scanf("%d",&t)&&t)
            tmp=max(ans[t],tmp);//求出此个杂务前必须完成的杂务的最晚时间
        ans[i]=tmp+l;//这个杂务的最晚时间就是他的前驱的最晚时间加上他自己;
        maxans=max(ans[i],maxans);
    } 
    printf("%d\n",maxans);
    return 0;
 } //短小精悍的代码,我不配

猜你喜欢

转载自blog.csdn.net/qq_44833767/article/details/104303754
今日推荐