-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathseungwoo.py
33 lines (26 loc) · 884 Bytes
/
seungwoo.py
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
import sys
import heapq
def solution():
inp = sys.stdin.readline
for _ in range(int(inp())):
n, k = map(int, inp().split())
q, top, parents, cost = [], [0 for _ in range(n + 1)], [[] for _ in range(n + 1)], [0] + list(map(int, inp().split()))
for _ in range(k):
x, y = map(int, inp().split())
top[y] += 1
parents[x].append(y)
target = int(inp())
for i in range(1, n + 1):
if not top[i]:
heapq.heappush(q, (cost[i], i))
while True:
c, n = heapq.heappop(q)
if n == target:
print(c)
break
for parent in parents[n]:
top[parent] -= 1
if not top[parent]:
heapq.heappush(q, (c + cost[parent], parent))
if __name__ == "__main__":
solution()