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())
edges = [set() for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    edges[a - 1].add((b - 1, c))

NINF = float('-inf')
dists = [NINF for _ in range(n)]
dists[0] = 0

for i in range(n):
    updated = False
    n_updated = False
    for j in range(n):
        dj = dists[j]
        if dj == NINF:
            continue
        for b, c in edges[j]:
            if dists[b] >= dj + c:
                continue
            dists[b] = dj + c
            updated = True
            n_updated = b == n
    if not updated:
        print(dists[-1])
        break
else:
    print('inf' if n_updated else dists[-1])

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
edges = [set() for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    edges[a - 1].add((b - 1, c))

NINF = float('-inf')
dists = [NINF for _ in range(n)]
dists[0] = 0

for i in range(n):
    updated = False
    for j in range(n):
        dj = dists[j]
        if dj == NINF:
            continue
        for b, c in edges[j]:
            if dists[b] >= dj + c:
                continue
            dists[b] = dj + c
            updated = True
    if not updated:
        print(dists[-1])
        break
else:
    print('inf')

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
edges = [set() for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    edges[a - 1].add((b - 1, c))

NINF = float('-inf')
dists = [NINF for _ in range(n)]
dists[0] = 0

for i in range(n):
    updated = False
    for j in range(n):
        if dists[j] == NINF:
            continue
        for b, c in edges[j]:
            if dists[b] >= dists[j] + c:
                continue
            dists[b] = dists[j] + c
            updated = True
    if not updated:
        print(dists[n - 1])
        break
    if i == n - 1:
        print('inf')

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
abc = [list(map(int, input().split())) for x in range(m)]
nodes = [[] for x in range(n + 1)]

for a, b, c in abc:
    nodes[a].append([a, b, -c])

costs = [None for x in range(n + 1)]
costs[1] = 0
flg = True
for i in range(n):
    for a, b, c in abc:
        if costs[a] == None:
            continue
        if costs[b] == None or costs[b] < costs[a] + c:
            flg = False
            costs[b] = costs[a] + c
    # for node in nodes[1:]:
    #     for a, b, c in node:
    #         if costs[a] == None:
    #             continue
    #         if costs[b] == None or costs[b] > costs[a] + c:
    #             flg = False
    #             costs[b] = costs[a] + c
    if flg:
        print(costs[n])
        break
    flg = True
else:
    print("inf")

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
abc = [list(map(int, input().split())) for x in range(m)]
nodes = [[] for x in range(n + 1)]

for a, b, c in abc:
    nodes[a].append([a, b, -c])

costs = [None for x in range(n + 1)]
costs[1] = 0
flg = True
for i in range(n * 2 - 1):
    for node in nodes[1:]:
        for a, b, c in node:
            if costs[a] == None:
                continue
            if costs[b] == None or costs[b] > costs[a] + c:
                flg = False
                costs[b] = costs[a] + c
    if flg:
        print(-costs[n])
        break
    flg = True
else:
    print("inf")

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
abc = [list(map(int, input().split())) for x in range(m)]
nodes = [[] for x in range(n + 1)]

for a, b, c in abc:
    nodes[a].append([a, b, -c])

costs = [None for x in range(n + 1)]
costs[1] = 0
flg = True
for i in range(n):
    for node in nodes[1:]:
        for a, b, c in node:
            if costs[a] == None:
                continue
            if costs[b] == None or costs[b] > costs[a] + c:
                flg = False
                costs[b] = costs[a] + c
    if flg:
        print(-costs[n])
        break
    flg = True
else:
    print("inf")

Submission status: Wrong Answer

Solution code:
n, m = map(int, raw_input().split())
graph = {}
visited = {}
# 0: not visited, 1: visited
scores = {}
for i in range(n):
    if i == 0:
        scores[i+1] = 0
    else:
        scores[i+1] = float("inf")
    graph[i+1] = {}

for i in range(m):
    a, b, c = map(int, raw_input().split())
    graph[a][b] = -c
    # {1: {2: 4, 3: 5}, 2: {3: 4}}

for v in graph.keys():
    for e in graph[v].keys():
        if scores[e] > scores[v] + graph[v][e]:
            scores[e] = scores[v] + graph[v][e]

ans = -scores[n]
negative = {}
for i in range(n):
    negative[i+1] = False

for v in graph.keys():
    for e in graph[v].keys():
        if scores[v] + graph[v][e] < scores[e]:
            scores[e] = scores[v] + graph[v][e]
            negative[e] = True
        if negative[v]:
            negative[e] = True

if negative[n]:
    print 'inf'
else:
    print ans

Submission status: Wrong Answer

Solution code:
n, m = map(int, raw_input().split())
graph = {}
visited = {}
# 0: not visited, 1: visited
scores = {}
for i in range(n):
    scores[i+1] = 0
    graph[i+1] = {}

for i in range(m):
    a, b, c = map(int, raw_input().split())
    graph[a][b] = c
    # {1: {2: 4, 3: 5}, 2: {3: 4}}

for v in graph.keys():
    for e in graph[v].keys():
        if scores[e] < scores[v] + graph[v][e]:
            scores[e] = scores[v] + graph[v][e]

ans = scores[n]
positive = {}
for i in range(n):
    positive[i+1] = False

for v in graph.keys():
    for e in graph[v].keys():
        if scores[v] + graph[v][e] > scores[e]:
            scores[e] = scores[v] + graph[v][e]
            positive[e] = True
        if positive[v]:
            positive[e] = True

if positive[n]:
    print 'inf'
else:
    print ans

Submission status: Wrong Answer

Solution code:
n, m = map(int, raw_input().split())
graph = {}
visited = {}
# 0: not visited, 1: visited
scores = {}
for i in range(n):
    if i == 0:
        scores[i+1] = 0
    else:
        scores[i+1] = -float('inf')
    graph[i+1] = {}

for i in range(m):
    a, b, c = map(int, raw_input().split())
    graph[a][b] = c
    # {1: {2: 4, 3: 5}, 2: {3: 4}}

for v in graph.keys():
    for e in graph[v].keys():
        if scores[e] < scores[v] + graph[v][e]:
            scores[e] = scores[v] + graph[v][e]

ans = scores[n]
positive = {}
for i in range(n):
    positive[i+1] = False

for v in graph.keys():
    for e in graph[v].keys():
        if scores[v] + graph[v][e] > scores[e]:
            scores[e] = scores[v] + graph[v][e]
            positive[e] = True
        if positive[v]:
            positive[e] = True

if positive[n]:
    print 'inf'
else:
    print ans

Submission status: Wrong Answer

Solution code:
n, m = map(int, raw_input().split())
graph = {}
visited = {}
# 0: not visited, 1: visited
scores = {}
for i in range(n):
    if i == 0:
        scores[i+1] = 0
    else:
        scores[i+1] = -float('inf')
    graph[i+1] = {}

for i in range(m):
    a, b, c = map(int, raw_input().split())
    graph[a][b] = c
    # {1: {2: 4, 3: 5}, 2: {3: 4}}

for v in graph.keys():
    for e in graph[v].keys():
        if scores[e] < scores[v] + graph[v][e]:
            scores[e] = scores[v] + graph[v][e]

flag = 0
# if inf, flag = 1
for e in graph[v].keys():
    if scores[v] + graph[v][e] > scores[e]:
        print 'inf'
        flag = 1
        break
if flag == 0:
    print scores[n]

Submission status: Wrong Answer

Solution code:
n, m = map(int, raw_input().split())
graph = {}
visited = {}
# 0: not visited, 1: visited
scores = {}
for i in range(n):
    if i == 0:
        scores[i+1] = 0
    else:
        scores[i+1] = -float('inf')
    graph[i+1] = {}

for i in range(m):
    a, b, c = map(int, raw_input().split())
    graph[a][b] = c
    # {1: {2: 4, 3: 5}, 2: {3: 4}}

for v in graph.keys():
    for e in graph[v].keys():
        if scores[e] < scores[v] + graph[v][e]:
            scores[e] = scores[v] + graph[v][e]

flag = 0
# if inf, flag = 1
for v in graph.keys():
    for e in graph[v].keys():
        if scores[v] + graph[v][e] > scores[e]:
            print 'inf'
            flag = 1
            break
if flag == 0:
    print scores[n]

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
e=[tuple(map(int,input().split())) for _ in range(m)]
d=[0]+[-float('inf')]*n
for _ in range(n):
    f=True
    for a,b,c in e:
        if d[b-1]<d[a-1]+c:
            d[b-1]=d[a-1]+c
            f=False
    if f:
        print(d[n-1])
        break
else:
    print('inf')

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int, input().split())
a=[]
b=[]
c=[]
D=0
for m in range (0,M):
  A,B,C=map(int, input().split() )
  a+=[A]
  b+=[B]
  c+=[C]
  D=max(D,C)
V=[0]+[-D*1000]*(N-1)# iに到達するときの最大得点
 
for n in range(N-1):
  for m in range(M):
    u=a[m]
    v=b[m]
    if V[b[m]-1]<V[a[m]-1]+c[m]:#共に有限の時はやる
      V[b[m]-1]=V[a[m]-1]+c[m]
 
moji2=[0]*N
for n in range(N):
  for m in range(M):
    u=a[m]
    v=b[m]
    if V[b[m]-1]<V[a[m]-1]+c[m]:#共に有限の時はやる
        V[b[m]-1]=V[a[m]-1]+c[m]
        moji2[b[m]-1]=1
if moji2[N-1]:
  print("inf")
else:
  print(V[N-1])

Submission status: Wrong Answer

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


def min_path(_s):
    d[_s] = 0
    while True:
        update = False
        for k in range(0, M):
            edge = abc[k]
            if d[edge.fromE - 1] != float("inf") and d[edge.to - 1] > d[edge.fromE - 1] + edge.cost:
                d[edge.to - 1] = d[edge.fromE - 1] + edge.cost
                update = True
        if not update:
            break


def find_negative_loop():
    d = [0 for i in range(N)]
    for i in range(0, N):
        for j in range(0, M):
            edge = abc[j]
            if d[edge.to - 1] > d[edge.fromE - 1] + edge.cost:
                d[edge.to - 1] = d[edge.fromE - 1] + edge.cost

                if i == N - 1:
                    return True

    return False


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

abc = []

for i in range(M):
    a, b, c = list(map(int, input().split()))
    abc.append(Edge(a, b, -c))

if find_negative_loop():
    print('inf')
else:
    d = [float("inf") for i in range(N)]
    min_path(0)
    ans = -d[N - 1]
    print(ans)

Submission status: Wrong Answer

Solution code:
# Bellman-Ford
n, m = map(int, raw_input().split())
a, b, c = [], [], []
for i in xrange(m):
    at, bt, ct = map(int, raw_input().split())
    a.append(at - 1)
    b.append(bt - 1)
    c.append(ct)
# step1 : initialize
point = [-1e9 * m - 1] * n
point[0] = 0
# step2 : relaxation
for i in xrange(2*n):
    for j in xrange(m):
        if point[b[j]] < point[a[j]] + c[j]:
            point[b[j]] = point[a[j]] + c[j]
            # step3 : check
            if i >= n:
                print("inf")
                exit()
print(point[n-1])

Submission status: Wrong Answer

Solution code:
# Bellman-Ford
n, m = map(int, raw_input().split())
a, b, c = [], [], []
for i in xrange(m):
    at, bt, ct = map(int, raw_input().split())
    a.append(at - 1)
    b.append(bt - 1)
    c.append(ct)
# step1 : initialize
point = [-1e9 * m - 1] * n
point[0] = 0
# step2 : relaxation
for i in xrange(n+1):
    for j in xrange(m):
        if point[b[j]] < point[a[j]] + c[j]:
            point[b[j]] = point[a[j]] + c[j]
            # step3 : check
            if i == n:
                print("inf")
                exit()
print(point[n-1])

Submission status: Wrong Answer

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


def min_path(_s):
    d[_s] = 0
    while True:
        update = False
        for i in range(0, M):
            edge = abc[i]
            if d[edge.fromE - 1] != float("inf") and d[edge.to - 1] > d[edge.fromE - 1] + edge.cost:
                d[edge.to - 1] = d[edge.fromE - 1] + edge.cost
                update = True
        if not update:
            break


def find_negative_loop():
    d = [0 for i in range(N)]
    for i in range(0, N):
        for j in range(0, M):
            edge = abc[j]
            if d[edge.to - 1] > d[edge.fromE - 1] + edge.cost:
                d[edge.to - 1] = d[edge.fromE - 1] + edge.cost

                if i == N - 1:
                    return True

    return False


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

abc = []

for i in range(M):
    a, b, c = list(map(int, input().split()))
    abc.append(Edge(a, b, -c))

if find_negative_loop():
    print('inf')
else:
    d = [float("inf") for i in range(N)]
    min_path(0)
    ans = -d[N - 1]
    print(ans)

Submission status: Wrong Answer

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


def min_path(_s):
    d[_s] = 0
    while True:
        update = False
        for i in range(0, M):
            edge = abc[i]
            if d[edge.fromE - 1] != float("inf") and d[edge.to - 1] > d[edge.fromE - 1] + edge.cost:
                d[edge.to - 1] = d[edge.fromE - 1] + edge.cost
                update = True
        if not update:
            break;


def find_negative_loop():
    d = [0 for i in range(N + 1)]
    for i in range(0, N):
        for j in range(0, M):
            edge = abc[j]
            if d[edge.to - 1] > d[edge.fromE - 1] + edge.cost:
                d[edge.to - 1] = d[edge.fromE - 1] + edge.cost

                if i == N - 1:
                    return True

    return False


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

abc = []

for i in range(M):
    a, b, c = list(map(int, input().split()))
    abc.append(Edge(a, b, -c))

if find_negative_loop():
    print('inf')
else:
    d = [float("inf") for i in range(N)]
    min_path(0)
    ans = -d[N - 1]
    print(ans)

Submission status: Wrong Answer

Solution code:
# Bellman-Ford
n, m = map(int, raw_input().split())
a, b, c = [], [], []
for i in xrange(m):
    at, bt, ct = map(int, raw_input().split())
    a.append(at - 1)
    b.append(bt - 1)
    c.append(ct)
# step1 : initialize
point = [-1e9 * m - 1] * n
point[0] = 0
pointl = [None] * n
# step2 : relaxation
for i in xrange(n+1):
    for j in xrange(m):
        if point[b[j]] < point[a[j]] + c[j]:
            point[b[j]] = point[a[j]] + c[j]
            pointl[b[j]] = a[j]
            if i == n:
                print("inf")
                exit()
# step3 : check
# for j in xrange(m):
#     if point[a[j]] + c[j] > point[b[j]]:
#         print("inf")
#         exit()
print(int(point[n-1]))

Submission status: Wrong Answer

Solution code:
# Bellman-Ford
n, m = map(int, raw_input().split())
a, b, c = [], [], []
for i in xrange(m):
    at, bt, ct = map(int, raw_input().split())
    a.append(at - 1)
    b.append(bt - 1)
    c.append(ct)
# step1 : initialize
point = [-1e9 * m - 1] * n
point[0] = 0
pointl = [None] * n
# step2 : relaxation
for i in xrange(n):
    for j in xrange(m):
        if point[b[j]] < point[a[j]] + c[j]:
            point[b[j]] = point[a[j]] + c[j]
            pointl[b[j]] = a[j]
# step3 : check
flag = True
for j in xrange(m):
    if point[a[j]] + c[j] > point[b[j]]:
        flag = False
        break
if flag:
    print(int(point[n-1]))
else:
    print("inf")

Submission status: Wrong Answer

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


def min_path(_s):
    d[_s] = 0
    while True:
        update = False
        for i in range(0, M):
            edge = abc[i]
            if d[edge.fromE] != float("inf") and d[edge.to] > d[edge.fromE] + edge.cost:
                d[edge.to] = d[edge.fromE] + edge.cost
                update = True
        if not update:
            break;


def find_negative_loop():
    d = [0 for i in range(N + 1)]
    for i in range(0, N):
        for j in range(0, M):
            edge = abc[j]
            if d[edge.to] > d[edge.fromE] + edge.cost:
                d[edge.to] = d[edge.fromE] + edge.cost

                if i == N - 1:
                    return True

    return False


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

abc = []

for i in range(M):
    a, b, c = list(map(int, input().split()))
    abc.append(Edge(a, b, -c))

if find_negative_loop():
    print('inf')
else:
    d = [float("inf") for i in range(N + 1)]
    min_path(1)
    ans = -d[N]
    print(ans)

Submission status: Wrong Answer

Solution code:
# Bellman-Ford
n, m = map(int, raw_input().split())
a, b, c = [], [], []
for i in xrange(m):
    at, bt, ct = map(int, raw_input().split())
    a.append(at - 1)
    b.append(bt - 1)
    c.append(ct)
# step1 : initialize
point = [-1e10 * m - 1] * n
point[0] = 0
pointl = [None] * n
# step2 : relaxation
for i in xrange(n-1):
    for j in xrange(m):
        if point[b[j]] < point[a[j]] + c[j]:
            point[b[j]] = point[a[j]] + c[j]
            pointl[b[j]] = a[j]
# step3 : check
flag = True
for j in xrange(m):
    if point[a[j]] + c[j] > point[b[j]]:
        flag = False
        break
if flag:
    print(int(point[n-1]))
else:
    print("inf")

Submission status: Wrong Answer

Solution code:
# Bellman-Ford
n, m = map(int, raw_input().split())
a, b, c = [], [], []
for i in xrange(m):
    at, bt, ct = map(int, raw_input().split())
    a.append(at - 1)
    b.append(bt - 1)
    c.append(ct)
# step1 : initialize
point = [-1e9 * m - 1] * n
point[0] = 0
pointl = [None] * n
# step2 : relaxation
for i in xrange(n-1):
    for j in xrange(m):
        if point[b[j]] < point[a[j]] + c[j]:
            point[b[j]] = point[a[j]] + c[j]
            pointl[b[j]] = a[j]
# step3 : check
flag = True
for j in xrange(m):
    if point[a[j]] + c[j] > point[b[j]]:
        flag = False
        break
if flag:
    print(int(point[n-1]))
else:
    print("inf")

Submission status: Wrong Answer

Solution code:
# Bellman-Ford
n, m = map(int, raw_input().split())
a, b, c = [], [], []
for i in xrange(m):
    at, bt, ct = map(int, raw_input().split())
    a.append(at - 1)
    b.append(bt - 1)
    c.append(ct)
# step1 : initialize
point = [-1e9 * m - 1] * n
point[0] = 0
pointl = [None] * n
# step2 : relaxation
for i in xrange(n-1):
    for j in xrange(m):
        if point[b[j]] < point[a[j]] + c[j]:
            point[b[j]] = point[a[j]] + c[j]
            pointl[b[j]] = a[j]
# step3 : check
flag = True
for j in xrange(m):
    if point[a[j]] + c[j] > point[b[j]]:
        flag = False
        break
if flag:
    print(point[n-1])
else:
    print("inf")

Submission status: Wrong Answer

Solution code:
import math

INF = float("inf")

V, E = map(int, input().split())
es = []

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

d = [INF] * V
d[0] = 0
cnt = [0] * V

for _ in range(V):
    updated = False
    for e in es:
        if math.isinf(d[e[0]]):
            continue
        if d[e[1]] > d[e[0]] + e[2]:
            d[e[1]] = d[e[0]] + e[2]
            cnt[e[1]] += 1
            updated = True
    if not updated:
        break

print(-d[V-1] if cnt[V-1] < V-1 else "inf")

Submission status: Wrong Answer

Solution code:
import math

INF = float("inf")

V, E = map(int, input().split())
es = []

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

d = [INF] * V
d[0] = 0
cnt = [0] * V

for _ in range(V - 1):
    updated = False
    for e in es:
        if math.isinf(d[e[0]]):
            continue
        if d[e[1]] > d[e[0]] + e[2]:
            d[e[1]] = d[e[0]] + e[2]
            cnt[e[1]] += 1
            updated = True
    if not updated:
        break

print(-d[V-1] if cnt[V-1] < V-1 else "inf")

Submission status: Wrong Answer

Solution code:
import math

INF = float("inf")

V, E = map(int, input().split())
es = []

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

d = [INF] * V
d[0] = 0
cnt = [0] * V

for _ in range(V - 1):
    updated = False
    for e in es:
        if math.isinf(d[e[0]]):
            continue
        if d[e[1]] > d[e[0]] + e[2]:
            d[e[1]] = d[e[0]] + e[2]
            cnt[e[1]] += 1
            updated = True
    if not updated:
        break

print(-d[V-1] if not cnt[V-1] >= V else "inf")

Submission status: Wrong Answer

Solution code:
import math
from heapq import heappush, heappop
[n,m]=[int(i) for i in  input().split()]
b=[[] for i in range(n)]
c=[[] for i in range(n)] 
for j in range(m):
 a=[int(i) for i in  input().split()]
 b[a[0]-1].append([a[1]-1,a[2]])
 c[a[1]-1].append(a[0]-1)
 
dp=["f" for i in range(n)]
dd=[0]*n
ddd=[0]*n
 
s=0    
dp[0]=0
q=[0]
 
qq=[]
ddd[n-1]=1
qq.append(n-1)
while len(qq):
        v=qq.pop(0)
        for j in c[v]:
            if ddd[j]==0:
                ddd[j]=1
                qq.append(j)
 
while len(q):
 
  i=heappop(q)
 
  for j in b[i]:
  
   if dd[j[0]]==1:
      if ddd[j[0]]==1 and ddd[i]==1:
       s=1
       
   else:
    if dp[j[0]]=="f" or dp[j[0]]<dp[i]+j[1]:
     dp[j[0]]=dp[i]+j[1]
     heappush(q,j[0])
     dd[i]=1
    
if s==1:
   print("inf")
else:
   print(dp[n-1])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
MAX_N = 10 ** 3 + 10
MAX_M = 2 * (10 ** 3) + 1
INF = 10 ** 13

def bfs(graph, N, M):
    que = []
    que.append([0, 0])
    visited = [-INF for x in range(MAX_N)]
    update_cnt = [0 for x in range(MAX_N)]
    result = -INF
    while (len(que) > 0):
        now = que[0]
        que.pop(0)

        pos = now[0]
        cost = now[1]

        if (update_cnt[pos] >= MAX_N):
            continue

        if (pos == N - 1):
            result = max(result, cost)

        for val in graph[pos]:
            nxt = val[0]
            nxt_cost = val[1]
            if (cost + nxt_cost > visited[nxt]):
                visited[nxt] = cost + nxt_cost
                update_cnt[nxt] += 1
                que.append([nxt, nxt_cost + cost])

    if (update_cnt[N - 1] >= MAX_N):
        return (0, False)
    return (result, True)


if __name__ == "__main__":
    N, M = map(int, input().split())
    graph = defaultdict(list)
    for x in range(M):
        a,b,c = map(int, input().split())
        a -= 1
        b -= 1
        graph[a].append([b, c])
    res,ok = bfs(graph, N, M)
    if (not ok):
        print ('inf')
    else:
        print (res)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
MAX_N = 10 ** 3 + 10
MAX_M = 2 * (10 ** 3) + 1
INF = 10 ** 13

def bfs(graph, N, M):
    que = []
    que.append([0, 0])
    visited = [-INF for x in range(MAX_N)]
    update_cnt = [0 for x in range(MAX_N)]
    result = -INF
    while (len(que) > 0):
        now = que[0]
        que.pop(0)

        pos = now[0]
        cost = now[1]
        if (visited[pos] > cost):
            continue
        if (update_cnt[pos] >= MAX_N):
            continue
        if (pos == N - 1):
            result = max(result, cost)
            if (update_cnt[N - 1] >= MAX_N):
                return (0, False)

        for val in graph[pos]:
            nxt = val[0]
            nxt_cost = val[1]
            if (cost + nxt_cost > visited[nxt]):
                visited[nxt] = cost + nxt_cost
                update_cnt[nxt] += 1
                que.append([nxt, nxt_cost + cost])

    if (update_cnt[N - 1] >= MAX_N):
        return (0, False)
    return (result, True)


if __name__ == "__main__":
    N, M = map(int, input().split())
    graph = defaultdict(list)
    for x in range(M):
        a,b,c = map(int, input().split())
        a -= 1
        b -= 1
        graph[a].append([b, c])
    res,ok = bfs(graph, N, M)
    if (not ok):
        print ('inf')
    else:
        print (res)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
MAX_N = 10 ** 3 + 10
MAX_M = 2 * (10 ** 3) + 1
INF = 10 ** 13

def bfs(graph, N, M):
    que = []
    que.append([0, 0])
    visited = [-INF for x in range(MAX_N)]
    update_cnt = [0 for x in range(MAX_N)]
    result = -INF
    while (len(que) > 0):
        now = que[0]
        que.pop(0)

        pos = now[0]
        cost = now[1]
        if (visited[pos] >= cost):
            continue
        if (update_cnt[pos] >= MAX_N):
            continue
        if (pos == N - 1):
            result = max(result, cost)
            if (update_cnt[N - 1] >= MAX_N):
                return (0, False)

        for val in graph[pos]:
            nxt = val[0]
            nxt_cost = val[1]
            if (cost + nxt_cost > visited[nxt]):
                visited[nxt] = cost + nxt_cost
                update_cnt[nxt] += 1
                que.append([nxt, nxt_cost + cost])

    if (update_cnt[N - 1] >= MAX_N):
        return (0, False)
    return (result, True)


if __name__ == "__main__":
    N, M = map(int, input().split())
    graph = defaultdict(list)
    for x in range(M):
        a,b,c = map(int, input().split())
        a -= 1
        b -= 1
        graph[a].append([b, c])
    res,ok = bfs(graph, N, M)
    if (not ok):
        print ('inf')
    else:
        print (res)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
MAX_N = 10 ** 3 + 10
MAX_M = 2 * (10 ** 3) + 1
INF = 10 ** 13

def bfs(graph, N, M):
    que = []

    visited = [-INF for x in range(MAX_N)]
    update_cnt = [0 for x in range(MAX_N)]

    que.append(0)
    visited[0] = 0
    while (len(que) > 0):
        now = que[0]
        que.pop(0)

        pos = now
        cost = visited[pos]

        if (visited[pos] > cost):
            continue
        if (update_cnt[pos] >= MAX_N):
            continue
        for val in graph[pos]:
            nxt = val[0]
            nxt_cost = val[1]
            if (cost + nxt_cost > visited[nxt]):
                visited[nxt] = cost + nxt_cost
                update_cnt[nxt] += 1
                que.append(nxt)

    if (update_cnt[N - 1] >= MAX_N):
        return (0, False)
    return (visited[N - 1], True)


if __name__ == "__main__":
    N, M = map(int, input().split())
    graph = defaultdict(list)
    for x in range(M):
        a,b,c = map(int, input().split())
        a -= 1
        b -= 1
        graph[a].append([b, c])
    res,ok = bfs(graph, N, M)
    if (not ok):
        print ('inf')
    else:
        print (res)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
MAX_N = 10 ** 3 + 1
MAX_M = 2 * (10 ** 3) + 1
INF = -(10 ** 100)

def bfs(graph, N, M):
    que = []
    que.append([0, 0])
    visited = defaultdict(list)
    result = INF
    while (len(que) > 0):
        now = que[0]
        que.pop(0)

        pos = now[0]
        cost = now[1]

        if (pos in visited):
            visited_cost = visited[pos][0]
            visited_cnt = visited[pos][1]
            if (visited_cost >= cost):
                continue
            if (visited_cnt >= MAX_N - 100):
                continue
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 0]

        if (pos == N - 1):
            result = max(result, cost)

        for val in graph[pos]:
            nxt = val[0]
            nxt_cost = val[1]
            if (nxt not in visited):
                que.append([nxt, nxt_cost + cost])
            elif(nxt_cost + cost > visited[nxt][0]):
                que.append([nxt, nxt_cost + cost])
    if (visited[N - 1][1] >= MAX_N):
        return (0, False)
    return (result, True)


if __name__ == "__main__":
    N, M = map(int, input().split())
    graph = defaultdict(list)
    for x in range(M):
        a,b,c = map(int, input().split())
        a -= 1
        b -= 1
        graph[a].append([b, c])
    res,ok = bfs(graph, N, M)
    if (not ok):
        print ('inf')
    else:
        print (res)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
MAX_N = 10 ** 3 + 10
MAX_M = 2 * (10 ** 3) + 1
INF = -(10 ** 100)

def bfs(graph, N, M):
    que = []
    que.append([0, 0])
    visited = defaultdict(list)
    result = INF
    while (len(que) > 0):
        now = que[0]
        que.pop(0)

        pos = now[0]
        cost = now[1]

        if (pos == N - 1):
            result = max(result, cost)

        if (pos in visited):
            visited_cost = visited[pos][0]
            visited_cnt = visited[pos][1]
            if (visited_cost >= cost):
                continue
            if (visited_cnt >= MAX_N // 2):
                continue
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 0]

        for val in graph[pos]:
            nxt = val[0]
            nxt_cost = val[1]
            if (nxt not in visited):
                que.append([nxt, nxt_cost + cost])
            elif(nxt_cost + cost > visited[nxt][0]):
                que.append([nxt, nxt_cost + cost])
    if (visited[N - 1][1] >= MAX_N):
        return (0, False)
    return (result, True)


if __name__ == "__main__":
    N, M = map(int, input().split())
    graph = defaultdict(list)
    for x in range(M):
        a,b,c = map(int, input().split())
        a -= 1
        b -= 1
        graph[a].append([b, c])
    res,ok = bfs(graph, N, M)
    if (not ok):
        print ('inf')
    else:
        print (res)