Cod sursa(job #3336356)

Utilizator stefan_15Salcianu Stefan Alexandru stefan_15 Data 24 ianuarie 2026 16:56:35
Problema Algoritmul Bellman-Ford Scor 0
Compilator py Status done
Runda Arhiva educationala Marime 0.96 kb










if __name__== "__main__":
    with open( "bellmanford.in","r") as f:
        n, m = map(int, f.readline().strip().split())
        muchii=[]
        for i in range(m):
            x, y,c = map(int, f.readline().strip().split())
            muchii.append((x,y,c))
        dist=[float("inf")] * (n+1)
        dist[1]=0
        for  _ in range(n-1):
            for x, y, c in muchii:
                if dist[x] != float("inf") and dist[x] + c<dist[y]:
                    dist[y]= dist[x]+ c
        ng_cycle=False
        for x, y, c in muchii:
                if dist[x] != float("inf") and dist[x] + c<dist[y]:
                    ng_cycle=True
                    break
        with open ("bellmanford.out", "w") as f:
            if ng_cycle:
                f.write("Ciclu negativ!")
            else:
                dist.pop(1)
                dist.pop(0)
                for el in dist:
                    f.write(str(el) + " ")