[题解]P1113 杂务

本题为拓扑排序板子题,但本蒟蒻耍无赖,巧妙的用dp转移的思想做出来了,发一篇题解纪念一下

#include<bits/stdc++.h>

using namespace std;
using ll=long long;
int t;
const int MAX=1e4+10;
struct node{
    int num,time;
}n[MAX];
bool cmp(node a,node b){
    return a.time<b.time;
}
void solve(){
    cout<<(*max_element(begin(n),end(n),cmp)).time<<endl;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>t;
    for(int i=1;i<=t;i++){
        cin>>n[i].num>>n[i].time;
        string _;getline(cin,_);
        stringstream s;s<<_;
        int num,maxx=0;
        while(s>>num&&num!=0) maxx=max(n[num].time,maxx);
        n[i].time+=maxx;
    }
    solve();
    return 0;
}

另,附Testcase1并给出转移过程解释:

参数分别为:编号 结点时间 前置条件(每行以0结尾)
1 3 0
2 9 1 0
3 2 1 2 0
4 2 1 2 3 0
5 12 2 0
6 1 1 2 3 4 5 0
7 17 3 0
8 7 1 2 3 4 5 6 7 0
9 13 1 3 8 0
10 2 1 2 3 4 5 6 7 8 9 0

转移过程:

  1. 节点1:最早完成时间为3(自身时间)。
  2. 节点2:前置节点是节点1,所以最早完成时间为节点1的最早完成时间 + 节点2的时间 = 3 + 9 = 12。
  3. 节点3:前置节点是节点1和节点2,所以最早完成时间为 max(节点1的最早完成时间, 节点2的最早完成时间) + 节点3的时间 = max(3, 12) + 2 = 14。
  4. 节点4:前置节点是节点1、节点2和节点3,所以最早完成时间为 max(节点1的最早完成时间, 节点2的最早完成时间, 节点3的最早完成时间) + 节点4的时间 = max(3, 12, 14) + 2 = 16。
  5. 节点5:前置节点是节点2,所以最早完成时间为节点2的最早完成时间 + 节点5的时间 = 12 + 12 = 24。
  6. 节点6:前置节点是节点1、节点2、节点3、节点4和节点5,所以最早完成时间为 max(节点1的最早完成时间, 节点2的最早完成时间, 节点3的最早完成时间, 节点4的最早完成时间, 节点5的最早完成时间) + 节点6的时间 = max(3, 12, 14, 16, 24) + 1 = 25。
  7. 节点7:前置节点是节点3,所以最早完成时间为节点3的最早完成时间 + 节点7的时间 = 14 + 17 = 31。
  8. 节点8:前置节点是节点1到节点7,所以最早完成时间为 max(节点1到节点7的最早完成时间) + 节点8的时间 = max(3, 12, 14, 16, 24, 25, 31) + 7 = 38。
  9. 节点9:前置节点是节点1、节点3和节点8,所以最早完成时间为 max(节点1的最早完成时间, 节点3的最早完成时间, 节点8的最早完成时间) + 节点9的时间 = max(3, 14, 38) + 13 = 51。
  10. 节点10:前置节点是节点1到节点9,所以最早完成时间为 max(节点1到节点9的最早完成时间) + 节点10的时间 = max(3, 12, 14, 16, 24, 25, 31, 38, 51) + 2 = 53。
posted @   椰萝Yerosius  阅读(1)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示