-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday16.2.py
71 lines (62 loc) · 1.08 KB
/
day16.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from collections import deque
D = {}
aid = 0
nid = 0
while True:
l = input()
if l[0] == '~':
break
l = l.split(' ')
vn = l[1]
rate = int(l[4][5:-1])
caid = -1
if rate > 0:
caid = aid
aid += 1
cnid = nid
nid += 1
ld = []
for i in range(9, len(l)):
s = l[i]
if i != len(l)-1:
s = s[:-1]
ld.append(s)
D[vn] = (rate, ld, cnid, caid)
dp = [[-1] * 100 for _ in range(pow(2, 18))]
def bfs(vx, sx, tx):
Q = deque()
Q.append((vx, sx, tx, 0))
while(len(Q) > 0):
cq = Q.popleft()
v = cq[0]
s = cq[1]
t = cq[2]
id1 = cq[3]
z = D[v][2]
w = D[v][3]
if dp[id1][z] >= s:
continue
dp[id1][z] = s
if t == 26:
continue
f = D[v][0]
c = D[v][1]
if f > 0 and (id1 & (1<<w)) == 0:
t2 = t + 1
s2 = s + f * (26 - t2)
Q.append((v, s2, t2, id1 | (1<<w)))
for x in c:
Q.append((x, s, t+1, id1))
bfs('AA', 0, 0)
ans2 = 0
dp2 = [0] * (1<<16)
for i in range(1<<16):
for j in range(100):
dp2[i] = max(dp2[i], dp[i][j])
for i in range(1<<16):
j = i
while j > 0:
ni = ((~i) & ((1<<16)-1))
j = (j-1) & i
ans2 = max(ans2, dp2[ni] + dp2[j])
print(ans2)