-
Notifications
You must be signed in to change notification settings - Fork 0
/
D.py
70 lines (55 loc) · 1.67 KB
/
D.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
import heapq
class Edge():
def __init__(self, to, cost):
self.to = to
self.cost = cost
class Pair():
def __init__(self, first, second):
#first: 最短距離, second: 頂点番号
self.first = first
self.second = second
def __lt__(self, other):
return (self.first ,self.second) < (other.first, other.second)
def dijkstra(s):
heapq.heapify(que)
d[s] = 0
heapq.heappush(que, Pair(0, s))
while que:
p = heapq.heappop(que)
v = p.second
if d[v] < p.first:
continue
for i in range(len(G[v])):
e = G[v][i]
if d[e.to] > d[v] + e.cost:
d[e.to] = d[v] + e.cost
heapq.heappush(que, Pair(d[e.to], e.to))
n = int(input())
G = [[] for _ in range(n)]
for i in range(n-1):
v, to, c = map(int, input().split())
G[v-1].append(Edge(to-1, c))
G[to-1].append(Edge(v-1, c))
q, k = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(q)]
d = [float('inf')]*n
que = []
dijkstra(k-1)
for x, y in xy:
print(d[x-1]+d[y-1])
'''using warshall-floyd is going to be TLE because of O(n^3)'''
# n = int(input())
# d = [[float('inf')]*n for _ in range(n)]
# for i in range(n-1):
# a, b, c = map(int, input().split())
# d[a-1][b-1] = c
# d[b-1][a-1] = c
# for k in range(n):
# for i in range(n):
# for j in range(n):
# if d[i][k]!=float('inf') and d[k][i]!=float('inf'):
# d[i][j] = min(d[i][j], d[i][k] + d[k][j])
# q, k = map(int, input().split())
# xy = [list(map(int, input().split())) for _ in range(q)]
# for x, y in xy:
# print(d[x-1][k-1]+d[k-1][y-1])