Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N, M = map(int, input().split())
E = [[] for _ in range(N)]
RE = [[] for _ in range(N)]
S = [0] * N
for _ in range(M):
  l, r, d = map(int, input().split())
  S[l - 1] = 1
  E[l - 1].append([d, r - 1])
  RE[r - 1].append([d, l - 1])
D = [None] * N
def dijkstra(i):
  D[i] = 0
  Q = [[0, i]]
  while Q:
    d, s = heapq.heappop(Q)
    for dist, adj in E[s]:
      if D[adj] == None:
        D[adj] = D[s] + dist
        heapq.heappush(Q, [D[adj], adj])
      elif D[adj] == 'Error' or D[adj] != D[s] + dist:
        D[s] = 'Error'
        break
    else:
      for dist, adj in RE[s]:
        if D[adj] == None:
          D[adj] = D[s] - dist
          heapq.heappush(Q, [D[adj], adj])
        elif D[adj] == 'Error' or D[adj] != D[s] - dist:
          D[s] = 'Error'
          break
      else:
        continue
    break
for i in range(N):
  if D[i] == 'Error':
    print('No')
    break
  elif D[i] == None:
    dijkstra(i)
else:
  for d in D:
    if d == 'Error':
      print('No')
      break
  else:
    print('Yes')

Fixed submission:
import heapq
N, M = map(int, input().split())
E = [[] for _ in range(N)]
RE = [[] for _ in range(N)]
S = [0] * N
for _ in range(M):
  l, r, d = map(int, input().split())
  S[l - 1] = 1
  E[l - 1].append([d, r - 1])
  RE[r - 1].append([d, l - 1])
D = [None] * N
def dijkstra(i):
  D[i] = 0
  Q = [[0, i]]
  while Q:
    d, s = heapq.heappop(Q)
    for dist, adj in E[s]:
      if D[adj] == None:
        D[adj] = D[s] + dist
        heapq.heappush(Q, [D[adj], adj])
      elif D[adj] == 'Error' or D[adj] != D[s] + dist:
        D[s] = 'Error'
        break
    else:
      for dist, adj in RE[s]:
        if D[adj] == None:
          D[adj] = D[s] - dist
          heapq.heappush(Q, [D[adj], adj])
        elif D[adj] == 'Error' or D[adj] != D[s] - dist:
          D[s] = 'Error'
          break
      else:
        continue
    break
for i in range(N):
  if D[i] == 'Error':
    print('No')
    break
  elif D[i] == None:
    dijkstra(i)
else:
  for d in D:
    if d == 'Error':
      print('No')
      break
  else:
    print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N, M = map(int, input().split())
S = [False] * N
G = [[] for i in range(N)]
def root(i):
  if R[i] == i:
    return i
  else:
    R[i] = root(R[i])
    return R[i]
loop = False
for i in range(M):
  l, r, d = map(int, input().split())
  G[l - 1].append([r - 1, d])
for i in range(N):
  if not S[i] and G[i]:
    _X = [None for i in range(N)]
    _X[i] = 0
    Q = []
    heapq.heappush(Q, [0, i])
    contract = False
    while Q:
      s = heapq.heappop(Q)
      S[s[1]] = True
      for adj, d in G[s[1]]:
        if _X[adj] == None:
          _X[adj] = s[0] + d
          if _X[adj] <= 10 ** 9:
            heapq.heappush(Q, [_X[adj], adj])
          else:
            contract = True
            break
        elif _X[adj] != s[0] + d:
          contract = True
          break
      if contract:
        break
    if contract:
      print('No')
      break
else:
  print('Yes')

Fixed submission:
import heapq
N, M = map(int, input().split())
E = [[] for _ in range(N)]
RE = [[] for _ in range(N)]
S = [0] * N
for _ in range(M):
  l, r, d = map(int, input().split())
  S[l - 1] = 1
  E[l - 1].append([d, r - 1])
  RE[r - 1].append([d, l - 1])
D = [None] * N
def dijkstra(i):
  D[i] = 0
  Q = [[0, i]]
  while Q:
    d, s = heapq.heappop(Q)
    for dist, adj in E[s]:
      if D[adj] == None:
        D[adj] = D[s] + dist
        heapq.heappush(Q, [D[adj], adj])
      elif D[adj] == 'Error' or D[adj] != D[s] + dist:
        D[s] = 'Error'
        break
    else:
      for dist, adj in RE[s]:
        if D[adj] == None:
          D[adj] = D[s] - dist
          heapq.heappush(Q, [D[adj], adj])
        elif D[adj] == 'Error' or D[adj] != D[s] - dist:
          D[s] = 'Error'
          break
      else:
        continue
    break
for i in range(N):
  if D[i] == 'Error':
    print('No')
    break
  elif D[i] == None:
    dijkstra(i)
else:
  for d in D:
    if d == 'Error':
      print('No')
      break
  else:
    print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N, M = map(int, input().split())
S = [False] * N
G = [[] for i in range(N)]
def root(i):
  if R[i] == i:
    return i
  else:
    R[i] = root(R[i])
    return R[i]
loop = False
for i in range(M):
  l, r, d = map(int, input().split())
  G[l - 1].append([r - 1, d])
for i in range(N):
  if not S[i]:
    _X = [None for i in range(N)]
    _X[i] = 0
    Q = []
    heapq.heappush(Q, [0, i])
    contract = False
    while Q:
      s = heapq.heappop(Q)
      S[s[1]] = True
      for adj, d in G[s[1]]:
        if _X[adj] == None:
          _X[adj] = s[0] + d
          if _X[adj] <= 10 ** 9:
            heapq.heappush(Q, [_X[adj], adj])
          else:
            contract = True
            break
        elif _X[adj] != s[0] + d:
          contract = True
          break
      if contract:
        break
    if contract:
      print('No')
      break
else:
  print('Yes')

Fixed submission:
import heapq
N, M = map(int, input().split())
E = [[] for _ in range(N)]
RE = [[] for _ in range(N)]
S = [0] * N
for _ in range(M):
  l, r, d = map(int, input().split())
  S[l - 1] = 1
  E[l - 1].append([d, r - 1])
  RE[r - 1].append([d, l - 1])
D = [None] * N
def dijkstra(i):
  D[i] = 0
  Q = [[0, i]]
  while Q:
    d, s = heapq.heappop(Q)
    for dist, adj in E[s]:
      if D[adj] == None:
        D[adj] = D[s] + dist
        heapq.heappush(Q, [D[adj], adj])
      elif D[adj] == 'Error' or D[adj] != D[s] + dist:
        D[s] = 'Error'
        break
    else:
      for dist, adj in RE[s]:
        if D[adj] == None:
          D[adj] = D[s] - dist
          heapq.heappush(Q, [D[adj], adj])
        elif D[adj] == 'Error' or D[adj] != D[s] - dist:
          D[s] = 'Error'
          break
      else:
        continue
    break
for i in range(N):
  if D[i] == 'Error':
    print('No')
    break
  elif D[i] == None:
    dijkstra(i)
else:
  for d in D:
    if d == 'Error':
      print('No')
      break
  else:
    print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
X = [-1] * N
E = [[] for i in range(N)]
I = [0] * N
for i in range(M):
  L, R, D = map(int, input().split())
  E[L - 1].append([R - 1, D])
  X[R - 1] = -2
  I[R - 1] += 1
ans = True
for i in range(N):
  if X[i] == -1:
    _X = [x for x in X]
    _X[i] = 0
    Q = [i]
    while Q:
      s = Q[0]
      Q = Q[1:]
      X[s] = -3
      for e, d in E[s]:
        if _X[e] <= -2 and _X[s] + d <= 10 ** 9:
          _X[e] = _X[s] + d
          Q.append(e)
        elif _X[e] != _X[s] + d:
          ans = False
          break
    if not ans:
      break
for i in range(N):
  if X[i] == -2 and I[i]:
    ans = False
    break
print('Yes' if ans else 'No')

Fixed submission:
import heapq
N, M = map(int, input().split())
E = [[] for _ in range(N)]
RE = [[] for _ in range(N)]
S = [0] * N
for _ in range(M):
  l, r, d = map(int, input().split())
  S[l - 1] = 1
  E[l - 1].append([d, r - 1])
  RE[r - 1].append([d, l - 1])
D = [None] * N
def dijkstra(i):
  D[i] = 0
  Q = [[0, i]]
  while Q:
    d, s = heapq.heappop(Q)
    for dist, adj in E[s]:
      if D[adj] == None:
        D[adj] = D[s] + dist
        heapq.heappush(Q, [D[adj], adj])
      elif D[adj] == 'Error' or D[adj] != D[s] + dist:
        D[s] = 'Error'
        break
    else:
      for dist, adj in RE[s]:
        if D[adj] == None:
          D[adj] = D[s] - dist
          heapq.heappush(Q, [D[adj], adj])
        elif D[adj] == 'Error' or D[adj] != D[s] - dist:
          D[s] = 'Error'
          break
      else:
        continue
    break
for i in range(N):
  if D[i] == 'Error':
    print('No')
    break
  elif D[i] == None:
    dijkstra(i)
else:
  for d in D:
    if d == 'Error':
      print('No')
      break
  else:
    print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
X = [-1] * N
E = [[] for i in range(N)]
I = [0] * N
for i in range(M):
  L, R, D = map(int, input().split())
  E[L - 1].append([R - 1, D])
  X[R - 1] = -2
  I[R - 1] += 1
ans = True
for i in range(N):
  if X[i] == -1:
    _X = [x for x in X]
    _X[i] = 0
    Q = [i]
    while Q:
      s = Q[0]
      Q = Q[1:]
      X[s] = -3
      for e, d in E[s]:
        if _X[e] <= -2 and _X[s] + d <= 10 ** 9:
          _X[e] = _X[s] + d
          Q.append(e)
        elif _X[e] != _X[s] + d:
          ans = False
          break
    if not ans:
      break
for i in range(N):
  if X[i] == -2 and I[i]:
    ans = False
    break
print('Yes' if ans else 'No')

Fixed submission:
import heapq
N, M = map(int, input().split())
E = [[] for _ in range(N)]
RE = [[] for _ in range(N)]
S = [0] * N
for _ in range(M):
  l, r, d = map(int, input().split())
  S[l - 1] = 1
  E[l - 1].append([d, r - 1])
  RE[r - 1].append([d, l - 1])
D = [None] * N
def dijkstra(i):
  D[i] = 0
  Q = [[0, i]]
  while Q:
    d, s = heapq.heappop(Q)
    for dist, adj in E[s]:
      if D[adj] == None:
        D[adj] = D[s] + dist
        heapq.heappush(Q, [D[adj], adj])
      elif D[adj] == 'Error' or D[adj] != D[s] + dist:
        D[s] = 'Error'
        break
    else:
      for dist, adj in RE[s]:
        if D[adj] == None:
          D[adj] = D[s] - dist
          heapq.heappush(Q, [D[adj], adj])
        elif D[adj] == 'Error' or D[adj] != D[s] - dist:
          D[s] = 'Error'
          break
      else:
        continue
    break
for i in range(N):
  if D[i] == 'Error':
    print('No')
    break
  elif D[i] == None:
    dijkstra(i)
else:
  for d in D:
    if d == 'Error':
      print('No')
      break
  else:
    print('Yes')