-
Notifications
You must be signed in to change notification settings - Fork 6
/
xyzzy.py
55 lines (54 loc) · 1.9 KB
/
xyzzy.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
47
48
49
50
51
52
53
54
55
import sys
INF, n = float('inf'), -1
vals, el, tmp = [], [], []
for line in sys.stdin:
if n == -1:
n = V = int(line)
v = -1
vals.clear()
el.clear()
tmp.clear()
elif n != 0:
nums = list(map(int, line.split()))
if v == -1:
v, l, *nums = nums
vals.append(v)
tmp.extend(map(lambda x: (len(vals)-1, x-1), nums))
if len(tmp) == l:
el.extend(tmp)
v = -1
tmp.clear()
n -= 1
if n == 0:
D = [INF]*V
D[0], win = -100, 0
for _ in range(V):
if D[-1] != INF and D[-1] < 0:
win = 1
break
for x, y in el:
new = D[x] - vals[y]
if new < 0 and new < D[y] and D[x] < 0: D[y] = new
if not win:
D2, stop = D.copy(), False
for _ in range(V):
tmp = D2.copy()
for x, y in el:
new = tmp[x] - vals[y]
if new < 0 and new < tmp[y] and tmp[x] < 0: tmp[y] = new
for i in range(V):
if D2[i] == INF and tmp[i] != INF:
stop = True
break
if stop: break
D2 = tmp
D2 = [D2[i] + ((D2[i]-D[i])*1e8 if D2[i] != INF else 0) for i in range(V)] # proof by AC?
for _ in range(V):
if D2[-1] != INF and D2[-1] < 0:
win = 1
break
for x, y in el:
new = D2[x] - vals[y]
if new < 0 and new < D2[y] and D2[x] < 0: D2[y] = new
print(['hopeless', 'winnable'][win])
n = -1