杂物
P1113 杂务 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 求所有杂物的最早完成时间,和关键路径的求法类似
- ans为每个任务的完成时间,初始化为每个任务需要的时间
- 每次bfs到一个点时,遍历它所有连接到的点,如果可以增大ans值就更新为起点的值+终点任务的持续时间
- 答案为ans中最大的,也就是最少需要的时间
// https://www.luogu.com.cn/problem/P1113
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 10001
int n, dutt[MAX], ans[MAX], far[MAX];
vector<int> G[MAX];
void input()
{
cin >> n;
int t, len, pre;
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &t, far + i);
while (true)
{
scanf("%d", &pre);
if (pre == 0)
{
break;
}
dutt[t]++;
G[pre].push_back(t);
}
}
}
queue<int> q;
void bfs()
{
for (int i = 1; i <= n; i++)
{
if (dutt[i] == 0)
{
q.push(i);
ans[i] = far[i];
}
}
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = 0; i < G[t].size(); i++)
{
int to = G[t][i];
dutt[to]--;
ans[to] = max(ans[to], ans[t] + far[to]);
if (!dutt[to])
{
q.push(to);
}
}
}
}
int main()
{
int t = 0;
input();
bfs();
for (int i = 1; i <= n; i++)
{
t = max(t, ans[i]);
}
printf("%d", t);
}