Problem:


Score : 400 points

Problem Statement

There is a directed graph with N vertices and M edges.
The i-th edge (1≤i≤M) points from vertex a_i to vertex b_i, and has a weight c_i.
We will play the following single-player game using this graph and a piece.

Initially, the piece is placed at vertex 1, and the score of the player is set to 0.
The player can move the piece as follows:

When the piece is placed at vertex a_i, move the piece along the i-th edge to vertex b_i. After this move, the score of the player is increased by c_i.

The player can end the game only when the piece is placed at vertex N.
The given graph guarantees that it is possible to traverse from vertex 1 to vertex N.

When the player acts optimally to maximize the score at the end of the game, what will the score be?
If it is possible to increase the score indefinitely, print inf.

Constraints

2≤N≤1000 

1≤M≤min(N(N-1),2000) 

1≤a_i,b_i≤N (1≤i≤M) 

a_i≠b_i (1≤i≤M) 

a_i≠a_j or b_i≠b_j (1≤i<j≤M) 

-10^9≤c_i≤10^9 (1≤i≤M)

c_i is an integer.

In the given graph, there exists a path from vertex 1 to vertex N.

Input

Input is given from Standard Input in the following format:  

N M  
a_1 b_1 c_1  
a_2 b_2 c_2
:  
a_M b_M c_M  

Output

Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf.  

Sample Input 1

3 3
1 2 4
2 3 3
1 3 5

Sample Output 1

7

There are two ways to move the piece to vertex N=3:

vertex 1 → vertex 2 → vertex 3 : score 4+3=7

vertex 1 → vertex 3 : score 5

Thus, the maximum possible score at the end of the game is 7.

Sample Input 2

2 2
1 2 1
2 1 1

Sample Output 2

inf

It is possible to increase the score indefinitely by alternating between vertex 1 and 2.

Sample Input 3

6 5
1 2 -1000000000
2 3 -1000000000
3 4 -1000000000
4 5 -1000000000
5 6 -1000000000

Sample Output 3

-5000000000



Sample input:
3 3
1 2 4
2 3 3
1 3 5


Sample output:
7

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
G = []
for _ in range(M):
    a, b, c = map(int, input().split())
    G.append((a, b, -c))

d = [10**18]*(N+1)
d[1] = 0
for i in range(N):
    for v, nv, cost in G:
        if d[nv] > d[v]+cost:
            d[nv] = d[v]+cost
            if i == N-1 and nv == N-1:
                print('inf')
                exit()
print(-d[N])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
G = []
for _ in range(M):
    a, b, c = map(int, input().split())
    G.append((a, b, -c))

inf = float('inf')
d = [inf]*(N+1)
d[1] = 0
for i in range(N):
    for elem in G:
        v, nv, cost = elem
        if d[nv] > d[v]+cost:
            d[nv] = d[v]+cost
            if i == N-1:
                print('inf')
                exit()
print(-d[N])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
G = []
for _ in range(M):
    a, b, c = map(int, input().split())
    G.append((a, b, -c))

inf = 10**18
d = [inf]*(N+1)
d[1] = 0
for i in range(N+1):
    for elem in G:
        v, nv, cost = elem
        if d[nv] > d[v]+cost:
            d[nv] = d[v]+cost
            if i == N:
                print('inf')
                exit()
print(-d[N])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
G = []
for _ in range(M):
    a, b, c = map(int, input().split())
    G.append((a, b, -c))

inf = 10**18
d = [inf]*(N+1)
d[1] = 0
for i in range(N):
    for elem in G:
        v, nv, cost = elem
        if d[v] != inf and d[nv] > d[v]+cost:
            d[nv] = d[v]+cost
            if i == N-1:
                print('inf')
                exit()
print(-d[N])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
G = []
for _ in range(M):
    a, b, c = map(int, input().split())
    G.append((a, b, -c))

inf = 10**18
d = [inf]*(N+1)
d[1] = 0
for i in range(N):
    for elem in G:
        v, nv, cost = elem
        if d[nv] > d[v]+cost:
            d[nv] = d[v]+cost
            if i == N-1:
                print('inf')
                exit()
print(-d[N])

Submission status: Wrong Answer

Solution code:
_min = -10**12
# 入力 : 隣接行列
# 出力 : 各ノード間の距離行列
def war(dis):
    size = len(dis)
    # 非接続要素が「float("inf")」なら省略可
    """
    for i in range(size):
        for j in range(size):
            if i != j and dis[i][j] == 0:
                dis[i][j] = float("inf")
    """
    for k in range(size):
        for i in range(size):
            for j in range(size):
                if dis[i][k] > _min and dis[k][j] > _min and dis[i][j] < dis[i][k] + dis[k][j]:
                    dis[i][j] = dis[i][k] + dis[k][j]
    return dis

n, m = [int(i) for i in input().split()]
adj = [[_min for _ in range(n)] for _ in range(n)]
for _ in range(m):
    a, b, c = [int(i) for i in input().split()]
    adj[a-1][b-1] = c
dis = war(adj)
for i in range(len(dis)):
    for j in range(len(dis[0])):
        # 0->i->j->n-1でループがあるかどうか
        if dis[0][i] > _min \
                and dis[i][j] > _min \
                and dis[j][i] > _min\
                and dis[i][j] + dis[j][i] > 0:
            print("inf")
            exit()
print(dis[0][-1])

Submission status: Wrong Answer

Solution code:
import sys


def input(): return sys.stdin.readline().strip()


N, M = (list)(map(int, input().split()))
edge = []

for _ in range(M):
    edge.append((list)(map(int, input().split())))


inf = 10**15

d = [inf]*(N+1)
d[1] = 0
ans = inf
for i in range(N-1):
    for a, b, c in edge:
        c = -c
        new_d = d[a]+c
        if d[a] != inf and d[b] > new_d:
            d[b] = new_d

for a, b, c in edge:
    c = -c
    new_d = d[a]+c
    if d[a] > new_d:
        d[b] = new_d
        if b == N:
            print('inf')
            exit()
print(-d[-1])

Submission status: Wrong Answer

Solution code:
import sys


def input(): return sys.stdin.readline().strip()


N, M = (list)(map(int, input().split()))
edge = []

for _ in range(M):
    edge.append((list)(map(int, input().split())))


inf = 10**15

d = [inf]*(N+1)
d[1] = 0
ans = inf
for i in range(N-1):
    for a, b, c in edge:
        c = -c
        new_d = d[a]+c
        if d[a] != inf and d[b] > new_d:
            d[b] = new_d

for a, b, c in edge:
    c = -c
    new_d = d[a]+c
    if d[b] < new_d:
        d[b] = new_d
        if b == N:
            print('inf')
            exit()
print(-d[-1])

Submission status: Wrong Answer

Solution code:
import sys


def input(): return sys.stdin.readline().strip()


N, M = (list)(map(int, input().split()))
edge = []

for _ in range(M):
    edge.append((list)(map(int, input().split())))


inf = 10**15

d = [inf]*(N+1)
d[1] = 0
ans = inf
for i in range(N-1):
    for a, b, c in edge:
        c = -c
        new_d = d[a]+c
        if d[a] != inf and d[b] > new_d:
            d[b] = new_d

for a, b, c in edge:
    c = -c
    new_d = d[a]+c
    if d[a] < new_d:
        d[b] = new_d
        if b == N:
            print('inf')
            exit()
print(-d[-1])

Submission status: Wrong Answer

Solution code:
import sys


def input(): return sys.stdin.readline().strip()


N, M = (list)(map(int, input().split()))
edge = []

for _ in range(M):
    edge.append((list)(map(int, input().split())))


inf = 10**15

d = [inf]*(N+1)
d[1] = 0
ans = inf
for i in range(N-1):
    for a, b, c in edge:
        c = -c
        new_d = d[a]+c
        if d[a] != inf and d[b] > new_d:
            d[b] = new_d

for a, b, c in edge:
    c = -c
    if d[a] + c < d[b]:
        d[b] = d[a]+c
        print('b->', b)
        if b == N:
            print('inf')
            exit()
print(-d[-1])

Submission status: Wrong Answer

Solution code:
import sys


def input(): return sys.stdin.readline().strip()


N, M = (list)(map(int, input().split()))
edge = []

for _ in range(M):
    edge.append((list)(map(int, input().split())))


inf = 10**15

d = [inf]*(N+1)
d[1] = 0
ans = inf
for i in range(N-1):
    for a, b, c in edge:
        c = -c
        new_d = d[a]+c
        if d[a] != inf and d[b] > new_d:
            d[b] = new_d

for a, b, c in edge:
    if d[a] + c < d[b]:
        d[b] = d[a]+c
        if b == N:
            print('inf')
            exit()
print(-d[-1])

Submission status: Wrong Answer

Solution code:
import sys


def input(): return sys.stdin.readline().strip()


N, M = (list)(map(int, input().split()))
edge = []

for _ in range(M):
    edge.append((list)(map(int, input().split())))


inf = 10**15

d = [inf]*(N+1)
d[1] = 0
ans = inf
for i in range(N-1):
    for a, b, c in edge:
        c = -c
        new_d = d[a]+c
        if d[a] != inf and d[b] > new_d:
            d[b] = new_d

for a, b, c in edge:
    if d[a] + c < d[b]:
        d[b] = d[a]+c
        if b == N-1:
            print('inf')
            exit()
print(-d[-1])

Submission status: Wrong Answer

Solution code:
N, M = (list)(map(int, input().split()))
edge = []

for _ in range(M):
    edge.append((list)(map(int, input().split())))


inf = 10**15

d = [inf]*(N+1)
d[1] = 0
ans = inf
for i in range(2*N):
    update = False
    for a, b, c in edge:
        c = -c
        new_d = d[a]+c
        if d[a] != inf and d[b] > new_d:
            d[b] = new_d
            update = True

    if not update:
        break
    elif i == (2*N-1):
        ans = 'inf'
        print(ans)
        exit()
print(-d[-1])

Submission status: Wrong Answer

Solution code:
N, M = (list)(map(int, input().split()))
edge = []

for _ in range(M):
    edge.append((list)(map(int, input().split())))


inf = 10**15

d = [inf]*(N+1)
d[1] = 0
ans = inf
for i in range(N+2):
    update = False
    for a, b, c in edge:
        c = -c
        new_d = d[a]+c
        if d[a] != inf and d[b] > new_d:
            d[b] = new_d
            update = True

    if not update:
        break
    elif i == (N+1):
        ans = 'inf'
        print(ans)
        exit()
print(-d[-1])

Submission status: Wrong Answer

Solution code:
N, M = (list)(map(int, input().split()))
edge = []

for _ in range(M):
    edge.append((list)(map(int, input().split())))


inf = 10**15

d = [inf]*(N+2)
d[1] = 0
ans = inf
for i in range(N+1):
    update = False
    for a, b, c in edge:
        c = -c
        new_d = d[a]+c
        if d[a] != inf and d[b] > new_d:
            d[b] = new_d
            update = True

    if not update:
        break
    elif i == N:
        ans = 'inf'

if not ans == 'inf':
    ans = d[N]
    print(-ans)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

"""
・ベルマンフォード
"""

import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)

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

edges = [None] * M
for i in range(M):
    a, b, c = map(int, input().split())
    # 0-indexed
    # コストの正負を逆転させれば最短経路問題として処理できる
    edges[i] = (a-1, b-1, -c)

def bellman_ford(N: int, edges: list, start: int) -> list:
    # 頂点[ある始点からの最短距離]
    # (経路自体を知りたい時はここに前の頂点も持たせる)
    verts = [float('inf')] * N
    verts[start] = 0
    # 各辺によるコストの置き換えを頂点数N-1回繰り返す
    for i in range(N-1):
        for src, dest, cost in edges:
            verts[dest] = min(verts[src] + cost, verts[dest])
    # 無限に減らせる場所がないか確認
    for src, dest, cost in edges:
        if verts[src] + cost < verts[dest] and dest == N-1:
            # 頂点Nがそれに該当していたら空リストを返却
            return []
    # 問題なければ頂点リストを返却
    return verts

verts = bellman_ford(N, edges, 0)
if not verts:
    print('inf')
else:
    # 正負を戻して出力
    print(-verts[N-1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

"""
・ベルマンフォード
"""

import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)

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

edges = [None] * M
for i in range(M):
    a, b, c = map(int, input().split())
    # 0-indexed
    # コストの正負を逆転させれば最短経路問題として処理できる
    edges[i] = (a-1, b-1, -c)

def bellman_ford(N: int, edges: list, start: int) -> list:
    # 頂点[ある始点からの最短距離]
    # (経路自体を知りたい時はここに前の頂点も持たせる)
    verts = [float('inf')] * N
    verts[start] = 0
    # 各辺によるコストの置き換えを頂点数N-1回繰り返す
    for i in range(N-1):
        for src, dest, cost in edges:
            verts[dest] = min(verts[src] + cost, verts[dest])
    # 無限に減らせる場所がないか確認
    for i in range(N):
        for src, dest, cost in edges:
            if verts[src] + cost < verts[dest]:
                # あったら空リストを返却
                return []
    # 問題なければ頂点リストを返却
    return verts

verts = bellman_ford(N, edges, 0)
if not verts:
    print('inf')
else:
    # 正負を戻して出力
    print(-verts[N-1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

"""
・ベルマンフォード
"""

import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)

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

edges = [None] * M
for i in range(M):
    a, b, c = map(int, input().split())
    # 0-indexed
    # コストの正負を逆転させれば最短経路問題として処理できる
    edges[i] = (a-1, b-1, -c)

def bellman_ford(N: int, edges: list, start: int) -> list:
    # 頂点[ある始点からの最短距離]
    # (経路自体を知りたい時はここに前の頂点も持たせる)
    verts = [float('inf')] * N
    verts[start] = 0
    # 各辺によるコストの置き換えを頂点数N-1回繰り返す
    for i in range(N):
        for src, dest, cost in edges:
            verts[dest] = min(verts[src] + cost, verts[dest])
    # 無限に減らせる場所がないか確認
    for src, dest, cost in edges:
        if verts[src] + cost < verts[dest]:
            # あったら空リストを返却
            return []
    # 問題なければ頂点リストを返却
    return verts

verts = bellman_ford(N, edges, 0)
if not verts:
    print('inf')
else:
    # 正負を戻して出力
    print(-verts[N-1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

"""
・ベルマンフォード
"""

import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)

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

edges = [None] * M
for i in range(M):
    a, b, c = map(int, input().split())
    # 0-indexed
    # コストの正負を逆転させれば最短経路問題として処理できる
    edges[i] = (a-1, b-1, -c)

def bellman_ford(N: int, edges: list, start: int) -> list:
    # 頂点[ある始点からの最短距離]
    # (経路自体を知りたい時はここに前の頂点も持たせる)
    verts = [float('inf')] * N
    verts[start] = 0
    # 各辺によるコストの置き換えを頂点数N-1回繰り返す
    for i in range(N-1):
        for src, dest, cost in edges:
            verts[dest] = min(verts[src] + cost, verts[dest])
    # 無限に減らせる場所がないか確認
    for src, dest, cost in edges:
        if verts[src] + cost < verts[dest]:
            # あったら空リストを返却
            return []
    # 問題なければ頂点リストを返却
    return verts

verts = bellman_ford(N, edges, 0)
if not verts:
    print('inf')
else:
    # 正負を戻して出力
    print(-verts[N-1])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
Edges = []
for i in range(M):
    a, b, c = map(int, input().split())
    a, b = a - 1, b - 1
    c = -c
    Edges.append([a, b, c])

dist = [float('inf')] * N
dist[0] = 0

for i in range(1, N+1):
    for fr, to, cost in Edges:
        if (dist[fr] != float('inf')) and (dist[to] > dist[fr] + cost):
            dist[to] = dist[fr] + cost
            if (i == N - 1) & (to == N - 1):
                print('inf')
                exit()

print(-dist[-1])

Submission status: Wrong Answer

Solution code:
class BellmanFord:
    def __init__(self, graph, num_v, start_point):
        """
        graph : [[a1,b1,c1],[a2,b2,c2],‥]みたいな3次元配列のリスト,a1からb1までのコストはc1
        num_v :頂点の数
        start_point :スタート地点(a1)
        """
        self.num_v = num_v
        self.graph = graph
        self.start_point = start_point
 
 
    def execute(self):
        dist = [float("inf") for _ in range(self.num_v)]
        dist[self.start_point] = 0

        negative = [False for _ in range(self.num_v)]

        for i in range(self.num_v - 1):
            for start, end, cost in self.graph:
                if dist[end] > dist[start] + cost:
                    dist[end] = dist[start] + cost

        #負の経路がないなら|V-1|以上の繰り返しで最短経路の更新は行われない、負の経路の検出
        for i in range(self.num_v):
            for start, end, cost in self.graph:
                if dist[end] > dist[start] + cost:
                    dist[end] = dist[start] + cost
                    negative[end] = True
                if negative[start]:
                    negative[end] = True
        return dist, negative

N,M = map(int,input().split())
T = [[int(_) for _ in input().split()] for i in range(M)]
 
E = []
for a, b, c in T:
    E.append((a - 1, b - 1, c))

bellman = BellmanFord(E, N, 0)
result, negative = bellman.execute()
 
if negative[-1]:
    print("inf")
else:
    print(result[-1])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [0] * M
B = [0] * M
C = [0] * M
for i in range(M):
	A[i], B[i], C[i] = map(int, input().split())
	A[i] -= 1
	B[i] -= 1
inf = 123456789123456789123456789
dist = [ -inf ] * N
dist[0] = 0
for i in range(2 * N):
	for j in range(M):
		if dist[B[j]] < dist[A[j]] + C[j]:
			dist[B[j]] = dist[A[j]] + C[j]
bounded = True
for i in range(M):
	if dist[B[i]] < dist[A[i]] + C[i]:
		bounded = False
if not bounded:
	print("inf")
else:
	print(dist[N - 1])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [0] * M
B = [0] * M
C = [0] * M
for i in range(M):
	A[i], B[i], C[i] = map(int, input().split())
	A[i] -= 1
	B[i] -= 1
inf = 123456789123456789123456789
dist = [ -inf ] * N
dist[0] = 0
for i in range(N):
	for j in range(M):
		if dist[B[j]] < dist[A[j]] + C[j]:
			dist[B[j]] = dist[A[j]] + C[j]
bounded = True
for i in range(M):
	if dist[B[i]] < dist[A[i]] + C[i]:
		bounded = False
if not bounded:
	print("inf")
else:
	print(dist[N - 1])

Submission status: Wrong Answer

Solution code:
class edge:
	def __init__(self, to, cost):
		self.to = to
		self.cost = cost

N, M = map(int, input().split())
G = list()
for i in range(N):
	G.append(list())
for i in range(M):
	a, b, c = map(int, input().split())
	G[a - 1].append(edge(b - 1, c))
inf = 123456789123456789123456789
dist = [ -inf ] * N
dist[0] = 0
vis = [ False ] * N
for i in range(9 * N):
	if i % N == 0:
		vis = [False] * N
	maxptr = -1
	for j in range(N):
		if (not vis[j]) and (maxptr == -1 or dist[maxptr] < dist[j]):
			maxptr = j
	vis[maxptr] = True
	for j in G[maxptr]:
		if dist[j.to] < dist[maxptr] + j.cost:
			dist[j.to] = dist[maxptr] + j.cost
bounded = True
for i in range(N):
	for j in G[i]:
		if dist[j.to] < dist[i] + j.cost:
			bounded = False
if not bounded:
	print("inf")
else:
	print(dist[N - 1])

Submission status: Wrong Answer

Solution code:
class edge:
	def __init__(self, to, cost):
		self.to = to
		self.cost = cost

N, M = map(int, input().split())
G = list()
for i in range(N):
	G.append(list())
for i in range(M):
	a, b, c = map(int, input().split())
	G[a - 1].append(edge(b - 1, c))
inf = 123456789123456789123456789
dist = [ -inf ] * N
dist[0] = 0
vis = [ False ] * N
for i in range(2 * N):
	if i == N:
		vis = [False] * N
	maxptr = -1
	for j in range(N):
		if (not vis[j]) and (maxptr == -1 or dist[maxptr] < dist[j]):
			maxptr = j
	vis[maxptr] = True
	for j in G[maxptr]:
		if dist[j.to] < dist[maxptr] + j.cost:
			dist[j.to] = dist[maxptr] + j.cost
bounded = True
for i in range(N):
	for j in G[i]:
		if dist[j.to] < dist[i] + j.cost:
			bounded = False
if not bounded:
	print("inf")
else:
	print(dist[N - 1])

Submission status: Wrong Answer

Solution code:
class edge:
	def __init__(self, to, cost):
		self.to = to
		self.cost = cost

N, M = map(int, input().split())
G = list()
for i in range(N):
	G.append(list())
for i in range(M):
	a, b, c = map(int, input().split())
	G[a - 1].append(edge(b - 1, c))
inf = 123456789123456789123456789
dist = [ -inf ] * N
dist[0] = 0
vis = [ False ] * N
for i in range(9 * N):
	maxptr = -1
	for j in range(N):
		if (not vis[j]) and (maxptr == -1 or dist[maxptr] < dist[j]):
			maxptr = j
	vis[maxptr] = True
	for j in G[maxptr]:
		if dist[j.to] < dist[maxptr] + j.cost:
			dist[j.to] = dist[maxptr] + j.cost
bounded = True
for i in range(N):
	for j in G[i]:
		if dist[j.to] < dist[i] + j.cost:
			bounded = False
if not bounded:
	print("inf")
else:
	print(dist[N - 1])

Submission status: Wrong Answer

Solution code:
class edge:
	def __init__(self, to, cost):
		self.to = to
		self.cost = cost

N, M = map(int, input().split())
G = list()
for i in range(N):
	G.append(list())
for i in range(M):
	a, b, c = map(int, input().split())
	G[a - 1].append(edge(b - 1, c))
inf = 123456789123456789123456789
dist = [ -inf ] * N
dist[0] = 0
vis = [ False ] * N
for i in range(N):
	maxptr = -1
	for j in range(N):
		if (not vis[j]) and (maxptr == -1 or dist[maxptr] < dist[j]):
			maxptr = j
	vis[maxptr] = True
	for j in G[maxptr]:
		if dist[j.to] < dist[maxptr] + j.cost:
			dist[j.to] = dist[maxptr] + j.cost
bounded = True
for i in range(N):
	for j in G[i]:
		if dist[j.to] < dist[i] + j.cost:
			bounded = False
if not bounded:
	print("inf")
else:
	print(dist[N - 1])

Submission status: Wrong Answer

Solution code:
class BellmanFord:
    def __init__(self, graph, num_v, start_point):
        self.num_v = num_v
        self.graph = graph
        self.start_point = start_point


    def execute(self):
        dist = [float("inf") for _ in range(self.num_v)]
        dist[self.start_point] = 0
        negative = [False for _ in range(self.num_v)]
        for i in range(self.num_v):
            for src, dest, cost in self.graph:
                if dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    negative[dest] = True
                if negative[src]:
                    negative[dest] = True
        return dist, negative


n, m = map(int, input().split())
graph = []

for i in range(m):
    a, b, c = map(int, input().split())
    graph.append([a-1, b-1, -c])
bellman = BellmanFord(graph, n, 0)
result, negative = bellman.execute()

if negative[-1]:
    print("inf")
else:
    print(-result[-1])

Submission status: Wrong Answer

Solution code:
class BellmanFord:
    def __init__(self, graph, num_v, start_point):
        self.num_v = num_v
        self.graph = graph
        self.start_point = start_point


    def execute(self):
        dist = [float("inf") for _ in range(self.num_v)]
        dist[self.start_point] = 0
        negative = [False for _ in range(self.num_v)]
        for i in range(self.num_v - 1):
            for src, dest, cost in self.graph:
                if dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
        for i in range(self.num_v):
            for src, dest, cost in self.graph:
                if dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    if dist[dest] > dist[src] + cost:
                        dist[dest] = dist[src] + cost
                        negative[dest] = True
                    if negative[src]:
                        negative[dest] = True
        return dist, negative


n, m = map(int, input().split())
graph = []

for i in range(m):
    a, b, c = map(int, input().split())
    graph.append([a-1, b-1, -c])
bellman = BellmanFord(graph, n, 0)
result, negative = bellman.execute()

if negative[-1]:
    print("inf")
else:
    print(-result[-1])

Submission status: Wrong Answer

Solution code:
class BellmanFord:
    def __init__(self, graph, num_v, start_point):
        self.num_v = num_v
        self.graph = graph
        self.start_point = start_point


    def execute(self):
        dist = [float("inf") for _ in range(self.num_v)]
        dist[self.start_point] = 0
        negative = [False for _ in range(self.num_v)]
        for i in range(self.num_v - 1):
            for src in range(self.num_v-1):
                for dest, cost in self.graph[src].items():
                    if dist[dest] > dist[src] + cost:
                        dist[dest] = dist[src] + cost
        for i in range(self.num_v):
            for src in range(self.num_v):
                for dest, cost in self.graph[src].items():
                    if dist[dest] > dist[src] + cost:
                        dist[dest] = dist[src] + cost
                        negative[dest] = True
                    if negative[src]:
                        negative[dest] = True
        return dist, negative


n, m = map(int, input().split())
graph = [dict() for _ in range(n)]

for i in range(m):
    a, b, c = map(int, input().split())
    graph[a-1][b-1] = -c
bellman = BellmanFord(graph, n, 0)
result, negative = bellman.execute()

if negative[-1]:
    print("inf")
else:
    print(-result[-1])

Submission status: Wrong Answer

Solution code:
def BellmanFord(edges, num_v, source):
    # グラフの初期化
    inf = float("inf")
    dist = [inf for i in range(num_v)]
    dist[source - 1] = 0

    # 辺の緩和
    for i in range(num_v):
        for edge in edges:
            if edge[0] != inf and dist[edge[1] - 1] > dist[edge[0] - 1] + edge[2]:
                dist[edge[1] - 1] = dist[edge[0] - 1] + edge[2]
                if i == num_v - 1: return -1

    return dist

n, m = map(int, input().split())
graph = []

for i in range(m):
    a, b, c = map(int, input().split())
    graph.append([a, b, -c])
bellman = BellmanFord(graph, n,  1)


if bellman == -1:
    print("inf")
else:
    print(-bellman[n-1])

Submission status: Wrong Answer

Solution code:
class BellmanFord:
    def __init__(self, graph, num_v, start_point):
        self.num_v = num_v
        self.graph = graph
        self.start_point = start_point

    def execute(self):
        dist = [float("inf") for _ in range(self.num_v)]
        dist[self.start_point] = 0

        for i in range(self.num_v):
            for src, dest, cost in self.graph:
                if dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    if i == self.num_v - 1:
                        return -1
        return dist


n, m = map(int, input().split())
graph = []

for i in range(m):
    a, b, c = map(int, input().split())
    graph.append([a-1, b-1, -c])
bellman = BellmanFord(graph, n,  0)
result = bellman.execute()

if result == -1:
    print("inf")
else:
    print(-result[n-1])

Submission status: Wrong Answer

Solution code:
class BellmanFord:
    def __init__(self, graph, start_point):
        self.num_v = len(graph)
        self.graph = graph
        self.start_point = start_point

    def execute(self):
        dist = [float("inf") for _ in range(self.num_v)]
        dist[self.start_point] = 0

        for i in range(self.num_v):
            for src in range(self.num_v):
                for dest, cost in self.graph[src].items():
                    if dist[dest] > dist[src] + cost:
                        dist[dest] = dist[src] + cost
                        if i == self.num_v - 1:
                            return -1
        return dist


n, m = map(int, input().split())
graph = [dict() for _ in range(n)]

for i in range(m):
    a, b, c = map(int, input().split())
    graph[a - 1][b - 1] = - c
bellman = BellmanFord(graph, 0)
result = bellman.execute()

if result == -1:
    print("inf")
else:
    print(-result[n-1])

Submission status: Wrong Answer

Solution code:
class BellmanFord:
    def __init__(self, graph, start_point):
        self.num_v = len(graph)
        self.graph = graph
        self.start_point = start_point

    def execute(self):
        dist = [float("inf") for _ in range(self.num_v)]
        dist[self.start_point] = 0

        for i in range(self.num_v):
            for dest, cost in self.graph[i].items():
                if dist[dest] > dist[i] + cost:
                    dist[dest] = dist[i] + cost
                    if i == self.num_v - 1:
                        return -1
        return dist


n, m = map(int, input().split())
graph = [dict() for _ in range(n)]

for i in range(m):
    a, b, c = map(int, input().split())
    graph[a - 1][b - 1] = - c
bellman = BellmanFord(graph, 0)
result = bellman.execute()

if result == -1:
    print("inf")
else:
    print(-result[n-1])

Submission status: Wrong Answer

Solution code:
V, E = [int(_) for _ in input().split()]
G = {}

for i in range(V):
    G[i] = {}
for _ in range(E):
    s, t, c = [int(_) for _ in input().split()]
    s -= 1  # 0を始点とする場合
    t -= 1  # 0を始点とする場合
    G[s][t] = -c

INF = float('inf')

D = [INF] * V
D[0] = 0  # 0を始点とする場合
count = 0
while True:
    count += 1
    update = False
    for s in G.keys():
        for t in G[s].keys():
            if D[s] != INF and D[t] > D[s] + G[s][t]:
                D[t] = D[s] + G[s][t]
                update = True
    if not update:
        break
    if count > V:
        print('inf')
        exit()
print(-D[-1])

Submission status: Wrong Answer

Solution code:
V, E = [int(_) for _ in input().split()]
G = {}

for i in range(V):
    G[i] = {}
for _ in range(E):
    s, t, c = [int(_) for _ in input().split()]
    s -= 1  # 0を始点とする場合
    t -= 1  # 0を始点とする場合
    G[s][t] = -c

INF = float('inf')

D = [INF] * V
D[0] = 0  # 0を始点とする場合
count = 0
while True:
    count += 1
    update = False
    for s in G.keys():
        for t in G[s].keys():
            if D[s] != INF and D[t] > D[s] + G[s][t]:
                D[t] = D[s] + G[s][t]
                update = True
    if not update:
        break
    if count == V:
        print('inf')
        exit()
print(-D[-1])

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
d = []
for i in range(m):#h:高さ
    d.append([int(m) for m in input().split()])
e = []
for i in range(m):
    e.append([])
for i in range(m):
    e[i].append(d[i][0] - 1)
    e[i].append(d[i][1] - 1)
    e[i].append(-d[i][2])
#edges = d
#num_v = n
#source = m


#ベルマンフォード
#高々|V|-1回で最安定経路にたどり着くっていうのがなかなか理解できなくて、紙に書きながら理解した。

"""edge = [[0,1,2],[0,2,5],[1,2,4],[1,3,6],[1,4,10],[2,3,2],[3,5,1],[4,5,3],[4,6,5],[5,6,9],
       [1,0,2],[2,0,5],[2,1,4],[3,1,6],[4,1,10],[3,2,2],[5,3,1],[5,4,3],[6,4,5],[6,5,9]]"""
edge = e

def shortest_path(edge,num_v,start):
    inf = float("inf")
    d = [inf for f in range(num_v)]
    d[start] = 0;
    for i in range(num_v):
        update = False
        for e in edge:
            if d[e[0]] != inf and d[e[1]] > d[e[0]] + e[2]:
                d[e[1]] = d[e[0]] + e[2]
                update = True
        if not update:
            break
        if i == num_v - 1:
            d[n - 1] = -inf
            break
    return d

#print(-shortest_path(edge,n,0)[n - 1])
print("inf")
#print(BellmanFord(e, n, m))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
d = []
for i in range(m):#h:高さ
    d.append([int(m) for m in input().split()])
e = []
for i in range(m):
    e.append([])
for i in range(m):
    e[i].append(d[i][0] - 1)
    e[i].append(d[i][1] - 1)
    e[i].append(-d[i][2])
#edges = d
#num_v = n
#source = m


#ベルマンフォード
#高々|V|-1回で最安定経路にたどり着くっていうのがなかなか理解できなくて、紙に書きながら理解した。

"""edge = [[0,1,2],[0,2,5],[1,2,4],[1,3,6],[1,4,10],[2,3,2],[3,5,1],[4,5,3],[4,6,5],[5,6,9],
       [1,0,2],[2,0,5],[2,1,4],[3,1,6],[4,1,10],[3,2,2],[5,3,1],[5,4,3],[6,4,5],[6,5,9]]"""
edge = e

def shortest_path(edge,num_v,start):
    inf = float("inf")
    d = [inf for f in range(num_v)]
    d[start] = 0;
    for i in range(num_v):
        update = False
        for e in edge:
            if d[e[0]] != inf and d[e[1]] > d[e[0]] + e[2]:
                d[e[1]] = d[e[0]] + e[2]
                update = True
        if i == num_v - 1:
            d[n - 1] = -inf
            break
        if not update:
            break
    return d

print(-shortest_path(edge,n,0)[n - 1])

#print(BellmanFord(e, n, m))