[Link] https://www.acmicpc.net/problem/1005
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader br;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
int test = toi(br.readLine());
int[] cost;
int[] line;
Node[] nodes;
for(int iter = 0; iter < test; iter++) {
line = getArr();
int n = line[0], k = line[1];
cost = getArr();
nodes = new Node[n];
for(int i = 0; i < n; i++) nodes[i] = new Node(i, cost[i]);
for(int i = 0; i < k; i++) {
line = getArr();
int a = line[0] - 1, b = line[1] - 1;
nodes[b].deg++;
nodes[a].list.add(nodes[b]);
}
int haveToBuild = toi(br.readLine()) - 1;
Queue<Node> q = new LinkedList<>();
int min = Integer.MAX_VALUE;
for(int i = 0; i < n; i++) {
if(nodes[i].init || nodes[i].deg != 0) continue;
q.add(nodes[i]);
while(!q.isEmpty()) {
Node node = q.poll();
for(Node next: node.list) {
if(next.init) {
next.min = Math.max(next.min, node.min + cost[next.idx]);
if(--next.deg == 0) q.add(next);
} else {
next.min = node.min + cost[next.idx];
if(--next.deg == 0) q.add(next);
next.init = true;
}
}
}
}
sb.append(nodes[haveToBuild].min).append("\n");
}
print(sb);
}
static class Node {
ArrayList<Node> list = new ArrayList<>();
int deg;
int min;
int idx;
boolean init;
Node(int idx, int min) { this.idx = idx; this.min = min; }
}
static int toi(String s) { return Integer.parseInt(s); }
static String[] getLine() throws IOException { return br.readLine().split(" "); }
static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
static <T> void print(T s) { System.out.print(s); }
static <T> void println(T s) { System.out.println(s); }
}