您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

49 行
1.1KB

  1. import re
  2. import itertools
  3. text = open(0).read()
  4. graph = {}
  5. vals = {}
  6. for ln in text.splitlines():
  7. pos, val, *adjs = re.findall(r'[A-Z]{2}|\d+', ln)
  8. if int(val):
  9. vals[pos] = int(val)
  10. graph[pos] = {k: 1 for k in adjs}
  11. old = {k: v.copy() for k, v in graph.items()}
  12. for k in graph:
  13. graph[k][k] = 0
  14. edge = {k}
  15. seen = set()
  16. for step in range(1, 1000):
  17. edge = {n for k in edge for n in old[k]} - seen
  18. seen |= edge
  19. for n in edge:
  20. graph[k].setdefault(n, step)
  21. def tic(pending, start, left):
  22. out = []
  23. for end in pending:
  24. rem = max(0, left - graph[start][end] - 1)
  25. out.append([vals[end] * rem, end, rem])
  26. return sorted(out, reverse=True)
  27. def solve1(pending, total, pos, t_left):
  28. if not pending:
  29. yield total
  30. else:
  31. if ans1 > total + sum(vals[p] for p in pending) * t_left:
  32. return
  33. for contrib, pos, t_left in tic(pending, pos, t_left):
  34. yield from solve1(pending - {pos}, total + contrib, pos, t_left)
  35. ans1 = 0
  36. for ans in solve1(set(vals), 0, 'AA', 30):
  37. if ans > ans1:
  38. ans1 = ans
  39. print(ans1)