Problem p02949 (python)

Fixed submission:
import sys
import numpy as np
from scipy.sparse.csgraph import bellman_ford, connected_components
input = sys.stdin.readline


def main():
    n, m, p = map(int, input().split())
    
    l = np.zeros((n, n))
    for i in range(m):
        a, b, c =map(int, input().split())
        c = p-c
        if c == 0:
            c = 10**(-7.9)
        if l[a-1][b-1] == 0:
            l[a-1][b-1] = c
        else:
            l[a-1][b-1] = min(l[a-1][b-1], c)
    x = l[-1, 0]
    l[-1][0] = 10**(-7.9)
    n, labels = connected_components(l, connection="strong")
    l = l[labels == labels[0]][:, labels == labels[0]]
    l[-1, 0] = x
    
    try:
        print(max(0, int(round(bellman_ford(l, indices=0)[-1]))*(-1)))
    except:
        print(-1)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
from scipy.sparse.csgraph import bellman_ford
from scipy.sparse import csr_matrix
from collections import deque
input = sys.stdin.readline


def main():
    n, m, p = map(int, input().split())
    
    inf = 10**9
    l = np.zeros((n, n))
    tree_forth = [[] for _ in range(n)]
    tree_back = [[] for _ in range(n)]
    for i in range(m):
        a, b, c =map(int, input().split())
        tree_forth[a-1].append(b-1)
        tree_back[b-1].append(a-1)
        c = p-c
        if c == 0:
            c = 10**(-8)
        if l[a-1][b-1] == 0:
            l[a-1][b-1] = c
        else:
            l[a-1][b-1] = min(l[a-1][b-1], c)
    
    already1, already2 = [True]*n, [True]*n
    for already, tree, start in zip([already1, already2], [tree_forth, tree_back], [0, n-1]):
        not_yet = deque([start])
        already[start] = False
        while not_yet:
            key = not_yet.pop()
            for v in tree[key]:
                if already[v] == False:
                    continue
                already[v] = False
                not_yet.append(v)
    
    
    for i in range(n):
        if already1[i] or already2[i]:
            for j in range(n):
                l[i][j] = 0
                l[j][i] = 0

    print(2/0)
    
    try:
        print(max(0, int(bellman_ford(csr_matrix(l))[0][-1])*(-1)))
    except:
        print(-1)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
from collections import deque
input = sys.stdin.readline


def main():
    n, m, p = map(int, input().split())
    
    tree = [[] for _ in range(n)]
    tree_back = [[] for _ in range(n)]
    
    for i in range(m):
        a, b, c = map(int, input().split())
        tree[a-1].append((b-1, c))
        tree_back[b-1].append(a-1)
    
    not_yet = deque([n-1])
    already = [False]*n
    already[n-1] = True
    while not_yet:
        key = not_yet.pop()
        for v in tree_back[key]:
            if already[v]:
                continue
            already[v] = True
            not_yet.append(v)
    
    not_yet = deque([0])
    already2 = [False]*n
    already2[0] = True
    while not_yet:
        key = not_yet.pop()
        for (v, c) in tree[key]:
            if already2[v]:
                continue
            already2[v] = True
            not_yet.append(v)
    
    
    inf = 10**9
    adjoining_list = [[inf]*n for _ in range(n)]
    for i in range(n):
        if already[i] and already2[i]:
            for (v, c) in tree[i]:
                adjoining_list[i][v] = p-c
    
    adjoining_list = np.array(adjoining_list)
    try:
        ans = bellman_ford(csr_matrix(adjoining_list))
    except:
        print(-1)
    else:
        print(max(0, int(-ans[0][n-1])))


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
from collections import deque
input = sys.stdin.readline


def main():
    n, m, p = map(int, input().split())
    
    tree = [[] for _ in range(n)]
    tree_back = [[] for _ in range(n)]
    
    for i in range(m):
        a, b, c = map(int, input().split())
        tree[a-1].append((b-1, c))
        tree_back[b-1].append(a-1)
    
    not_yet = deque([n-1])
    already = [False]*n
    already[n-1] = True
    while not_yet:
        key = not_yet.pop()
        for v in tree_back[key]:
            if already[v]:
                continue
            already[v] = True
            not_yet.append(v)
    
    not_yet = deque([0])
    already2 = [False]*n
    already2[0] = True
    while not_yet:
        key = not_yet.pop()
        for (v, c) in tree[key]:
            if already2[v]:
                continue
            already2[v] = True
            not_yet.append(v)
    
    
    inf = 10**9
    adjoining_list = [[inf]*n for _ in range(n)]
    for i in range(n):
        if already[i] and already2[i]:
            for (v, c) in tree[i]:
                adjoining_list[i][v] = p-c
    
    adjoining_list = np.array(adjoining_list)
    try:
        ans = bellman_ford(csr_matrix(adjoining_list))
    except:
        print(-1)
    else:
        print(max(0, int(-ans[0][n-1])))


if __name__ == "__main__":
    main()