Problem p02949 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**5)


def dfs(s):
    for i in to[s]:
        if not toflag[i]:
            toflag[i]=True
            dfs(i)


def rdfs(s):
    for i in ot[s]:
        if not otflag[i]:
            otflag[i]=True
            rdfs(i)


def bellman_ford():
    for i in range(n):
        upd=False
        for a,b,c in edges:
            if d[b]>d[a]+c:
                upd=True
                d[b]=d[a]+c     
        if not upd:
            break
        if i == n-1:
            print(-1)
            exit()


def main():

    for i in range(m):
        a,b,c=map(lambda x:int(x)-1,input().split())
        edges.append([a,b,-(c+1-p)])
        to[a].append(b)
        ot[b].append(a)

    toflag[0]=True
    otflag[n-1]=True
    dfs(0) # check for 0 to i path
    rdfs(n-1) # check for n-1 to i path
 
    for i in range(m):
        a,b,c=edges[i]
        if toflag[a] and otflag[a] and toflag[b] and otflag[b]: continue;
        edges[i]=[0,0,0]

    d[0]=0
    bellman_ford()
    print(max(0,-d[-1]))



if __name__=='__main__':
    n,m,p=map(int,input().split())
    edges=[]
    to=[[] for i in range(n)]
    ot=[[] for i in range(n)]
    toflag=[False]*n
    otflag=[False]*n
    d=[10**9]*n
    main()

Buggy submission (verdict: Runtime Error):
from numpy.core.numeric import indices
from scipy.sparse.csgraph import bellman_ford, NegativeCycleError
import numpy as np

def dfs(s):
    for i in to[s]:
        if not toflag[i]:
            toflag[i]=True
            dfs(i)


def rdfs(s):
    for i in ot[s]:
        if not otflag[i]:
            otflag[i]=True
            rdfs(i)


def main():

    for i in range(m):
        a,b,c=map(lambda x:int(x)-1,input().split())
        g[a][b]=min(g[a][b], -(c+1-p))
        to[a].append(b)
        ot[b].append(a)

    toflag[0]=True
    otflag[n-1]=True
    dfs(0) # check for 0 to i path
    rdfs(n-1) # check for n-1 to i path
 
    for i in range(n):
        for j in range(n):
            if toflag[i] and otflag[i] and toflag[j] and otflag[j]: continue;
            g[i][j]=0

    try:
        d=bellman_ford(g, indices=[0]).astype(int)
        print(max(-d[0][-1], 0))
    except NegativeCycleError:
        print(-1)



if __name__=='__main__':
    n,m,p=map(int,input().split())
    g=np.array([[0]*n for i in range(n)])
    to=[[] for i in range(n)]
    ot=[[] for i in range(n)]
    toflag=[False]*n
    otflag=[False]*n
    main()

Buggy submission (verdict: Runtime Error):
def dfs(s):
    for i in to[s]:
        if not toflag[i]:
            toflag[i]=True
            dfs(i)


def rdfs(s):
    for i in ot[s]:
        if not otflag[i]:
            otflag[i]=True
            rdfs(i)


def bellman_ford():
    for i in range(n):
        upd=False
        for a,b,c in edges:
            if d[b]>d[a]+c:
                upd=True
                d[b]=d[a]+c     
        if not upd:
            break
        if i == n-1:
            print(-1)
            exit()


def main():

    for i in range(m):
        a,b,c=map(lambda x:int(x)-1,input().split())
        edges.append([a,b,-(c+1-p)])
        to[a].append(b)
        ot[b].append(a)

    toflag[0]=True
    otflag[n-1]=True
    dfs(0) # check for 0 to i path
    rdfs(n-1) # check for n-1 to i path
 
    for i in range(m):
        a,b,c=edges[i]
        if toflag[a] and otflag[a] and toflag[b] and otflag[b]: continue;
        edges[i]=[0,0,0]

    d[0]=0
    bellman_ford()
    print(max(0,-d[-1]))



if __name__=='__main__':
    n,m,p=map(int,input().split())
    edges=[]
    to=[[] for i in range(n)]
    ot=[[] for i in range(n)]
    toflag=[False]*n
    otflag=[False]*n
    d=[10**9]*n
    main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**3)


def dfs(s,v=0,c=0):
  global ans
  if s==n-1:
    ans=max(ans, v-p*c)
  for i in range(n):
    if not to[s][i]: continue;
    if s==i:
      if p<to[s][i] and i==n-1: print(-1); exit()
      else: continue;
    dfs(i,v+to[s][i],c+1)
  #return ans


def main():
  for i in range(m):
    a,b,c=map(lambda x:int(x)-1,input().split())
    to[a][b]=c+1
  #print(to)
  dfs(0)
  print(max(0,ans))
  

if __name__=='__main__':
  n,m,p=map(int,input().split())
  to=[[0]*n for i in range(n)]
  ans=0
  main()