-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1757-donkey-barril.py
55 lines (41 loc) · 1.05 KB
/
1757-donkey-barril.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
'''
2
5 4
0 1
0 2
0 3
0 4
5 4
0 1
0 2
0 3
2 4
'''
import heapq
from collections import defaultdict
def Dijkstra(custo, total_vertices):
distancia = [total_vertices] * total_vertices
distancia[0] = 0
fila = []
heapq.heappush(fila, (0, 0))
while fila:
c, v = heapq.heappop(fila)
if distancia[v] < c:
continue
for r in set(range(total_vertices)) - {v}:
p = custo[r][v]
if distancia[r] > distancia[v] + p:
distancia[r] = distancia[v] + p
heapq.heappush(fila, (distancia[r], r))
return distancia
total_testes = int(input())
for _ in range(total_testes):
total_cidades, caminhos_em_terra = input().split()
total_cidades, caminhos_em_terra = int(total_cidades), int(caminhos_em_terra)
grafo = defaultdict(lambda: [0]*total_cidades)
for caminho_em_terra in range(caminhos_em_terra):
A, B = input().split()
A, B = int(A), int(B)
grafo[A][B] = 1
grafo[B][A] = 1
print(max(Dijkstra(grafo, total_cidades)))