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:
def d():
    n, m = map(int, input().split())
    node = []
    for i in range(m):
        ai, bi, ci = map(int, input().split())
        node.append([ai, bi, ci])
    node = sorted(node, key=lambda n: n[0])
    route = [None]*n
    route[0] = 0
    updated = True
    # updated_times = 0
    updated_route = []
    while updated:
        updated = False
        temp_updated_route = []
        for i in node:
            ai, bi, ci = i
            if route[ai-1] != None:
                # then, reflesh bi.
                if route[bi-1] == None:
                    route[bi-1] = route[ai-1] + ci
                    # updated = True
                else:
                    if route[bi-1] < route[ai-1] + ci:
                        route[bi-1] = route[ai-1] + ci
                        updated = True
                        temp_updated_route.append(bi-1)
                # updated_times += 1
        if temp_updated_route in updated_route:
            print('inf')
            break
        if updated:
            updated_route.append(temp_updated_route)
    else:
        print(route[-1])
    #print(route, updated_route)
    pass

d()

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:
       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 ** 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 >= 2 * MAX_N):
                return (0, False)
            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])
    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)
    visited[0] = [0, 0]
    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):
                return (0, False)
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 1]

        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])
    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:
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 and ddd[j[0]]==1:
       q=[]
       s=1
       break
   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 ** 100)

def bfs(graph, N, M):
    que = []
    que.append([0, 0])
    visited = defaultdict(list)
    visited[0] = [0, 0]
    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):
                return (0, False)
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 1]

        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])
    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 in visited):
            visited_cost = visited[pos][0]
            visited_cnt = visited[pos][1]
            if (visited_cost >= cost):
                continue
            if (visited_cnt >= MAX_M):
                return (0, False)
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 1]

        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])
    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 in visited):
            visited_cost = visited[pos][0]
            visited_cnt = visited[pos][1]
            if (visited_cost >= cost):
                continue
            if (visited_cnt >= (10 ** 4)):
                return (0, False)
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 1]

        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])
    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 in visited):
            visited_cost = visited[pos][0]
            visited_cnt = visited[pos][1]
            if (visited_cost >= cost):
                continue
            if (visited_cnt >= (10 ** 5)):
                return (0, False)
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 1]

        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])
    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 in visited):
            visited_cost = visited[pos][0]
            visited_cnt = visited[pos][1]
            if (visited_cost >= cost):
                continue
            if (visited_cnt >= (10 ** 6)):
                return (0, False)
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 1]

        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])
    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:
N, M = list(map(int, input().split()))
a = []
b = []
c = []

for o in range(M):
    a_i, b_i, c_i = list(map(int, input().split()))
    a.append(a_i)
    b.append(b_i)
    c.append(c_i)


cost = [-float('inf') for i in range(N + 1)]
cost[1] = 0
flag = True

for _ in range(N - 1):
    for k in range(M):
        cost[b[k]] = max(cost[b[k]], cost[a[k]] + c[k])


def check():
    for l in range(M):
        if (cost[b[l]] < cost[a[l]] + c[l]) and l == N:
            return 'inf'
    return cost[N]


print(check())

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 in visited):
            visited_cost = visited[pos][0]
            visited_cnt = visited[pos][1]
            if (visited_cost >= cost):
                continue
            if (visited_cnt >= MAX_N):
                return (0, False)
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 1]

        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])
    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:
INF = 10**15
N, M = map(int, raw_input().split())
G = [map(int, raw_input().split()) for _ in range(M)]

def run():
    cost = [-INF for i in range(N+1)]
    cost[1] = 0
    for _ in range(N+1):
        for a, b, c in G:
            if cost[a] == INF:
                continue
            if cost[a] + c > cost[b]:
                cost[b] = cost[a] + c
    looped = False
    for a, b, c in G:
        if cost[a] == INF:
            continue
        if cost[a] + c > cost[b]:
            looped = True
    return "inf" if looped else cost[N]
print run()

Submission status: Wrong Answer

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

for o in range(M):
    a_i, b_i, c_i = list(map(int, input().split()))
    a.append(a_i)
    b.append(b_i)
    c.append(c_i)


cost = [-float('inf') for i in range(N + 1)]
cost[1] = 0
flag = True

for _ in range(N - 1):
    for k in range(M):
        cost[b[k]] = max(cost[b[k]], cost[a[k]] + c[k])


def check():
    for l in range(M):
        if cost[b[l]] < cost[a[l]] + c[l]:
            return 'inf'
    return cost[N]


print(check())

Submission status: Wrong Answer

Solution code:
INF = 10**20
N, M = map(int, raw_input().split())
G = [map(int, raw_input().split()) for _ in range(M)]

def run():
    cost = [-INF for i in range(N+1)]
    cost[1] = 0
    for _ in range(N+1):
        for a, b, c in G:
            if cost[a] == INF:
                continue
            if cost[a] + c > cost[b]:
                cost[b] = cost[a] + c
    looped = False
    for a, b, c in G:
        if cost[a] == INF:
            continue
        if cost[a] + c > cost[b]:
            looped = True
    return "inf" if looped else cost[N]
print run()

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 in visited):
            visited_cnt = visited[pos][1]
            visited_cost = visited[pos][0]
            if (visited_cost >= cost):
                continue
            if (visited_cnt >= 5 * MAX_N):
                return INF
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 1]

        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[pos][0]):
                que.append([nxt, nxt_cost + cost])
    return result


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 = bfs(graph, N, M)
    if (res == INF):
        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 in visited):
            visited_cnt = visited[pos][1]
            visited_cost = visited[pos][0]
            if (visited_cost >= cost):
                continue
            if (visited_cnt >= MAX_N):
                return INF
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 1]

        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[pos][0]):
                que.append([nxt, nxt_cost + cost])
    return result


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 = bfs(graph, N, M)
    if (res == INF):
        print ('inf')
    else:
        print (res)

Submission status: Wrong Answer

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

for i in range(M):
    a_i, b_i, c_i = list(map(int, input().split()))
    a.append(a_i)
    b.append(b_i)
    c.append(c_i)


cost = [-float('inf') for i in range(N + 1)]
cost[1] = 0
flag = True

for i in range(N + 1):
    for k in range(M):
        cost[b[k]] = max(cost[b[k]], cost[a[k]] + c[k])


def check():
    for k in range(M):
        if cost[b[k]] < cost[a[k]] + c[k]:
            return 'inf'
    return cost[N]


print(check())

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_cnt = visited[pos][1]
            visited_cost = visited[pos][0]
            if (visited_cost >= cost):
                continue
            if (visited_cnt >= 10):
                return INF
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 1]

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

        for val in graph[pos]:
            nxt = val[0]
            nxt_cost = val[1]
            que.append([nxt, nxt_cost + cost])
    return result


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 = bfs(graph, N, M)
    if (res == INF):
        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_cnt = visited[pos][1]
            visited_cost = visited[pos][0]
            if (visited_cost >= cost):
                continue
            if (visited_cnt >= MAX_N):
                return INF
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 1]

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

        for val in graph[pos]:
            nxt = val[0]
            nxt_cost = val[1]
            que.append([nxt, nxt_cost + cost])
    return result


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 = bfs(graph, N, M)
    if (res == INF):
        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_cnt = visited[pos][0]
            visited_cost = visited[pos][1]
            if (visited_cost >= cost):
                continue
            if (visited_cnt >= MAX_N):
                return INF
            visited[pos] = [cost, visited_cnt + 1]
        else:
            visited[pos] = [cost, 1]

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

        for val in graph[pos]:
            nxt = val[0]
            nxt_cost = val[1]
            que.append([nxt, nxt_cost + cost])
    return result


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 = bfs(graph, N, M)
    if (res == INF):
        print ('inf')
    else:
        print (res)

Submission status: Wrong Answer

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

for i in range(M):
    a_i, b_i, c_i = list(map(int, input().split()))
    a.append(a_i)
    b.append(b_i)
    c.append(c_i)


cost = [-float('inf') for i in range(N)]
cost[0] = 0
flag = True

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


def check():
    for k in range(M):
        if cost[b[k] - 1] < cost[a[k] - 1] + c[k]:
            return 'inf'
    return cost[N - 1]


print(check())

Submission status: Wrong Answer

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

for i in range(M):
    a_i, b_i, c_i = list(map(int, input().split()))
    a.append(a_i)
    b.append(b_i)
    c.append(c_i)


cost = [-float('inf') for i in range(N)]
cost[0] = 0
flag = True

while flag:
    for k in range(M):
        cost[b[k] - 1] = max(cost[b[k] - 1], cost[a[k] - 1] + c[k])
        if k == M - 2:
            flag = False

def check():
    for k in range(M):
        if cost[b[k] - 1] < cost[a[k] - 1] + c[k]:
            return 'inf'
    return cost[N - 1]




print(check())

Submission status: Wrong Answer

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

for i in range(M):
    a_i, b_i, c_i = list(map(int, input().split()))
    a.append(a_i)
    b.append(b_i)
    c.append(c_i)


cost = [-100000000000 for i in range(N)]
cost[0] = 0


def check():
    for k in range(M):
        if cost[b[k] - 1] < cost[a[k] - 1] + c[k]:
            return 'inf'
    return cost[N - 1]

for j in range(M - 1):
    for k in range(M):
        cost[b[k] - 1] = max(cost[b[k] - 1], cost[a[k] - 1] + c[k])

print(check())

Submission status: Wrong Answer

Solution code:
N, M = map(int, raw_input().split())
G = {i: [] for i in range(N+1)}
for i in range(M):
    a, b, c = map(int, raw_input().split())
    G[a].append((b, c))

def run():
    cost = [-10**20 for i in range(N+1)]
    cost[1] = 0
    que = [1]
    cnt = 0
    while len(que) > 0:
        a = que.pop(0)
        cnt += 1
        if cnt > M*N:
            return 'inf'
        for b, c in G[a]:
            if cost[b] < cost[a] + c:
                cost[b] = cost[a] + c
                que.append(b)
    return cost[N]
print run()

Submission status: Wrong Answer

Solution code:
N, M = map(int, raw_input().split())
G = {i: [] for i in range(N+1)}
for i in range(M):
    a, b, c = map(int, raw_input().split())
    G[a].append((b, c))

def run():
    cost = [-10**20 for i in range(N+1)]
    cost[1] = 0
    que = [1]
    cnt = 0
    while len(que) > 0:
        a = que.pop(0)
        cnt += 1
        if cnt > 2*M:
            return 'inf'
        for b, c in G[a]:
            if cost[b] < cost[a] + c:
                cost[b] = cost[a] + c
                que.append(b)
    return cost[N]
print run()

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)]

for j in range(m):
 a=[int(i) for i in  input().split()]
 b[a[0]-1].append([a[1]-1,a[2]])
 

dp=["f" for i in range(n)]
dd=[0]*n
c=[0 for i in range(n)]

s=0    
dp[0]=0
q=[0]
while len(q):
 
  i=heappop(q)
  
  for j in b[i]:
   if dd[j[0]]==1:
       q=[]
       s=1
       break
   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:
N, M = map(int, raw_input().split())
G = {i: [] for i in range(N+1)}
for i in range(M):
    a, b, c = map(int, raw_input().split())
    G[a].append((b, c))

def run():
    cost = [-10**20 for i in range(N+1)]
    cost[1] = 0
    que = [1]
    cnt = 0
    while len(que) > 0:
        a = que.pop(0)
        cnt += 1
        if cnt > 2*M*N:
            return 'inf'
        for b, c in G[a]:
            if cost[b] < cost[a] + c:
                cost[b] = cost[a] + c
                que.append(b)
    return cost[N]
print run()

Submission status: Wrong Answer

Solution code:
N, M = map(int, raw_input().split())
G = [map(int, raw_input().split()) for i in range(M)]
def run():
    cost = [-10**20 for i in range(N+1)]
    cost[1] = 0
    que = [1]
    cnt = 0
    while len(que) > 0:
        s = que.pop(0)
        cnt += 1
        if cnt > 2*M*N:
            return 'inf'
        for a, b, c in G:
            if (s == a) and (cost[b] < cost[a] + c):
                cost[b] = cost[a] + c
                que.append(b)
    return cost[N]
print run()

Submission status: Wrong Answer

Solution code:
N, M = map(int, raw_input().split())
G = [map(int, raw_input().split()) for i in range(M)]
def run():
    cost = [-10**20 for i in range(N+1)]
    cost[1] = 0
    que = [1]
    cnt = 0
    while len(que) > 0:
        s = que.pop(0)
        cnt += 1
        if cnt > N**2:
            return 'inf'
        for a, b, c in G:
            if (s == a) and (cost[b] < cost[a] + c):
                cost[b] = cost[a] + c
                que.append(b)
    return cost[N]
print run()

Submission status: Wrong Answer

Solution code:
N, M = map(int, raw_input().split())
G = [map(int, raw_input().split()) for i in range(M)]
def run():
    cost = [-10**10 for i in range(N+1)]
    cost[1] = 0
    que = [1]
    cnt = 0
    while len(que) > 0:
        s = que.pop(0)
        cnt += 1
        if cnt > N**2:
            return 'inf'
        for a, b, c in G:
            if (s == a) and (cost[b] < cost[a] + c):
                cost[b] = cost[a] + c
                que.append(b)
    return cost[N]
print run()