Problem: There is a directed graph with N vertices numbered 1 to N and M edges. The i-th edge is directed from Vertex A_i to Vertex B_i, and there are C_i coins placed along that edge. Additionally, there is a button on Vertex N.  We will play a game on this graph. You start the game on Vertex 1 with zero coins, and head for Vertex N by traversing the edges while collecting coins. It takes one minute to traverse an edge, and you can collect the coins placed along the edge each time you traverse it. As usual in games, even if you traverse an edge once and collect the coins, the same number of coins will reappear next time you traverse that edge, which you can collect again.  When you reach Vertex N, you can end the game by pressing the button. (You can also choose to leave Vertex N without pressing the button and continue traveling.) However, when you end the game, you will be asked to pay T \times P coins, where T is the number of minutes elapsed since the start of the game. If you have less than T \times P coins, you will have to pay all of your coins instead.  Your score will be the number of coins you have after this payment. Determine if there exists a maximum value of the score that can be obtained. If the answer is yes, find that maximum value.

Submission attempts (in order):
import sys
import numpy as np
from scipy.sparse.csgraph import bellman_ford, connected_components
from scipy.sparse.csgraph._shortest_path import NegativeCycleError

input = sys.stdin.buffer.readline
epsilon = 2 ** -26 # > 10 ** -8, which is the smallest cost scipy's bellman_ford can deal with. 

N, M, P = map(int, input().split())
graph = np.zeros((N, N), np.float64)
for _ in range(M):
    a, b, c = map(int, input().split())
    if graph[a-1][b-1] == 0:
        if -c+P == 0:
            graph[a-1][b-1] = epsilon
        else:
            graph[a-1][b-1] = -c+P
    elif graph[a-1][b-1] > -c+P:
        if -c+P == 0:
            graph[a-1][b-1] = epsilon
        else:
            graph[a-1][b-1] = -c+P

print(graph)

tmp = graph[-1, 0]
#connect N to 1
graph[-1, 0] = epsilon
_, labels = connected_components(graph, connection = "strong")
mask = (labels == labels[0])

#recover original N to 1 path.
graph[-1, 0] = tmp
graph = graph[mask, :][:, mask]

try:
    ans = bellman_ford(graph, directed = True, indices = 0)[-1]
    ans = max(0, int(round(-ans)))
except NegativeCycleError:
    ans = -1

print(ans)


import sys
import numpy as np
from scipy.sparse.csgraph import bellman_ford, connected_components
from scipy.sparse.csgraph._shortest_path import NegativeCycleError

input = sys.stdin.buffer.readline
epsilon = 2 ** -26 # > 10 ** -8, which is the smallest cost scipy's bellman_ford can deal with. 

N, M, P = map(int, input().split())
graph = np.zeros((N, N), np.float64)
for _ in range(M):
    a, b, c = map(int, input().split())
    if graph[a-1][b-1] == 0:
        if -c+P == 0:
            graph[a-1][b-1] = epsilon
        else:
            graph[a-1][b-1] = -c+P
    elif graph[a-1][b-1] > -c+P:
        if -c+P == 0:
            graph[a-1][b-1] = epsilon
        else:
            graph[a-1][b-1] = -c+P

tmp = graph[-1, 0]
#connect N to 1
graph[-1, 0] = epsilon
_, labels = connected_components(graph, connection = "strong")
mask = (labels == labels[0])

#recover original N to 1 path.
graph[-1, 0] = tmp
graph = graph[mask, :][:, mask]

try:
    ans = bellman_ford(graph, directed = True, indices = 0)[-1]
    ans = max(0, int(round(-ans)))
except NegativeCycleError:
    ans = -1

print(ans)


Verdicts: Wrong Answer

Accepted