Cod sursa(job #3370866)

Utilizator AnlegliftIvanus Victor Gabriel Anleglift Data 27 octombrie 2026 16:38:36
Problema Sate Scor 0
Compilator py Status done
Runda Arhiva de probleme Marime 1.04 kb
from collections import deque


def Solve():
    graph = {}
    with open("sate.in", "r") as file:
        n, m, x, y = [int(x.strip()) for x in file.readline().strip().split(" ")]

        graph = [[] for _ in range(n + 1)]
        for i in range(m):
            i, j, d = [int(x.strip()) for x in file.readline().strip().split(" ")]

            graph[i].append((j, d))
            graph[j].append((i, -d))

    queue = deque()
    queue.append((x, 0))

    visited = [0] * (n + 1)
    visited[x] = 1

    while queue:
        candidate, distance = queue.popleft()

        for neighbour in graph[candidate]:
            if visited[neighbour[0]] == 0:
                if neighbour[0] == y:
                    with open("sate.out", "w") as file:
                        file.write(str(abs(neighbour[1] + distance)))
                    return
                else:
                    queue.append((neighbour[0], distance + neighbour[1]))
                    visited[neighbour[0]] = 1


if __name__ == "__main__":
    Solve()