Problem p03450 (python)

Fixed submission:
from collections import deque

def bfs(x, graph, people):
    if people[x] is not None:
        return
    unvisited = deque([x])
    people[x] = 0
    while unvisited:
        now = unvisited.popleft()
        for next, d in graph[now]:
            if people[next] is None:
                people[next] =  people[now] + d
                unvisited.append(next)

def main():
    N, M = list(map(int, input().split()))

    graph = [ [] for n in range(N+1)]
    people = [None] * (N+1)

    for n in range(M):
        L, R, D = list(map(int, input().split()))
        graph[L].append((R, D))
        graph[R].append((L, -D))
    for i in range(1, N+1):
        bfs(i, graph, people)

    for i in range(1, N+1):
        for j, d in graph[i]:
            if people[j] - people[i] != d:
                print("No")
                exit()
    else:
        print("Yes")


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
l1 = list(map(int, input().split()))
N = l1[0]
M = l1[1]


per_dis = [ [] for n in range(N+1)]
people = [None] * (N+1)

for n in range(M):
    x = list(map(int, input().split()))
    per_dis[x[0]].append((x[1], x[2]))
    per_dis[x[1]].append((x[0], -x[2]))

if M != 0:
    visited = []
    unvisited = deque([(x[0], 0)])
    people[x[0]] = 0
    while unvisited:
        now = unvisited.popleft()
        for next in per_dis[now[0]]:
            if next[0] not in visited:
                people[next[0]] =  now[1] + next[1]
                unvisited.append((next[0], now[1] + next[1]))
                visited.append(now[0])

for i, x in enumerate(per_dis):
    if x != None:
        for y in x:
            if y[1] < 0:
                if people[y[0]] - people[i] != y[1]:
                    print("No")
                    sys.exit()
            else:
                if people[i] - people[y[0]] != -y[1]:
                    print("No")
                    sys.exit()


print("Yes")

Buggy submission (verdict: Runtime Error):
import sys                                                    
l1 = list(map(int, input().split()))                          
N = l1[0]                                                     
M = l1[1]                                                     
                                                              
                                                              
per_dis = [ [] for n in range(N+1)]                           
people = [None] * (N+1)                                       
                                                              
for n in range(M):                                            
    x = list(map(int, input().split()))                       
    per_dis[x[0]].append((x[1], x[2]))                        
    per_dis[x[1]].append((x[0], -x[2]))                       
                                                              
if M != 0:                                                    
    visited = []                                              
    unvisited = [(x[0], 0)]                                   
    people[x[0]] = 0                                          
    while unvisited:                                          
        now = unvisited[0]                                    
        unvisited = unvisited[1:]                             
        for next in per_dis[now[0]]:                          
            if next[0] not in visited:                        
                people[next[0]] =  now[1] + next[1]           
                unvisited.append((next[0], now[1] + next[1])) 
                visited.append(now[0])                        
                                                              
for i, x in enumerate(per_dis):                               
    if x != None:                                             
        for y in x:                                           
            if y[1] < 0:                                      
                if people[y[0]] - people[i] != y[1]:          
                    print("No")                               
                    sys.exit()                                
            else:                                             
                if people[i] - people[y[0]] != -y[1]:         
                    print("No")                               
                    sys.exit()                                
                                                              
                                                              
print("Yes")

Buggy submission (verdict: Runtime Error):
import sys                                                   
l1 = list(map(int, input().split()))                         
N = l1[0]                                                    
M = l1[1]                                                    
                                                             
dat = []                                                     
                                                             
per_dis = [ [] for n in range(N+1)]                          
                                                             
for n in range(M):                                           
    x = list(map(int, input().split()))                      
    dat.append(x)                                            
    per_dis[x[0]].append((x[1],x[2]))                        
    per_dis[x[1]].append((x[0],-x[2]))                       
                                                             
per_point = [ None for n in range(N+1)]                      
                                                             
if M != 0:                                                   
    visited = []                                             
    unvisited = [(dat[0][0], 0)]                             
    while unvisited:                                         
        now = unvisited[0]                                   
        unvisited = unvisited[1:]                            
        for next in per_dis[now[0]]:                         
            if next[0] not in visited:                       
                per_point[next[0]] = now[1] + next[1]        
                unvisited.append((next[0], now[1] + next[1]))
                visited.append(next[0])                      
                                                             
for x in dat:                                                
    if per_point[x[1]] - per_point[x[0]] != x[2]:            
        print("No")                                          
        sys.exit(0)                                          
print("Yes")

Buggy submission (verdict: Runtime Error):
import sys                                               
l1 = list(map(int, input().split()))                     
N = l1[0]                                                
M = l1[1]                                                
                                                         
dat = []                                                 
                                                         
per_dis = [ [] for n in range(N+1)]                      
                                                         
for n in range(M):                                       
    x = list(map(int, input().split()))                  
    dat.append(x)                                        
    per_dis[x[0]].append((x[1],x[2]))                    
    per_dis[x[1]].append((x[0],-x[2]))                   
                                                         
per_point = [ None for n in range(N+1)]                  
                                                         
visited = []                                             
unvisited = [(dat[0][0], 0)]                             
while unvisited:                                         
    now = unvisited[0]                                   
    unvisited = unvisited[1:]                            
    for next in per_dis[now[0]]:                         
        if next[0] not in visited:                       
            per_point[next[0]] = now[1] + next[1]        
            unvisited.append((next[0], now[1] + next[1]))
            visited.append(next[0])                      
                                                         
for x in dat:                                            
    if per_point[x[1]] - per_point[x[0]] != x[2]:        
        print("No")                                      
        sys.exit(0)                                      
print("Yes")

Buggy submission (verdict: Runtime Error):
 import sys                                                                                   
 l1 = list(map(int, input().split()))                                                         
 N = l1[0]                                                                                    
 M = l1[1]                                                                                    
                                                                                              
 dat = []                                                                                     
                                                                                              
 #per_dis = [[ 0 for n in range(N+1)] for n in range(N+1)]                                    
                                                                                              
 per_dis = [ [] for n in range(N+1)]                                                          
                                                                                              
 for n in range(M):                                                                           
     x = list(map(int, input().split()))                                                      
     dat.append(x)                                                                            
                                                                                              
                                                                                              
 for x in dat:                                                                                
     if per_dis[x[0]][x[1]] == 0 and per_dis[x[1]][x[0]] == 0:                                
         per_dis[x[0]][x[1]] = x[2]                                                           
         per_dis[x[1]][x[0]] = -x[2]                                                          
         for i in range(1,N):                                                                 
             for j in range(i+1,N):                                                           
                 if per_dis[i][j] != 0 and per_dis[i+1][j+1] != 0 and per_dis[i][j+1] == 0:   
                     per_dis[i][j+1] = per_dis[i][j] + per_dis[i+1][j+1]                      
                     per_dis[j+1][i] = -per_dis[i][j+1]                                       
                 elif per_dis[i][j] == 0 and per_dis[i+1][j+1] != 0 and per_dis[i][j+1] != 0: 
                     per_dis[i][j] = per_dis[i][j+1] - per_dis[i+1][j+1]                      
                     per_dis[j][i] = -per_dis[i][j]                                           
                 elif per_dis[i][j] != 0 and per_dis[i+1][j+1] == 0 and per_dis[i][j+1] != 0: 
                     per_dis[i+1][j+1] = per_dis[i][j+1] - per_dis[i][j]                      
                     per_dis[j+1][i+1] = -per_dis[i+1][j+1]                                   
     else:                                                                                    
         if abs(per_dis[x[0]][x[1]]) != x[2]:                                                 
             print("No")                                                                      
             sys.exit(0)                                                                      
                                                                                              
 print("Yes")