fork download
  1. import sys
  2. from heapq import heappop, heappush
  3.  
  4. def solution():
  5. inf = sys.maxsize
  6. num_city = int(sys.stdin.readline())
  7. num_bus = int(sys.stdin.readline())
  8.  
  9. graph = {i:[] for i in range(1, num_city+1)}
  10. for _ in range(num_bus):
  11. v1, v2, c = map(int, sys.stdin.readline().split())
  12. graph[v1].append((v2, c))
  13.  
  14. src, dst = map(int, sys.stdin.readline().split())
  15.  
  16. distance = [inf]*(num_city+1)
  17. minheap = [(0, src)]
  18.  
  19. visited = [False]*(num_city+1)
  20.  
  21. for _ in range(num_city):
  22. if minheap:
  23. cost, city = heappop(minheap)
  24. visited[city] = True
  25.  
  26. for adj_city, c in graph[city]:
  27. if not visited[adj_city] and cost + c < distance[adj_city]:
  28. distance[adj_city] = cost + c
  29. heappush(minheap, (distance[adj_city], adj_city))
  30. else:
  31. break
  32. print(distance[dst])
  33. return
  34.  
  35. solution()
Success #stdin #stdout 0.04s 9868KB
stdin
7
8
1 2 1
1 3 2
1 4 3
2 5 5
3 5 3
4 5 1
5 6 9
6 7 1
1 7
stdout
9223372036854775807