Problem p03722 (python)

Fixed submission:
INF = float('inf')
class BellmanFord:
    
    def bellmanford(self,s,V,E):
        dist = [INF for _ in range(V)]
        dist[s] = 0
        for i in range(V):
            for e in E:
                u,v,cost = e
                if dist[u] != INF and dist[v] > dist[u] + cost:
                    dist[v] = dist[u]+cost
                    if i == V-1 and v == V-1:
                        return -1
        return dist
    

def main():
    N,M = map(int,input().split())
    E = []
    for _ in range(M):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        E.append((a,b,-c))
    bf = BellmanFord()
    dist = bf.bellmanford(0,N,E)
    if dist == -1:
        print('inf')
    else:
        ans = dist[-1]
        print(-ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
INF = float('inf')
class BellmanFord:
    
    def bellmanford(self,s,V,E):
        dist = [INF for _ in range(V)]
        dist[s] = 0
        while (True):
            update = False
            for e in E:
                u,v,cost = e
                if dist[u] != INF and dist[v] > dist[u] + cost:
                    dist[v] = dist[u]+cost
                    update = True
            if not update:break
        return dist
    
    def findneg(self,V,E):
        dist = [0 for _ in range(V)]
        neg = [False for _ in range(V)]
        for i in range(V):
            for e in E:
                u,v,cost = e
                if dist[u] == INF:continue
                if dist[u] != INF and dist[v] > dist[u] + cost:
                    dist[v] = dist[u]+cost
                    neg[v] = True
                    if i == V-1:return True
                if neg[u]:
                    neg[v] = True 
        return neg

def main():
    N,M = map(int,input().split())
    E = []
    for _ in range(M):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        E.append((a,b,-c))
    bf = BellmanFord()
    neg = bf.findneg(N,E)
    if neg[-1]:
        print("inf")
    else:
        dist = bf.bellmanford(0,N,E)
        ans = dist[-1]
        print(-ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
INF = float('inf')
class BellmanFord:
    
    def bellmanford(self,s,V,E):
        dist = [INF for _ in range(V)]
        dist[s] = 0
        while (True):
            update = False
            for e in E:
                u,v,cost = e
                if dist[u] != INF and dist[v] > dist[u] + cost:
                    dist[v] = dist[u]+cost
                    update = True
            if not update:break
        return dist
    
    def findneg(self,V,E):
        dist = [0 for _ in range(V)]
        neg = [False for _ in range(N)]
        for i in range(V):
            for e in E:
                u,v,cost = e
                if dist[u] == INF:continue
                if dist[u] != INF and dist[v] > dist[u] + cost:
                    dist[v] = dist[u]+cost
                    neg[v] = True
                    if i == V-1:return True
                if neg[u]:
                    neg[v] = True 
        return neg

def main():
    N,M = map(int,input().split())
    E = []
    for _ in range(M):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        E.append((a,b,-c))
    bf = BellmanFord()
    neg = bf.findneg(N,E)
    if neg[-1]:
        print("inf")
    else:
        dist = bf.bellmanford(0,N,E)
        ans = dist[-1]
        print(-ans)

if __name__ == "__main__":
    main()