-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathseorim.py
40 lines (30 loc) · 996 Bytes
/
seorim.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
36
37
38
39
40
import sys
from collections import deque
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
time = [0] + list(map(int, input().split()))
complete_time = [-1] * (n+1)
indegree = [0] * (n+1)
graph = [[] for _ in range(n+1)]
for _ in range(k):
x, y = map(int, input().split())
indegree[y] += 1
graph[x].append(y)
w = int(input())
queue = deque()
for i in range(1, n+1):
if indegree[i] == 0:
queue.append(i)
complete_time[i] = time[i]
for _ in range(n):
curr = queue.popleft()
if curr == w:
print(complete_time[w])
break
for next in graph[curr]:
complete_time[next] = max(complete_time[next], time[next] + complete_time[curr])
indegree[next] -= 1
if indegree[next] == 0:
queue.append(next)