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())
INF = 10**13

a, b, c = [([0]*m) for _ in range(3)] 
for i in range(m):
    d,e,f = map(int, input().split())
    a[i] = d-1
    b[i] = e-1
    c[i] = -f

d = [INF]*n
d[0] = 0

for _ in range(n-1):
    for x,y,z in zip(a,b,c):
        if d[x] == INF:
            continue
        d[y] = min(d[y], d[x]+z)

ans = -d[-1]
negative = [False]*n

for _ in range(n):
    for x,y,z in zip(a,b,c):
        if d[y] > d[x]+z:
            d[y] = d[x]+z
            negative[y] = True
        negative[y] |= negative[x]

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

Submission status: Wrong Answer

Solution code:
def BellmanFord(x,n,s):
	inf = float("inf")
	dist = [inf for i in range(n+1)]
	dist[s] = 0
	for i in range(1,n+1):
		for j,k,c in x:
			if j!=inf and dist[k]>dist[j]+c:
				dist[k] = dist[j]+c
				if i==n: return -1
	return dist
n,m = (int(i) for i in input().split())
x = []
for _ in range(m):
	i,j,k = (int(l) for l in input().split())
	x.append([i,j,-k])
b = BellmanFord(x,n,1)
if b==-1: print("inf")
else: print(-b[n])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
edge = []
for i in range(M):
    a,b,c = map(int, input().split())
    edge.append((a-1,b-1, c))
point = [None] * N
point[0] = 0
for i in range((N*4)//3):
    for a,b,c in edge:
        if point[a] is not None:
            if point[b] is None:
                point[b] = point[a] + c
            point[b] = max(point[b], point[a] + c)
current_point = point[N-1]
for i in range(N//4):
    for a,b,c in edge:
        if point[a] is not None:
            if point[b] is None:
                point[b] = point[a] + c
            point[b] = max(point[b], point[a] + c)
if current_point < point[N-1]:
    print("inf")
else:
    print(current_point)

Submission status: Wrong Answer

Solution code:
n,m = map(int,raw_input().split())

minusinf = float('-inf')
dist = [minusinf for i in range(n)]
dist[0] = 0

arr = []

for i in range(m):
	arr.append(map(int,raw_input().split()))


count = n*m
while True :
	isupload = False
	for line in arr :
		frm = line[0] - 1
		to = line[1] -1
		cost = line[2]
		if dist[frm] != minusinf and dist[to] < dist[frm] + cost:
			dist[to] = dist[frm] + cost
			isupload = True
	count -= 1
	if not isupload or count == 0: 
		break

if count == 0 :
	print 'inf'
else:
	print dist[n-1]

Submission status: Wrong Answer

Solution code:
# from math import inf
inf = 10**50

N, M = map(int, input().split())
edges = []
for j in range(M):
    edges.append(list(map(int, input().split())))

d = [0] + ([-inf]*(N-1))
pred = [None] * N

for i in range(N-1):
    for j in range(M):
        u = edges[j][0]-1
        v = edges[j][1]-1
        w = edges[j][2]
        if (d[v] < d[u] + w) and (d[u] != -inf):
            d[v] = d[u] + w
            pred[v] = u

positive_loop = False
for i in range(N):
    for j in range(M):
        u = edges[j][0]-1
        v = edges[j][1]-1
        w = edges[j][2]
        if (v == N-1) and (d[u] + w > d[v]) and (d[u] != -inf):
            d[v] = d[u] + w
            positive_loop = True
            break
    if positive_loop:
        break

if positive_loop:
    print("inf")
else:
    print(d[N-1])

Submission status: Wrong Answer

Solution code:
# from math import inf
inf = 10**50

N, M = map(int, input().split())
edges = []
for j in range(M):
    edges.append(list(map(int, input().split())))

d = [0] + ([-inf]*(N-1))
pred = [None] * N

for i in range(N-1):
    for j in range(M):
        u = edges[j][0]-1
        v = edges[j][1]-1
        w = edges[j][2]
        if (d[v] < d[u] + w) and (d[u] != -inf):
            d[v] = d[u] + w
            pred[v] = u

positive_loop = False
for i in range(N):
    for j in range(M):
        u = edges[j][0]-1
        v = edges[j][1]-1
        w = edges[j][2]
        if (v == N-1) and (d[u] + w > d[v]) and (d[u] != -inf):
            positive_loop = True
            break
    if positive_loop:
        break

if positive_loop:
    print("inf")
else:
    print(d[N-1])

Submission status: Wrong Answer

Solution code:
# from math import inf
from time import sleep
inf = 10**50

N, M = map(int, input().split())
edges = []
for j in range(M):
    edges.append(list(map(int, input().split())))

d = [0] + ([-inf]*(N-1))
pred = [None] * N

for i in range(N-1):
    for j in range(M):
        u = edges[j][0]-1
        v = edges[j][1]-1
        w = edges[j][2]
        if (d[v] < d[u] + w) and (d[u] != -inf):
            d[v] = d[u] + w
            pred[v] = u

positive_loop = False
for j in range(M):
    u = edges[j][0]-1
    v = edges[j][1]-1
    w = edges[j][2]
    if (d[u] + w > d[v]) and (d[u] != -inf):
        positive_loop = True

if positive_loop:
    sleep(1)
    print("inf")
else:
    print(d[N-1])

Submission status: Wrong Answer

Solution code:
# from math import inf
inf = 10**50

N, M = map(int, input().split())
edges = []
for j in range(M):
    edges.append(list(map(int, input().split())))

d = [0] + ([-inf]*(N-1))
pred = [None] * N

for i in range(N-1):
    for j in range(M):
        u = edges[j][0]-1
        v = edges[j][1]-1
        w = edges[j][2]
        if (d[v] < d[u] + w) and (d[u] != -inf):
            d[v] = d[u] + w
            pred[v] = u

positive_loop = False
for j in range(M):
    u = edges[j][0]-1
    v = edges[j][1]-1
    w = edges[j][2]
    if (d[u] + w > d[v]) and (d[u] != -inf):
        positive_loop = True

if positive_loop:
    print("inf")
else:
    print(d[N-1])

Submission status: Wrong Answer

Solution code:
# from math import inf
inf = 10**50

N, M = map(int, input().split())
edges = []
for j in range(M):
    edges.append(list(map(int, input().split())))

d = [0] + ([-inf]*(N-1))
pred = [None] * N

for i in range(N-1):
    for j in range(M):
        u = edges[j][0]-1
        v = edges[j][1]-1
        w = edges[j][2]
        if (d[v] < d[u] + w) and (d[u] != -inf):
            d[v] = d[u] + w
            pred[v] = u

positive_loop = False
for j in range(M):
    u = edges[j][0]-1
    v = edges[j][1]-1
    w = edges[j][2]
    if d[u] + w > d[v]:
        positive_loop = True

if positive_loop:
    print("inf")
else:
    print(d[N-1])

Submission status: Wrong Answer

Solution code:
# from math import inf
inf = 10**50

N, M = map(int, input().split())
edges = []
for j in range(M):
    edges.append(list(map(int, input().split())))

d = [0] + ([-inf]*(N-1))
pred = [None] * N

for i in range(N+1):
    for j in range(M):
        u = edges[j][0]-1
        v = edges[j][1]-1
        w = edges[j][2]
        if d[v] < d[u] + w:
            d[v] = d[u] + w
            pred[v] = u

positive_loop = False
for j in range(M):
    u = edges[j][0]-1
    v = edges[j][1]-1
    w = edges[j][2]
    if d[u] + w > d[v]:
        positive_loop = True

if positive_loop:
    print("inf")
else:
    print(d[N-1])

Submission status: Wrong Answer

Solution code:
# from math import inf
inf = 10**50

N, M = map(int, input().split())
edges = []
for j in range(M):
    edges.append(list(map(int, input().split())))

d = [0] + ([-inf]*(N-1))
pred = [None] * N

for i in range(N-1):
    for j in range(M):
        u = edges[j][0]-1
        v = edges[j][1]-1
        w = edges[j][2]
        if d[v] < d[u] + w:
            d[v] = d[u] + w
            pred[v] = u

positive_loop = False
for j in range(M):
    u = edges[j][0]-1
    v = edges[j][1]-1
    w = edges[j][2]
    if d[u] + w > d[v]:
        positive_loop = True

if positive_loop:
    print("inf")
else:
    print(d[N-1])

Submission status: Wrong Answer

Solution code:
# from math import inf
inf = 10**20

N, M = map(int, input().split())
edges = []
for j in range(M):
    edges.append(list(map(int, input().split())))

d = [0] + ([-inf]*(N-1))
pred = [None] * N

for i in range(N-1):
    for j in range(M):
        u = edges[j][0]-1
        v = edges[j][1]-1
        w = edges[j][2]
        if d[v] < d[u] + w:
            d[v] = d[u] + w
            pred[v] = u

positive_loop = False
for j in range(M):
    u = edges[j][0]-1
    v = edges[j][1]-1
    w = edges[j][2]
    if d[u] + w > d[v]:
        positive_loop = True

if positive_loop:
    print("inf")
else:
    print(d[N-1])

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n, m = [int(i) for i in input().split()]
es = [[int(j) for j in input().split()] for i in range(m)]

x = [float('-inf')] * (n+1)
x[1] = 0

es.sort(key=itemgetter(0, 1))

for a, b, c in es:
    x[b] = max(x[b], x[a]+c)

es.reverse()

for a, b, c in es:
    x[b] = max(x[b], x[a]+c)

prevxn = x[n]
for a, b, c in es:
        x[b] = max(x[b], x[a]+c)

if prevxn != x[n]:
    print('inf')
else:
    print(x[n])

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n, m = [int(i) for i in input().split()]
es = [[int(j) for j in input().split()] for i in range(m)]

x = [float('-inf')] * (n+1)
x[1] = 0

es.sort(key=itemgetter(0, 1))

for i in range(2):
    for a, b, c in es:
        x[b] = max(x[b], x[a]+c)

prevxn = x[n]
for a, b, c in es:
        x[b] = max(x[b], x[a]+c)

if prevxn != x[n]:
    print('inf')
else:
    print(x[n])

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n, m = [int(i) for i in input().split()]
es = [[int(j) for j in input().split()] for i in range(m)]

x = [float('-inf')] * (n+1)
x[1] = 0

es.sort(key=itemgetter(0, 1))


for a, b, c in es:
    x[b] = max(x[b], x[a]+c)

prevxn = x[n]
for a, b, c in es:
        x[b] = max(x[b], x[a]+c)

if prevxn != x[n]:
    print('inf')
else:
    print(x[n])

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy

n, m = [int(i) for i in input().split()]
es = [[int(j) for j in input().split()] for i in range(m)]

x = [float('-inf')] * n
x[0] = 0
border = sys.maxsize // 1000

for i in range(n):
    for a, b, c in es:
        a, b = a-1, b-1
        x[b] = max(x[b], x[a]+c)
        if x[b] >= border:
            x[b] = float('inf')

prevx = deepcopy(x)
for a, b, c in es:
        a, b = a-1, b-1
        x[b] = max(x[b], x[a]+c)

if prevx != x:
    print('inf')
else:
    print(x[n-1])

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy

n, m = [int(i) for i in input().split()]
es = [[int(j) for j in input().split()] for i in range(m)]

x = [float('-inf')] * n
x[0] = 0
border = sys.maxsize // 10

for i in range(n):
    for a, b, c in es:
        a, b = a-1, b-1
        x[b] = max(x[b], x[a]+c)
        if x[b] >= border:
            x[b] = float('inf')

prevx = deepcopy(x)
for a, b, c in es:
        a, b = a-1, b-1
        x[b] = max(x[b], x[a]+c)

if prevx != x:
    print('inf')
else:
    print(x[n-1])

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy

n, m = [int(i) for i in input().split()]
es = [[int(j) for j in input().split()] for i in range(m)]

x = [float('-inf')] * n
x[0] = 0

for i in range(n):
    for a, b, c in es:
        a, b = a-1, b-1
        x[b] = max(x[b], x[a]+c)
        if x[b] >= sys.maxsize:
            x[b] = float('inf')

prevx = deepcopy(x)
for a, b, c in es:
        a, b = a-1, b-1
        x[b] = max(x[b], x[a]+c)

if prevx != x:
    print('inf')
else:
    print(x[n-1])

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy

n, m = [int(i) for i in input().split()]
es = [[int(j) for j in input().split()] for i in range(m)]

x = [-sys.maxsize - 1] * n
x[0] = 0

for i in range(n):
    for a, b, c in es:
        a, b = a-1, b-1
        x[b] = max(x[b], x[a]+c)

prevx = deepcopy(x)
for a, b, c in es:
        a, b = a-1, b-1
        x[b] = max(x[b], x[a]+c)

if prevx != x:
    print('inf')
else:
    print(x[n-1])

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
INF = float('inf')
es = [tuple(map(int,input().split())) for i in range(M)]

ds = [INF] * N
ds[0] = 0

for i in range(N):
    update = False
    for a,b,c in es:
        a,b,c = a-1,b-1,-c
        if ds[a] != INF and ds[b] > ds[a] + c:
            ds[b] = ds[a] + c
            update = True
    if update and i == N-1:
        print('inf')
        exit()

print(-ds[-1])

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
INF = float('inf')
es = [tuple(map(int,input().split())) for i in range(M)]

ds = [INF] * N
ds[0] = 0

for i in range(N):
    update = False
    for a,b,c in es:
        a,b,c = a-1,b-1,-c
        if ds[a] != INF and ds[b] > ds[a] + c:
            ds[b] = ds[a] + c
            update = True
    if update:
        if i == N-1:
            print('inf')
            exit()
    else:
        print(-ds[-1])
        exit()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return tuple(map(int, input().split()))
INF = float("inf")
V, E = inpl()
 
edges = [inpl() for _ in range(E)]
 
def belman_ford(V, edges, r):
    "If glaph contains negative cycles then return True"
    D = [-INF]*(V+1)
    D[r] = 0
#    if V == 1: # 今回は常にV>=2
#        return False, [0]
    
    for _ in range(V): # 最長でも1からNへの移動はN-1回なので負の閉路がなければN回目までには必ずアップデートされない
        update = False
        for s, t, d in edges:
            if D[t] < D[s] + d:
                D[t] = D[s] + d
                update = True
        if not update:
            break
    else:
        return True, D
    return False, D
    
NEGATIVE, D = belman_ford(V, edges, 1)
 
if NEGATIVE:
    print("inf")
else:
    print(D[-1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return tuple(map(int, input().split()))
INF = float("inf")
V, E = inpl()

edges = [inpl() for _ in range(E)]

def belman_ford(V, edges, r):
    "If glaph contains negative cycles then return True"
    D = [-INF]*(V+1)
    D[r] = 0
#    if V == 1: # 今回は常にV>=2
#        return False, [0]
    
    for _ in range(E-1): # 最長でも1からNへの移動はN-1回じゃない？
        update = False
        for s, t, d in edges:
            if D[t] < D[s] + d:
                D[t] = D[s] + d
                update = True
        if not update:
            break
    else:
        return True, D
    return False, D
    
NEGATIVE, D = belman_ford(V, edges, 1)

if NEGATIVE:
    print("inf")
else:
    print(D[-1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return tuple(map(int, input().split()))
INF = float("inf")
V, E = inpl()

edges = [inpl() for _ in range(E)]

def belman_ford(V, edges, r):
    "If glaph contains negative cycles then return True"
    D = [-INF]*(V+1)
    D[r] = 0
#    if V == 1: # 今回は常にV>=2
#        return False, [0]
    
    for _ in range(V-1): # 最長でも1からNへの移動はN-1回
        update = False
        for s, t, d in edges:
            if D[t] < D[s] + d:
                D[t] = D[s] + d
                update = True
        if not update:
            break
    else:
        return True, D
    return False, D
    
NEGATIVE, D = belman_ford(V, edges, 1)

if NEGATIVE:
    print("inf")
else:
    print(D[-1])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
edge = []
for i in range(M):
    a, b, c = map(int, input().split())
    edge.append([a, b, c*-1])
d = [float('inf')] * (N+1)
d[0], d[1] = -1, 0
for i in range(N):
    update = False
    for e in edge:
        if (d[e[0]] != float('inf')) & (d[e[1]] > d[e[0]] + e[2]):
            d[e[1]] = d[e[0]] + e[2]
            update = True
    if not update:
        break
else:
    print('inf')
    exit()

print(d[N]*-1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
a = [0 for i in range(M)]
b = [0 for i in range(M)]
c = [0 for i in range(M)]
for i in range(M):
    a[i], b[i], c[i] = map(int, input().split())
distance = [-10**20 for i in range(N)]
distance[0] = 0
for i in range(1,N):
    for j in range(M):
        u = a[j]-1; v = b[j]-1
        if distance[v] <= distance[u] + c[j]:
            distance[v] = distance[u] + c[j]
flag = 0
for j in range(M):
    u = a[j]-1; v = b[j]-1
    if distance[u] + c[j] > distance[v]:
        flag = 1
        break
if flag == 0:
    print(distance[N-1])
else:
    print("inf")

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
a = [0 for i in range(M)]
b = [0 for i in range(M)]
c = [0 for i in range(M)]
for i in range(M):
    a[i], b[i], c[i] = map(int, input().split())
distance = [-10**20 for i in range(N)]
predecessor = [-1 for i in range(N)]
distance[0] = 0

for i in range(N):
    for j in range(M):
        u = a[j]-1; v = b[j]-1
        if distance[v] < distance[u] + c[j]:
            distance[v] = distance[u] + c[j]
            predecessor[v] = u
flag = 0
for j in range(M):
    u = a[j]-1; v = b[j]-1
    if distance[u] + c[j] > distance[v]:
        flag = 1
        break
if flag == 0:
    print(distance[N-1])
else:
    print("inf")

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
a = [0 for i in range(M)]
b = [0 for i in range(M)]
c = [0 for i in range(M)]
for i in range(M):
    a[i], b[i], c[i] = map(int, input().split())
    a[i] -= 1; b[i] -= 1; c[i] *= -1
distance = [10**20 for i in range(N)]
predecessor = [-1 for i in range(N)]
distance[0] = 0

for i in range(2*N):
    for j in range(M):
        u = a[j]; v = b[j]
        if distance[v] > distance[u] + c[j]:
            distance[v] = distance[u] + c[j]
            predecessor[v] = u
flag = 0
for j in range(M):
    u = a[j]; v = b[j]
    if distance[u] + c[j] < distance[v]:
        flag = 1
        break
if flag == 0:
    print(-distance[N-1])
else:
    print("inf")

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
a = [0 for i in range(M)]
b = [0 for i in range(M)]
c = [0 for i in range(M)]
for i in range(M):
    a[i], b[i], c[i] = map(int, input().split())
    a[i] -= 1; b[i] -= 1; c[i] *= -1
distance = [10**20 for i in range(N)]
predecessor = [-1 for i in range(N)]
distance[0] = 0

for i in range(N):
    for j in range(M):
        u = a[j]; v = b[j]
        if distance[v] > distance[u] + c[j]:
            distance[v] = distance[u] + c[j]
            predecessor[v] = u
flag = 0
for j in range(M):
    u = a[j]; v = b[j]
    if distance[u] + c[j] < distance[v]:
        flag = 1
        break
if flag == 0:
    print(-distance[N-1])
else:
    print("inf")

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
a = [0 for i in range(M)]
b = [0 for i in range(M)]
c = [0 for i in range(M)]
for i in range(M):
    a[i], b[i], c[i] = map(int, input().split())
    a[i] -= 1; b[i] -= 1; c[i] *= -1
distance = [10**20 for i in range(N)]
predecessor = [-1 for i in range(N)]
distance[0] = 0

for i in range(N):
    for j in range(M):
        u = a[j]; v = b[j]
        if distance[v] > distance[u] + c[j]:
            distance[v] = distance[u] + c[j]
            predecessor[v] = u
flag = 0
for j in range(M):
    u = a[j]; v = b[j]
    if distance[u] + c[j] < distance[v]:
        print("Inf")
        flag = 1
        break
if flag == 0:
    print(-distance[N-1])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
a = [0 for i in range(M)]
b = [0 for i in range(M)]
c = [0 for i in range(M)]
for i in range(M):
    a[i], b[i], c[i] = map(int, input().split())
    a[i] -= 1; b[i] -= 1; c[i] *= -1
distance = [10**20 for i in range(N)]
predecessor = [-1 for i in range(N)]
distance[0] = 0

for i in range(N):
    for j in range(M):
        u = a[j]; v = b[j]
        if distance[v] > distance[u] + c[j]:
            distance[v] = distance[u] + c[j]
            predecessor[v] = u
flag = 0
for j in range(M):
    u = a[j]; v = b[j]
    if distance[u] + c[j] < distance[v]:
        print("inf")
        flag = 1
        break
if flag == 0:
    print(-distance[N-1])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
a = [0 for i in range(M)]
b = [0 for i in range(M)]
c = [0 for i in range(M)]
for i in range(M):
    a[i], b[i], c[i] = map(int, input().split())
    a[i] -= 1; b[i] -= 1; c[i] *= -1
distance = [10**20 for i in range(N)]
predecessor = [-1 for i in range(N)]
distance[0] = 0

for i in range(N-1):
    for j in range(M):
        u = a[j]; v = b[j]
        if distance[v] > distance[u] + c[j]:
            distance[v] = distance[u] + c[j]
            predecessor[v] = u
flag = 0
for j in range(M):
    u = a[j]; v = b[j]
    if distance[u] + c[j] < distance[v]:
        print("inf")
        flag = 1
        break
if flag == 0:
    print(-distance[N-1])

Submission status: Wrong Answer

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

distance = [10**17 for i in range(N)]
predecessor = [-1 for i in range(N)]
distance[0] = 0

for i in range(N-1):
    for j in range(M):
        u = a[j]; v = b[j]
        if distance[v] > distance[u] + c[j]:
            distance[v] = distance[u] + c[j]
            predecessor[v] = u
flag = 0
for j in range(M):
    u = a[j]; v = b[j]
    if distance[u] + c[j] < distance[v]:
        print("inf")
        flag = 1
        break
if flag == 0:
    print(-distance[N-1])

Submission status: Wrong Answer

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

distance = [10**17 for i in range(N)]
predecessor = [-1 for i in range(N)]
distance[0] = 0

for i in range(N-1):
    for j in range(M):
        u = a[j]; v = b[j]
        if distance[v] > distance[u] + c[j]:
            distance[v] = distance[u] + c[j]
            predecessor[v] = u

for j in range(M):
    u = a[j]; v = b[j]
    if distance[u] + c[j] < distance[v]:
        print("inf")
        exit()
print(-distance[N-1])

Submission status: Wrong Answer

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

d[0] = 0
for i in range(N-1):
    for j in range(M):
        d[b[j]] = max((d[b[j]], d[a[j]] + c[j]))

isCycle = False
for i in range(N-1):
    if isCycle:
        break
    for j in range(M):
        if j == N-1 and d[b[j]] < d[a[j]] + c[j]:
            isCycle = True
            break
        d[b[j]] = max((d[b[j]], d[a[j]] + c[j]))
if isCycle:
    print('inf')
else:
    print(d[N-1])

Submission status: Wrong Answer

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

d[0] = 0
for i in range(N-1):
    for j in range(M):
        d[b[j]] = max((d[b[j]], d[a[j]] + c[j]))

flg = any([b[i] == N-1 and d[b[i]] < d[a[i]] + c[i] for _ in range(N-1) for i in range(M)])

if flg:
    print('inf')
else:
    print(d[N-1])

Submission status: Wrong Answer

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

d[0] = 0
for i in range(N-1):
    for j in range(M):
        d[b[j]] = max((d[b[j]], d[a[j]] + c[j]))

flg = any([d[b[i]] < d[a[i]] + c[i] for i in range(M)])

if flg:
    print('inf')
else:
    print(d[N-1])

Submission status: Wrong Answer

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

d[0] = 0
for i in range(N-1):
    for j in range(M):
        d[b[j]] = max((d[b[j]], d[a[j]] + c[j]))

flg = False
for i in range(M):
    if d[b[i]] < d[a[i]] + c[i]:
        flg = True

if flg:
    print('inf')
else:
    print(d[N-1])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
a, b, c = [0] * M, [0] * M, [0] * M
INF = int(1e13) + 1
for i in range(M):
    a[i], b[i], c[i] = map(int, input().split())
    a[i] -= 1
    b[i] -= 1

d = [-INF] * N
d[0] = 0
for __x in range(N):
    for i in range(M):
        if d[b[i]] < d[a[i]] + c[i]:
            d[b[i]] = d[a[i]] + c[i]

isCycle = False
for i in range(M):
    if d[b[i]] < d[a[i]] + c[i]:
        d[b[i]] = d[a[i]] + c[i]
        isCycle = True
if isCycle:
    print('inf')
else:
    print(d[N-1])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
a, b, c = [0] * M, [0] * M, [0] * M
INF = int(1e17) + 1
for i in range(M):
    a[i], b[i], c[i] = map(int, input().split())
    a[i] -= 1
    b[i] -= 1

d = [-INF] * N
d[0] = 0
for __x in range(N-1):
    for i in range(M):
        if d[b[i]] < d[a[i]] + c[i]:
            d[b[i]] = d[a[i]] + c[i]

isCycle = False
for i in range(M):
    if d[b[i]] < d[a[i]] + c[i]:
        d[b[i]] = d[a[i]] + c[i]
        isCycle = True
if isCycle:
    print('inf')
else:
    print(d[N-1])

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int,raw_input().split())
a=[]
for i in range(M):
	x,y,z=map(int,raw_input().split())
	a.append((x,y,-1*z))

#print a

#distance
x=[ float("inf") for _ in range(N+1)]
#Set start point value to zero. 
x[1]=0


#Bellman-Ford
for h in range(N):
	for fr,to,cost in a:
		x[to]=min(x[to],x[fr]+cost)

			
print -1*x[N]

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int,raw_input().split())
a=[]
for i in range(M):
	x,y,z=map(int,raw_input().split())
	a.append((x,y,-1*z))

#print a

#distance
x=[ float("inf") for _ in range(N+1)]
#Set start point value to zero. 
x[1]=0


#Bellman-Ford
for h in range(N):
	for fr,to,cost in a:
		x[to]=min(x[to],x[fr]+cost)


update=[ False for _ in range(N+1)]

#Judge inf
for h in range(1):
	for fr,to,cost in a:
		if x[to]>x[fr]+cost:
			x[to]=x[fr]+cost
			update[to]=True
		if update[fr]:
			update[to]=True


if True in update:
	print "inf"
else:				
	print -1*x[N]

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int,raw_input().split())
a=[]
for i in range(M):
	x,y,z=map(int,raw_input().split())
	a.append((x,y,z))

#print a

#distance
x=[ float("-inf") for _ in range(N+1)]
#Set start point value to zero. 
x[1]=0


#Bellman-Ford
for h in range(N):
	for fr,to,cost in a:
		x[to]=max(x[to],x[fr]+cost)


update=[ False for _ in range(N+1)]

#Judge inf
for h in range(N):
	for fr,to,cost in a:
		if x[to]<x[fr]+cost:
			x[to]=x[fr]+cost
			update[to]=True
		if update[fr]==True:
			update[to]=True

if True in update:
	print "inf"
else:				
	print x[N]

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int,raw_input().split())
a=[]
for i in range(M):
	x,y,z=map(int,raw_input().split())
	a.append((x,y,z))

#print a

#distance
x=[ float("-inf") for _ in range(N+1)]
#Set start point value to zero. 
x[1]=0


#Bellman-Ford
for h in range(N):
	for i in a:
		x[i[1]]=max(x[i[1]],x[i[0]]+i[2])

#Judge inf
for h in range(N):
	for i in a:
		if x[i[1]]<x[i[0]]+i[2]:
			print "inf"
			sys.exit()

print x[N]

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int,raw_input().split())
a=[]
for i in range(M):
	x,y,z=map(int,raw_input().split())
	a.append((x,y,z))

#print a

#distance
x=[ float("-inf") for _ in range(N+1)]
#Set start point value to zero. 
x[1]=0


#Bellman-Ford
for h in range(N):
	for i in a:
		x[i[1]]=max(x[i[1]],x[i[0]]+i[2])

#Judge inf
for h in range(1):
	for i in a:
		if x[i[1]]<x[i[0]]+i[2]:
			print "inf"
			sys.exit()

print x[N]

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int,raw_input().split())
a=[[] for _ in range(N+1)]
for i in range(M):
	x,y,z=map(int,raw_input().split())
	a[x].append((y,z))

#print a

#distance
x=[ float("-inf") for _ in range(N+1)]
#Set start point value to zero. 
x[1]=0

#Bellman-Ford
for h in range(M):
	for i in range(1,N+1):
		for j in a[i]:
			x[j[0]]=max(x[j[0]],x[i]+j[1])



#Bellman-Ford
for h in range(M):
	for i in range(1,N+1):
		for j in a[i]:
			if x[j[0]] < x[i]+j[1]:
				print "inf"
				sys.exit()

print x[N]

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int,raw_input().split())
a=[[] for _ in range(N+1)]
for i in range(M):
	x,y,z=map(int,raw_input().split())
	a[x].append((y,z))

#print a

#distance
x=[ float("-inf") for _ in range(N+1)]
#Set start point value to zero. 
x[1]=0

#Bellman-Ford
for h in range(N):
	for i in range(1,N+1):
		for j in a[i]:
			x[j[0]]=max(x[j[0]],x[i]+j[1])



#Bellman-Ford
for h in range(N):
	for i in range(1,N+1):
		for j in a[i]:
			if x[j[0]] < x[i]+j[1]:
				print "inf"
				sys.exit()

print x[N]

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int,raw_input().split())
a=[[] for _ in range(N+1)]
for i in range(M):
	x,y,z=map(int,raw_input().split())
	a[x].append((y,z))

#print a

#distance
x=[ float("-inf") for _ in range(N+1)]
#Set start point value to zero. 
x[1]=0

#Bellman-Ford
for i in range(1,N+1):
	for j in a[i]:
		x[j[0]]=max(x[j[0]],x[i]+j[1])


#Judge inf
for i in range(1,N+1):
	for j in a[i]:
		if x[j[0]]<x[i]+j[1]:
			print "inf"
			sys.exit()

print x[N]

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import heapq
N,M=map(int,raw_input().split())
a={}
for i in range(M):
	x,y,z=map(int,raw_input().split())
	#print x,y,z
	if x in a:
		a[x].extend([(-z,y)])
	else:
		a[x]=[(-z,y)]		

#print a

if N in a.keys():
	print "inf"
	sys.exit()

def djk(n):
	Start=n
	#d=[0 for i in range(N+1)]
	d={}
	d[Start]=0

	#優先度付きキューに[距離,頂点の番号]を格納する
	#スタート地点には距離0を入れる
	q=[]
	heapq.heappush(q, [0,Start])

	while len(q)!=0:

		#距離が最小の頂点を取り出し
		l,u=heapq.heappop(q)
		for i in a[u]:
			v=i[1]
			l_u_v=i[0]
			#print v, l_u_v
			if not v in d:
				d[v]=d[u]+l_u_v
				if v in a:
					heapq.heappush(q, [d[v],v])
			elif d[v]>d[u]+l_u_v:
				d[v]=d[u]+l_u_v
				if v in a:
					heapq.heappush(q, [d[v],v])
	#print d
	return -1*d[N]



print djk(1)