Problem p03450 (python)

Fixed submission:
from collections import deque
INFTY = 10**10
N,M = map(int,input().split())
ind = 2
if N==1:
    ind = 0
else:
    G = {}
    for _ in range(M):
        L,R,D = map(int,input().split())
        if L not in G:
            G[L]=[]
        G[L].append((R,D))
        if R not in G:
            G[R] = []
        G[R].append((L,-D))
    dist = [INFTY for _ in range(N+1)]
    hist = [0 for _ in range(N+1)]
    for i in G:
        ind = 2
        heap = deque([i])
        while heap:
            cur = heap.popleft()
            if hist[cur]==0:
                dist[cur] = 0
                hist[cur]=1
                ind = 0
            elif hist[cur]==1 and ind==2:
                break
            for x in G[cur]:
                v,w = x[0],x[1]
                if hist[v]==0:
                    dist[v] = dist[cur]+w
                    heap.append(v)
                    hist[v]=1
                else:
                    if dist[v]!=dist[cur]+w:
                        ind = 1
                        break
            if ind==1:break
        if ind==1:break
if ind!=1:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
INFTY = 10**10
A = sorted([list(map(int,input().split())) for _ in range(M)],key=lambda x:x[0])
hist = [0 for _ in range(N)]
dist = [-INFTY for _ in range(N)]
hist[A[0][0]]=1
dist[A[0][0]] = 0
hist[A[0][1]]=1
dist[A[0][1]] = dist[A[0][0]]+A[0][2]
flag = 0
for j in range(1,M):
    ind = A[i][0]
    ind2 = A[i][1]
    if hist[ind]==1:
        if hist[ind2]==1:
            if dist[ind2]==dist[ind]+A[i][2]:
                continue
            else:
                flag = 1
                break
        else:
            dist[ind2] = dist[ind]+A[i][2]
            hist[ind2] = 1
    else:
        if hist[ind2]==1:
            dist[ind] = dist[ind2]-A[i][2]
            hist[ind]=1
        else:
            dist[ind] = 0
            hist[ind] = 1
            dist[ind2] = A[i][2]
            hist[ind2]=1
if flag==0:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
from collections import deque
INFTY = 10**10
N,M = map(int,input().split())
if N==1:
    ind = 0
else:
    G = {}
    for _ in range(M):
        L,R,D = map(int,input().split())
        if L not in G:
            G[L]=[]
        G[L].append((R,D))
        if R not in G:
            G[R] = []
        G[R].append((L,-D))
    dist = [INFTY for _ in range(N+1)]
    hist = [0 for _ in range(N+1)]
    for i in G:
        heap = deque([i])
        ind = 2
        while heap:
            cur = heap.popleft()
            if hist[cur]==0:
                dist[cur] = 0
                hist[cur]=1
                ind = 0
            elif hist[cur]==1 and ind==2:
                break
            for x in G[cur]:
                v,w = x[0],x[1]
                if hist[v]==0:
                    dist[v] = dist[cur]+w
                    heap.append(v)
                    hist[v]=1
                else:
                    if dist[v]!=dist[cur]+w:
                        ind = 1
                        break
            if ind==1:break
        if ind==1:break
if ind!=1:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
from collections import deque
INFTY = 10**10
N,M = map(int,input().split())
G = {}
for _ in range(M):
    L,R,D = map(int,input().split())
    if L not in G:
        G[L]=[]
    G[L].append((R,D))
    if R not in G:
        G[R] = []
    G[R].append((L,-D))
dist = [INFTY for _ in range(N+1)]
hist = [0 for _ in range(N+1)]
for i in G:
    heap = deque([i])
    ind = 2
    while heap:
        cur = heap.popleft()
        if hist[cur]==0:
            dist[cur] = 0
            hist[cur]=1
            ind = 0
        elif hist[cur]==1 and ind==2:
            break
        for x in G[cur]:
            v,w = x[0],x[1]
            if hist[v]==0:
                dist[v] = dist[cur]+w
                heap.append(v)
                hist[v]=1
            else:
                if dist[v]!=dist[cur]+w:
                    ind = 1
                    break
        if ind==1:break
    if ind==1:break
if ind!=1:
    print("Yes")
else:
    print("No")