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 numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix

def get_path(start, goal, pred):
    return get_path_row(start, goal, pred[start])

def get_path_row(start, goal, pred_row):
    path = []
    i = goal
    while i != start and i >= 0:
        path.append(i)
        i = pred_row[i]
    if i < 0:
        return []
    path.append(i)
    return path[::-1]

N, M, P = map(int, input().split())
INF = 10**18
graph = np.array([[0]*N for _ in range(N)])
negative_V = []
for i in range(M):
  from_V, to_V, c = map(int, input().split())
  if from_V==to_V: 
    if to_V == N: 
      print(-1)
      exit()    
    negative_V.append(from_V-1)
    
  graph[from_V-1][to_V-1] = P-c

for remove in negative_V[::-1]:
  graph = np.delete(np.delete(graph, remove, 0), remove, 1)

csr = csr_matrix(graph) 


try:
  	
    
    dist = bellman_ford(csr, indices=[0])
except Exception: 
    print(-1)
else:    
  print(max(-(dist[0][-1]),0))
  

import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix

def get_path(start, goal, pred):
    return get_path_row(start, goal, pred[start])

def get_path_row(start, goal, pred_row):
    path = []
    i = goal
    while i != start and i >= 0:
        path.append(i)
        i = pred_row[i]
    if i < 0:
        return []
    path.append(i)
    return path[::-1]

N, M, P = map(int, input().split())

graph = [[0]*N for _ in range(N)]
negative_cnt = 0
for i in range(M):
  from_V, to_V, c = map(int, input().split())
  if from_V==to_V: 
    if to_V == N: 
      print(-1)
      exit()
    graph[from_V-1][to_V-1] = float('inf')
    continue    
  graph[from_V-1][to_V-1] = P-c
a = csr_matrix(graph) 


try:
  	
    
    dist = bellman_ford(a, indices=[0])
except Exception: 
    print(-1)
else:    
  print(max(-(dist[0][-1]),0))
  

import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix

def get_path(start, goal, pred):
    return get_path_row(start, goal, pred[start])

def get_path_row(start, goal, pred_row):
    path = []
    i = goal
    while i != start and i >= 0:
        path.append(i)
        i = pred_row[i]
    if i < 0:
        return []
    path.append(i)
    return path[::-1]

N, M, P = map(int, input().split())

graph = [[0]*N for _ in range(N)]
negative_cnt = 0
for i in range(M):
  from_V, to_V, c = map(int, input().split())
  if from_V==to_V: 
    if to_V == N: 
      print(-1)
      exit()    
    continue    
  graph[from_V-1][to_V-1] = P-c
a = csr_matrix(graph) 


try:
  	
    
    dist = bellman_ford(a, indices=[0])
except Exception: 
    print(-1)
else:    
  print(max(-(dist[0][-1]),0))
  


def reachable_nodeset(start, inc):
  reachable = set()
  reachable.add(start)
  nodes = [start]
  
  while nodes:
    n = nodes.pop()
    for i in inc[n]:
      if i not in reachable:
        nodes.append(i)
        reachable.add(i)
  return reachable

def bellmanford(num, start, goal, edges):
  cost = [float('inf')]*num
  cost[start] = 0
  
  for _ in range(num):
    updated = False
    for a, b, c in edges:
      
      if cost[b] > cost[a]+c:
        cost[b] = cost[a]+c
        updated = True
    if not updated: break
  else:
    
    return -1
  return max(0, -cost[goal])

N, M, P = map(int, input().split())
to = [[] for _ in range(N)]
ot = [[] for _ in range(N)]
edges = []

for _ in range(M):
  a, b, c = map(int, input().split())
  a -= 1
  b -= 1
  c = -(c-P)
  
  to[a].append(b)
  ot[b].append(a)
  edges.append((a,b,c))
  
reachableFromZero = reachable_nodeset(0, to)  
reachableToN = reachable_nodeset(N-1, ot)  
ok = reachableFromZero.intersection(reachableToN)
edges = tuple((a,b,c) for a,b,c in edges if a in ok and b in ok)

print(bellmanford(N, 0, N-1, edges))

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted