洛谷 P1113 杂务
Description
John
的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它。比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作。尽早将所有杂务完成是必要的,因为这样才有更多时间挤出更多的牛奶。当然,有些杂务必须在另一些杂务完成的情况下才能进行。比如:只有将奶牛赶进牛棚才能开始为它清洗乳房,还有在未给奶牛清洗乳房之前不能挤奶。我们把这些工作称为完成本项工作的准备工作。至少有一项杂务不要求有准备工作,这个可以最早着手完成的工作,标记为杂务1。John
有需要完成的n个杂务的清单,并且这份清单是有一定顺序的,杂务k(k>1)的准备工作只可能在杂务1至k−1中。写一个程序从1到n读入每个杂务的工作说明。计算出所有杂务都被完成的最短时间。当然互相没有关系的杂务可以同时工作,并且,你可以假定
John
的农场有足够多的工人来同时完成任意多项任务。
Input
第1行:一个整数nnn,必须完成的杂务的数目(3≤n≤10,000);
第2至(n+1)行: 共有n行,每行有一些用1个空格隔开的整数,分别表示:
* 工作序号(1至n,在输入文件中是有序的);
* 完成工作所需要的时间len(1≤len≤100)
* 一些必须完成的准备工作,总数不超过100个,由一个数字0结束。有些杂务没有需要准备的工作只描述一个单独的0,整个输入文件中不会出现多余的空格。
Output
- 一个整数,表示完成所有杂务所需的最短时间。
Sample Input
7 1 5 0 2 2 1 0 3 3 2 0 4 6 1 0 5 1 2 4 0 6 8 2 4 0 7 4 3 5 6 0
Sample output
23
题解:
- 一道简单的DAG
- 用拓扑转换为DAG然后dp即可
#include <iostream>
#include <cstdio>
#include <queue>
#define maxn 50005
using namespace std;
struct E {int next, to;} e[maxn * 100], edge[maxn * 100];
int n, num, cnt, num_edge, ans;
int a[maxn], h[maxn], in[maxn], seq[maxn], dp[maxn], head[maxn];
void add(int u, int v)
{
e[++num].next = h[u];
e[num].to = v;
h[u] = num;
}
void add_edge(int from, int to)
{
edge[++num_edge].next = head[from];
edge[num_edge].to = to;
head[from] = num_edge;
}
void topsort()
{
queue<int> que;
for(int i = 1; i <= n; i++)
if(!in[i]) que.push(i);
while(!que.empty())
{
int now = que.front();
seq[++cnt] = now; que.pop();
for(int i = h[now]; i != 0; i = e[i].next)
{
in[e[i].to]--;
if(!in[e[i].to]) que.push(e[i].to);
}
}
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
int t; cin >> t >> t;
a[i] = t;
while(1)
{
cin >> t;
if(!t) break;
add(t, i), in[i]++;
add_edge(i, t);
}
}
topsort();
for(int i = 1; i <= n; i++)
{
int x = seq[i];
for(int j = head[x]; j != 0; j = edge[j].next)
dp[x] = max(dp[x], dp[edge[j].to]);
dp[x] += a[x], ans = max(ans, dp[x]);
}
cout << ans;
return 0;
}