-
Notifications
You must be signed in to change notification settings - Fork 0
/
1005_2.py
35 lines (28 loc) · 917 Bytes
/
1005_2.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
34
35
from sys import stdin
from heapq import heapify, heappush, heappop
input = stdin.readline
def solve():
N, K = map(int, input().split())
time = [0, *map(int, input().split())]
outdegree = [[] for _ in range(N + 1)]
indegree = [0] * (N + 1)
for _ in range(K):
before, after = map(int, input().split())
outdegree[before].append(after)
indegree[after] += 1
W = int(input())
if indegree[W] == 0:
return time[W]
heap = [[time[i], i] for i in range(1, N + 1) if indegree[i] == 0]
heapify(heap)
while heap:
t, cur = heappop(heap)
for nxt in outdegree[cur]:
indegree[nxt] -= 1
if indegree[nxt] == 0:
if nxt == W:
return t + time[nxt]
heappush(heap, [t + time[nxt], nxt])
if __name__ == '__main__':
for _ in range(int(input())):
print(solve())