Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import stdin, stdout
- import math
- import heapq
- R = lambda : stdin.readline().strip()
- RL = lambda f=None: list(map(f, R().split(' '))) if f else list(R().split(' '))
- output = lambda x: stdout.write(str(x) + '\n')
- output_list = lambda x: output(' '.join(map(str, x)))
- MAX = int(5e5 + 5)
- def djkstra(n, m, k, graph, cost, vis, a, b):
- heap = []
- heapq.heappush(heap, [0, [1,0]])
- cost[1][0]=0
- while len(heap):
- curr = heapq.heappop(heap)
- curr_n, curr_s = curr[1][0], curr[1][1]
- cst = cost[curr_n][curr_s]
- for i in range(len(graph[curr_n])):
- if graph[curr_n][i][0]==curr_n:
- continue
- c_cst = cst + graph[curr_n][i][1]
- if (cost[graph[curr_n][i][0]][curr_s] == -1):
- heapq.heappush(heap, [c_cst*-1, [graph[curr_n][i][0], curr_s]])
- cost[graph[curr_n][i][0]][curr_s] = c_cst
- elif cost[graph[curr_n][i][0]][curr_s] > c_cst :
- cost[graph[curr_n][i][0]][curr_s] = c_cst
- heapq.heappush(heap, [c_cst*-1, [graph[curr_n][i][0], curr_s]])
- c_cst = cst
- if curr_s == k:
- continue
- if cost[graph[curr_n][i][0]][curr_s+1] == -1:
- heapq.heappush(heap, [c_cst*-1, [graph[curr_n][i][0], curr_s+1]])
- cost[graph[curr_n][i][0]][curr_s+1]=c_cst
- elif cost[graph[curr_n][i][0]][curr_s+1]>c_cst:
- cost[graph[curr_n][i][0]][curr_s+1]=c_cst
- heapq.heappush(heap, [c_cst*-1, [graph[curr_n][i][0], curr_s+1]])
- ret = int(2e18)
- for i in range(1,n+1):
- for j in range(k+1):
- if cost[i][j] == -1:
- continue
- ret = min(ret, cost[i][j])
- print(ret, i, j)
- return ret
- n = int(R())
- a = int(R())
- b = int(R())
- k = int(R())
- m, _ = RL(int)
- graph = [ [] for i in range(n+1) ]
- cost = [ 20*[-1] for i in range(n+1) ]
- vis = [ 20*[False] for i in range(n+1) ]
- for i in range(m):
- u, v, w = RL(int)
- graph[u].append([v, w])
- graph[v].append([u, w])
- print(djkstra(n, m, k, graph, cost, vis, a, b))
Add Comment
Please, Sign In to add comment