-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1005.cpp
55 lines (44 loc) · 985 Bytes
/
1005.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
#include <bits/stdc++.h>
using namespace std;
int dp[1001]; // N번 건물까지 짓는데 드는 최소 시간
int main(void) {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T;
cin >> T;
while(T--) {
int N, K, W;
cin >> N >> K;
queue<int> q;
vector<int> cost(N+1);
vector<int> nextStep[1001];
int inDeg[1001] = {0, }; // 진입차수
for(int i = 1; i <= N; i++)
cin >> cost[i];
for(int i = 1; i <= K; i++) {
int pre, next;
cin >> pre >> next;
nextStep[pre].push_back(next);
inDeg[next]++;
}
cin >> W;
for(int i = 1; i <= N; i++) {
if(!inDeg[i])
q.push(i);
dp[i] = cost[i];
}
while(!q.empty()) {
int cur = q.front();
q.pop();
for(int i = 0; i < nextStep[cur].size(); i++) {
int next = nextStep[cur][i];
inDeg[next]--;
dp[next] = max(dp[next], dp[cur] + cost[next]);
if(!inDeg[next])
q.push(next);
}
}
cout << dp[W] << '\n';
memset(dp, 0, sizeof(dp));
}
return 0;
}