-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN2056.cpp
56 lines (56 loc) · 1.23 KB
/
N2056.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# include <stdio.h>
# include <vector>
# include <queue>
# include <algorithm>
using namespace std;
# define MAXVAL 2000000
int cost[10000];
vector<int>T[10001];
vector<int>T2[10001];
vector<int> ind;
int DP[10001] ={0};
int main(){
int n;
scanf("%d",&n);
ind.assign(n+1,0);
int preCnt;
int preWork;
for(int i = 1;i<=n;i++){
scanf("%d",&cost[i]);
scanf("%d",&preCnt);
for(int j = 0;j<preCnt;j++){
scanf("%d",&preWork);
T[preWork].push_back(i);
ind[i] += 1;
}
}
queue<int> myQ;
for(int i = 1;i<=n;i++){
if(ind[i] == 0){
myQ.push(i);
DP[i] = cost[i];
}
}
while(!myQ.empty()){
int current = myQ.front();
myQ.pop();
for(int i = 0;i<T[current].size();i++){
int next = T[current][i];
ind[next] -= 1;
if (DP[next] < DP[current]+cost[next]) {
DP[next] = DP[current]+cost[next];
}
if(ind[next] == 0){
myQ.push(next);
}
}
}
int ans = 0;
for (int i=1; i<=n; i++) {
if (ans < DP[i]) {
ans = DP[i];
}
}
printf("%d\n",ans);
return 0;
}