fork download
  1. # Haskellwhores sosnooley.
  2. # No need to handle special cases like 'Nothing'.
  3. inf = float('inf')
  4.  
  5.  
  6. def yoba(map, start, end):
  7. # Haskellwhores sosnooley again.
  8. # 'bounds'? Hah.
  9. w = len(map)
  10. h = w and len(map[0])
  11. # And again. This lambda was borrowed from the Reifag's code.
  12. value = lambda x, y: 0 if y == start \
  13. else inf if not x \
  14. else min(arr[x - 1][n] + map[n][y] for n in range(h))
  15.  
  16. # Pythonwhores sosnooley.
  17. # Much more readable variant:
  18. # arr = [[value(x, y) for y in range(h)] for x in range(w)]
  19. # Unfortunately, it does not work because value() tries to access
  20. # the arr list.
  21. arr = []
  22. for x in range(w): arr.append([value(x, y) for y in range(h)])
  23. return min(arr[x][end] for x in range(w))
  24.  
  25.  
  26. if __name__ == '__main__':
  27. # Haskellwhores sosnooley again.
  28. toArray2D = lambda arr: list(zip(*arr))
  29. graph = [[inf, 7000000000000000000000000000000000000000000000000000000000000, 9000000000000000000000000000000000000000000000000000000000000, inf, inf, 14000000000000000000000000000000000000000000000000000000000000],
  30. [ 7000000000000000000000000000000000000000000000000000000000000, inf, 10000000000000000000000000000000000000000000000000000000000000, 15000000000000000000000000000000000000000000000000000000000000, inf, inf],
  31. [ 9000000000000000000000000000000000000000000000000000000000000, 10000000000000000000000000000000000000000000000000000000000000, inf, 11000000000000000000000000000000000000000000000000000000000000, inf, 2000000000000000000000000000000000000000000000000000000000000],
  32. [inf, 15000000000000000000000000000000000000000000000000000000000000, 11000000000000000000000000000000000000000000000000000000000000, inf, 6000000000000000000000000000000000000000000000000000000000000, inf],
  33. [inf, inf, inf, 6000000000000000000000000000000000000000000000000000000000000, inf, 9000000000000000000000000000000000000000000000000000000000000],
  34. [ 14000000000000000000000000000000000000000000000000000000000000, inf, 2000000000000000000000000000000000000000000000000000000000000, inf, 9000000000000000000000000000000000000000000000000000000000000, inf]]
  35. print(yoba(toArray2D(graph), 0, 4))
  36.  
Success #stdin #stdout 0.02s 5852KB
stdin
Standard input is empty
stdout
20000000000000000000000000000000000000000000000000000000000000