Загрузка данных


d = {}
for line in open('23_31566.txt'):
    l, m, w = line.split()
    d[int(l)] = d.get(int(l), []) + [(int(m), float(w))]

@lru_cache(None)
def f(v):
    if v == 8318:
        return 0
    if v not in d:
        return 10**10
    return min(1 + f(to) for to, w in d[v])

print(int(f(402)))

from functools import *

d = {}
for line in open('23_31577.txt'):
    l, m, w = line.split()
    d[int(l)] = d.get(int(l), []) + [(int(m), float(w))]

@lru_cache(None)
def f(v):
    if v == 82:
        return 0
    if v not in d:
        return -10**10
    return max(1 + f(to) for to, w in d[v])

print(int(f(15)))

from functools import *

d = {}
for s in open('23_31567.txt'):
    l, m, w = map(float, s.split())
    d[int(l)] = d.get(int(l), []) + [[int(m), float(w)]]

@lru_cache(None)
def f(v, k):
    if v == k:
        return (0, 0.0)
    if v not in d:
        return (10**10, 10**10)
    return min((x[1] + f(x[0], k)[0], f(x[0], k)[1] + 1) for x in d[v])

print(f(102, 2616)[1])