Example #1
0
def do():
    N, M, R = list(map(int, input().split(" ")))
    rs = list(map(int, input().split(" ")))
    INF = float('inf')

    graph = [[INF for _ in range(N + 1)] for _ in range(N + 1)]
    for _ in range(M):
        a, b, c = (map(int, input().split(" ")))
        graph[a][b] = c
        graph[b][a] = c

    graph = wf(graph)

    perms = itertools.permutations(rs)
    ans = INF
    for temp in perms:
        cand = 0
        for i in range(0, R - 1):
            fr = temp[i]
            to = temp[i + 1]
            cand += graph[fr][to]
        ans = min(cand, ans)
    print(int(ans))
Example #2
0
from scipy.sparse.csgraph import floyd_warshall as wf

n, m = map(int, input().split())
UVL = [list(map(int, input().split())) for i in range(m)]
INF = float('inf')

d = [[INF] * n for i in range(n)]
for i in range(n):
    d[i][i] = 0
for u, v, l in UVL:
    if u != 1 and v != 1:
        d[u - 1][v - 1] = l
        d[v - 1][u - 1] = l

d = wf(d)

start = []
for i in range(m):
    if UVL[i][0] == 1:
        start.append(UVL[i])

ans = INF
for i in start:
    for j in start:
        if i != j and d[i[1] - 1][j[1] - 1] != INF:
            tmp = i[2] + j[2] + d[i[1] - 1][j[1] - 1]
            ans = min(ans, tmp)

if ans == INF:
    print(-1)
else:
Example #3
0
from itertools import permutations as pe
from scipy.sparse.csgraph import floyd_warshall as wf
n, m, r = map(int, input().split())
s = [[(0 if i == j else float("inf")) for j in range(n + 1)]
     for i in range(n + 1)]
g = list(pe(list(map(int, input().split()))))
for i in range(m):
    a, b, c = map(int, input().split())
    s[a][b] = c
    s[b][a] = c
s = wf(s, directed=False)
k = float("inf")
for i in range(len(g)):
    d = g[i]
    c = 0
    for j in range(1, len(d)):
        c += s[d[j - 1]][d[j]]
        if c >= k:
            break
    else:
        k = min(c, k)
print(int(k))
Example #4
0
import numpy as np
from scipy.sparse.csgraph import floyd_warshall as wf
from itertools import combinations
import sys
inp = sys.stdin.readline

x = 10**9
N, M = map(int, inp().split())
D = np.zeros(N) + x
K = np.zeros((N, N)) + x
for _ in range(M):
    u, v, l = map(int, inp().split())
    u -= 1
    v -= 1
    if (0 in (u, v)):
        D[u + v] = l
    else:
        K[u, v] = K[v, u] = l

K = wf(K)
r = min([D[i] + D[j] + K[i, j] for i, j in combinations(range(0, N), 2)])
print(int([-1, r][r < x]))
Example #5
0
from scipy.sparse.csgraph import floyd_warshall as wf
import itertools

N, M, R = map(int, input().split())
r = [int(i) for i in input().split()]

G = [[float('inf')] * N for _ in range(N)]
for i in range(N):
    G[i][i] = 0
for _ in range(M):
    i, j, k = map(int, input().split())
    G[i - 1][j - 1] = k
    G[j - 1][i - 1] = k
    
wf = wf(G)

ans = float('inf')
for perm in itertools.permutations(r):
    tmp = 0
    for i in range(len(perm) - 1):
        tmp += wf[perm[i] - 1][perm[i + 1] - 1]
    ans = min(ans, tmp)
    
print(int(ans))
Example #6
0
from scipy.sparse.csgraph import floyd_warshall as wf

H,W = map(int,input().split())
C = [[int(i) for i in input().split()] for i in range(10)]
A = [[int(i) for i in input().split()] for i in range(H)]
B = wf(C)
ans = 0

for i in range(H):
    for j in range(W):
        if A[i][j] == -1:
            continue
        ans += B[A[i][j]][1]

print(int(ans))
Example #7
0
n, m = map(int, input().split())
a = [[float("inf") if j != i else 0 for j in range(n + 1)]
     for i in range(n + 1)]
from scipy.sparse.csgraph import floyd_warshall as wf
c = []
d = []
for i in range(m):
    u, v, l = map(int, input().split())
    if u != 1 and v != 1:
        a[u][v] = l
        a[v][u] = l
    elif u == 1:
        c.append(v)
        d.append(l)
    else:
        c.append(u)
        d.append(l)
a = wf(a, directed=False)
l = float("inf")
for i in range(len(c) - 1):
    for j in range(i + 1, len(c)):
        l = min(l, d[i] + d[j] + a[c[i]][c[j]])
print(int(l) if l != float("inf") else -1)
Example #8
0
from itertools import combinations
from scipy.sparse.csgraph import floyd_warshall as wf

n, m = map(int, input().split())
inf = float('inf')
edges = [[inf] * n for j in range(n)]
for i in range(n):
    edges[i][i] = 0

s = []
for i in range(m):
    u, v, l = map(int, input().split())
    if u == 1:
        s.append((v - 1, l))
    elif v == 1:
        s.append((u - 1, l))
    else:
        edges[u - 1][v - 1] = l
        edges[v - 1][u - 1] = l

dist = wf(edges, directed=False)
ans = inf
for u, v in combinations(s, 2):
    d = dist[u[0]][v[0]]
    if d != inf:
        ans = min(ans, u[1] + v[1] + int(d))
if ans == inf:
    print(-1)
else:
    print(ans)