8000 Merge pull request #387 from neppiness/1005 · jh2ee/basic-algo-lecture@b3fa0d2 · GitHub
[go: up one dir, main page]

Skip to content

Commit b3fa0d2

Browse files
Merge pull request encrypted-def#387 from neppiness/1005
Update 0x1A 1005.cpp
2 parents 3207e11 + 298128b commit b3fa0d2

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

0x1A/solutions/1005.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,47 @@
1-
// Authored by : BaaaaaaaaaaarkingDog
1+
// Authored by : scsc3204
22
// Co-authored by : -
3-
// http://boj.kr/****************
3+
// http://boj.kr/ddd8214a903148c381289d51e1027039
44
#include <bits/stdc++.h>
55
using namespace std;
66

7-
int main(void){
7+
int solve() {
8+
int n, k; cin >> n >> k;
9+
vector<int> adj[n + 2];
10+
int cost[n + 2] = {};
11+
int tot[n + 2] = {};
12+
int ind[n + 2] = {};
13+
14+
for(int i = 1; i <= n; i++)
15+
cin >> cost[i];
16+
17+
while(k--) {
18+
int u, v;
19+
cin >> u >> v;
20+
ind[v]++;
21+
adj[u].push_back(v);
22+
}
23+
24+
int w; cin >> w;
25+
queue<int> q;
26+
for(int i = 1; i <= n; i++)
27+
if(!ind[i]) q.push(i);
28+
29+
while(!q.empty()) {
30+
int cur = q.front(); q.pop();
31+
for(int nxt : adj[cur]) {
32+
tot[nxt] = max(tot[nxt], cost[cur] + tot[cur]);
33+
ind[nxt]--;
34+
if(!ind[nxt]) q.push(nxt);
35+
}
36+
}
37+
return tot[w] + cost[w];
38+
}
39+
40+
int main() {
841
ios::sync_with_stdio(0);
942
cin.tie(0);
10-
43 49E8 +
44+
int t; cin >> t;
45+
while(t--)
46+
cout << solve() << '\n';
1147
}

0 commit comments

Comments
 (0)
0