Problem p03452 (python)

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline

def bfs(graph, start):
    while graph != {}:
        start = min(graph.keys())
        queue = deque([])
        queue.append(start)
        pos = {}
        pos[start] = 0
        while queue:
            u = queue.popleft()
            for v,d in graph[u].items():
                if v not in pos:
                    pos[v] = pos[u] + d
                    if v in graph:
                        queue.append(v)
                elif pos[v] != pos[u] + d:
                    return False
            del graph[u]
            # print(queue)
    return True

n,m = map(int, input().split())

graph = {}
for i in range(m):
    l,r,d = map(int, input().split())
    if l not in graph:
        graph[l] = {}
    graph[l][r] = d
    if r not in graph:
        graph[r] = {}
    graph[r][l] = -d

if m == 0 or bfs(graph, min(graph.keys())):
    print('Yes')
else:
    print('No')
# print(graph)

Buggy submission (verdict: Runtime Error):
from collections import deque
import sys
input = sys.stdin.readline

def bfs(graph, start):
    while graph != {}:
        start = min(graph.keys())
        queue = deque([])
        queue.append(start)
        pos = {}
        pos[start] = 0

        while queue:
            u = queue.popleft()
            if u not in graph:
                continue
            for v,d in graph[u].items():
                if v not in pos:
                    pos[v] = pos[u] + d
                    queue.append(v)
                elif pos[v] != pos[u] + d:
                    return False
            del graph[u]
            # print(queue)
        return True

n,m = map(int, input().split())

graph = {}
for i in range(m):
    l,r,d = map(int, input().split())
    if l not in graph:
        graph[l] = {}
    graph[l][r] = d
    if r not in graph:
        graph[r] = {}
    graph[r][l] = -d

if bfs(graph, min(graph.keys())):
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
from collections import deque
import sys
input = sys.stdin.readline

def bfs(graph, start):
    queue = deque([])
    queue.append(start)
    pos = {}
    pos[start] = 0

    while queue:
        u = queue.popleft()
        if u not in graph:
            if graph != {}
                u = min(graph.keys())
                pos = {u:0}
            else:
                continue
        for v,d in graph[u].items():
            if v not in pos:
                pos[v] = pos[u] + d
                queue.append(v)
            elif pos[v] != pos[u] + d:
                return False
        del graph[u]
        # print(queue)
    return True

n,m = map(int, input().split())

graph = {}
for i in range(m):
    l,r,d = map(int, input().split())
    if l not in graph:
        graph[l] = {}
    graph[l][r] = d
    if r not in graph:
        graph[r] = {}
    graph[r][l] = -d

if m == 0 or bfs(graph, min(graph.keys())):
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
from collections import deque
import sys
input = sys.stdin.readline

def bfs(graph, out, start):
    queue = deque([])
    queue.append(start)
    pos[start] = 0
    dis = {0:start}
    mx = 0
    mn = 0
    mod = 10**9

    while queue:
        u = queue.popleft()
        if u not in graph:
            if graph != {}:
                u = min(graph.keys())
            else:
                continue
        for v,d in graph[u].items():
            if v not in pos:
                pos[v] = pos[u] + d
                dis[pos[u] + d] = v
                queue.append(v)
                if pos[v]> mx:
                    mx = pos[v]
                    if mx - mn > mod:
                        return False
            elif pos[v] != pos[u] + d:
                return False
        del graph[u]
        if u not in out:
            continue
        for v,d in out[u].items():
            if v not in pos:
                pos[v] = pos[u] + v
                queue.append(v)
                if pos[v] < mn:
                    mn = pos[v]
                    if mx - mn > mod:
                        return False
            elif pos[v] != pos[u] + d:
                return False
        # print(queue)
    return True

n,m = map(int, input().split())

graph = {}
out = {}
for i in range(m):
    l,r,d = map(int, input().split())
    if l not in graph:
        graph[l] = {}
    graph[l][r] = d
    if r not in out:
        out[r] = {}
    out[r][l] = -d

pos = {}
if m == 0 or bfs(graph, out, min(graph.keys())):
    print('Yes')
else:
    print('No')

# print(graph)
# print(pos)