-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1005.py
46 lines (41 loc) · 1.46 KB
/
1005.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
41
42
43
44
45
46
import sys
T = int(sys.stdin.readline())
res = ""
for case in range(T):
N, K = map(int, sys.stdin.readline().split())
l_D = list(map(int, sys.stdin.readline().split()))
l_follow = [[] for _ in range(N)]
l_precede = [[] for _ in range(N)]
for _ in range(K):
X, Y = map(int, sys.stdin.readline().split())
l_follow[X-1].append(Y-1)
l_precede[Y-1].append(X-1)
W = int(sys.stdin.readline()) - 1
buildArray = list()
for bdg in range(N):
if not l_precede[bdg]:
buildArray.append(bdg)
time = 0
while l_precede[W]:
minBuildTime = 100000
for bdg in buildArray:
minBuildTime = min(minBuildTime, l_D[bdg])
time += minBuildTime
tempPush = list()
set_buildComplete = set()
for bdg in buildArray:
l_D[bdg] -= minBuildTime
if l_D[bdg] == 0:
set_buildComplete.add(bdg)
if l_follow[bdg]:
for nextBuild in l_follow[bdg]:
l_precede[nextBuild].remove(bdg)
if not l_precede[nextBuild]:
tempPush.append(nextBuild)
tempExist = list()
for bdg in buildArray:
if bdg not in set_buildComplete:
tempExist.append(bdg)
buildArray = tempExist + tempPush
res = "%s\n%d" % (res, time + l_D[W])
sys.stdout.write(res.lstrip())