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:
import sys
n, m = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(m)]

rs = {}
for i in abc:
    if i[0] in rs:
        rs[i[0]][i[1]] = i[2]
    else:
        rs[i[0]] = {i[1]: i[2]}

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

inf_point = [False] * (n + 1)
nextr = [[1, i, {1}] for i in rs[1]]
count = 0
while len(nextr) != 0:
    r = nextr.pop(0)
    if r[1] in r[2]:
        maxn[r[1]] = float('inf')
    elif r[1] in rs:
        nextr += [[r[1], i, r[2].copy() | {r[1]}] for i in rs[r[1]]]
    if count > 4000:
        break

nextr = [[1, i, {1}] for i in rs[1]]
count = 0
while len(nextr) != 0:
    count += 1
    r = nextr.pop(0)
    maxn[r[1]] = max(maxn[r[1]], maxn[r[0]] + rs[r[0]][r[1]])
    if r[1] not in r[2] and r[1] in rs:
        nextr += [[r[1], i, r[2].copy() | {r[1]}] for i in rs[r[1]]]
    if count > 4000:
        break
print(maxn[n])

Submission status: Wrong Answer

Solution code:
import sys
n, m = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(m)]

rs = {}
for i in abc:
    if i[0] in rs:
        rs[i[0]][i[1]] = i[2]
    else:
        rs[i[0]] = {i[1]: i[2]}

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

inf_point = [False] * (n + 1)
nextr = [[1, i, {1}] for i in rs[1]]
while len(nextr) != 0:
    r = nextr.pop(0)
    if r[1] in r[2]:
        maxn[r[1]] = float('inf')
    elif r[1] in rs:
        nextr += [[r[1], i, r[2].copy() | {r[1]}] for i in rs[r[1]]]

nextr = [[1, i, {1}] for i in rs[1]]
while len(nextr) != 0:
    r = nextr.pop(0)
    maxn[r[1]] = max(maxn[r[1]], maxn[r[0]] + rs[r[0]][r[1]])
    if r[1] not in r[2] and r[1] in rs:
        nextr += [[r[1], i, r[2].copy() | {r[1]}] for i in rs[r[1]]]
print(maxn[n])

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
A=[[] for i in range(N+1)]
Lim=0
for i in range(M):
    a,b,c=map(int,input().split())
    A[a].append([b,c])
    if c>0:
        Lim+=c
#print(A)

S=[-10**15 for i in range(N+1)]
S[1]=0
Q=[[1,0]]
for i in range(10**6):
    if Q==[]:
        print(S[N])
        exit()
        """elif Q[0][1]>Lim:
        print("inf")
        exit()"""
    else:
        #print("l",Q,i)
        for j in range(len(A[Q[0][0]])):
            #print(S[A[Q[0][0]][j][0]],Q[0][1]+A[Q[0][0]][j][1])
            if S[A[Q[0][0]][j][0]]<Q[0][1]+A[Q[0][0]][j][1]:
                Q.append([A[Q[0][0]][j][0],Q[0][1]+A[Q[0][0]][j][1]])
                S[A[Q[0][0]][j][0]]=Q[0][1]+A[Q[0][0]][j][1]
        del Q[0]
    #print("Q",Q)
print("inf")

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
A=[[] for i in range(N+1)]
Lim=0
for i in range(M):
    a,b,c=map(int,input().split())
    A[a].append([b,c])
    if c>0:
        Lim+=c
#print(A)

S=[-10**15 for i in range(N+1)]
S[1]=0
Q=[[1,0]]
for i in range(5*10**5):
    if Q==[]:
        print(S[N])
        exit()
        """elif Q[0][1]>Lim:
        print("inf")
        exit()"""
    else:
        #print("l",Q,i)
        for j in range(len(A[Q[0][0]])):
            #print(S[A[Q[0][0]][j][0]],Q[0][1]+A[Q[0][0]][j][1])
            if S[A[Q[0][0]][j][0]]<Q[0][1]+A[Q[0][0]][j][1]:
                Q.append([A[Q[0][0]][j][0],Q[0][1]+A[Q[0][0]][j][1]])
                S[A[Q[0][0]][j][0]]=Q[0][1]+A[Q[0][0]][j][1]
        del Q[0]
    #print("Q",Q)
print("inf")

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
A=[[] for i in range(N+1)]
Lim=0
for i in range(M):
    a,b,c=map(int,input().split())
    A[a].append([b,c])
    if c>0:
        Lim+=c
#print(A)

S=[-10**15 for i in range(N+1)]
S[1]=0
Q=[[1,0]]
for i in range(10**5):
    if Q==[]:
        print(S[N])
        exit()
    elif Q[0][1]>Lim:
        print("inf")
        exit()
    else:
        #print("l",Q,i)
        for j in range(len(A[Q[0][0]])):
            #print(S[A[Q[0][0]][j][0]],Q[0][1]+A[Q[0][0]][j][1])
            if S[A[Q[0][0]][j][0]]<Q[0][1]+A[Q[0][0]][j][1]:
                Q.append([A[Q[0][0]][j][0],Q[0][1]+A[Q[0][0]][j][1]])
                S[A[Q[0][0]][j][0]]=Q[0][1]+A[Q[0][0]][j][1]
        del Q[0]
    #print("Q",Q)
print("inf")

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
A=[[] for i in range(N+1)]
Lim=0
for i in range(M):
    a,b,c=map(int,input().split())
    A[a].append([b,c])
    if c>0:
        Lim+=c
#print(A)

S=[-10**15 for i in range(N+1)]
S[1]=0
Q=[[1,0]]
for i in range(10**6):
    if Q==[]:
        print(S[N])
        exit()
    elif Q[0][1]>Lim:
        print("inf")
        exit()
    else:
        #print("l",Q,i)
        for j in range(len(A[Q[0][0]])):
            #print(S[A[Q[0][0]][j][0]],Q[0][1]+A[Q[0][0]][j][1])
            if S[A[Q[0][0]][j][0]]<Q[0][1]+A[Q[0][0]][j][1]:
                Q.append([A[Q[0][0]][j][0],Q[0][1]+A[Q[0][0]][j][1]])
                S[A[Q[0][0]][j][0]]=Q[0][1]+A[Q[0][0]][j][1]
        del Q[0]
    #print("Q",Q)
print("inf")

Submission status: Wrong Answer

Solution code:
import sys
n, m = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(m)]

rs = {}
for i in abc:
    if i[0] in rs:
        rs[i[0]].append([i[1], i[2]])
    else:
        rs[i[0]] = [[i[1], i[2]]]

nextr = [[1, 0, {1}]]
maxn = float('inf') * -1
while len(nextr) != 0:
    ori = nextr.pop(0)
    if ori[0] == n:
        maxn = max(maxn, ori[1])

    if ori[0] in rs:
        for j in rs[ori[0]]:
            if j[0] in ori[2]:
                print('inf')
                sys.exit(0)
            else:
                d = ori[2].copy()
                d.add(j[0])
                nextr.append([j[0], ori[1] + j[1], d])
print(maxn)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
info = [list(map(int,input().split())) for i in range(m)]

tree = [[] for i in range(n)]

for i in range(m):
  tree[info[i][0]-1].append([info[i][0]-1,info[i][1]-1,info[i][2],0])
visited =[-float("inf")]*n
visited[0]=0
count = [0]


def bfs(here):
  q =deque()
  for i in tree[here]:
    q.append(i)
  while q:
    tmp = q.popleft()
    here =tmp[0]
    go = tmp[1]
    cost = tmp[2]
    if visited[here] + cost> visited[go]:
      visited[go] = cost +visited[here]
    for i in range(len(tree[go])):
      if tree[go][i][3]==0: 
        q.appendleft(tree[go][i])
        tree[go][i][3]=1
      elif tree[go][i][3]==1: 
        q.appendleft(tree[go][i])
        tree[go][i][3]=2

        if go == n-1:
          print("inf")
          exit()
    
bfs(0)
print(visited[n-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
info = [list(map(int,input().split())) for i in range(m)]

tree = [[] for i in range(n)]

for i in range(m):
  tree[info[i][0]-1].append([info[i][0]-1,info[i][1]-1,info[i][2],False])
flag= [False]
visited =[-float("inf")]*n
visited[0]=0
count = [0]


def bfs(here):
  q =deque()
  for i in tree[here]:
    q.append(i)
  while q and count[0] <=m*n:
    tmp = q.popleft()
    here =tmp[0]
    go = tmp[1]
    cost = tmp[2]
    if visited[here] + cost> visited[go]:
      visited[go] = cost +visited[here]
    for i in range(len(tree[go])):
      if not tree[go][i][3]: 
        q.appendleft(tree[go][i])
        tree[go][i][3]=True
        
      else:
        q.append(tree[go][i])
        if go == n-1:
          print("inf")
          exit()

    count[0] +=1

    
bfs(0)
print(visited[n-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
info = [list(map(int,input().split())) for i in range(m)]

tree = [[] for i in range(n)]

for i in range(m):
  tree[info[i][0]-1].append([info[i][0]-1,info[i][1]-1,info[i][2],False])
flag= [False]
visited =[-float("inf")]*n
visited[0]=0
count = [0]


def bfs(here):
  q =deque()
  for i in tree[here]:
    q.append(i)
  while q and count[0] <=2*m:
    tmp = q.popleft()
    here =tmp[0]
    go = tmp[1]
    cost = tmp[2]
    if visited[here] + cost> visited[go]:
      visited[go] = cost +visited[here]
    for i in range(len(tree[go])):
      if not tree[go][i][3]: 
        q.appendleft(tree[go][i])
        tree[go][i][3]=True
      else:
        q.append(tree[go][i])
        
    if here ==n-1 and  m<=count[0]:
       flag[0] =True
    count[0] +=1

    
bfs(0)
print(visited[n-1] if flag[0] ==False else "inf")

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
info = [list(map(int,input().split())) for i in range(m)]

tree = [[] for i in range(n)]

for i in range(m):
  tree[info[i][0]-1].append([info[i][0]-1,info[i][1]-1,info[i][2],False])
flag= [False]
visited =[-float("inf")]*n
visited[0]=0
count = [0]


def bfs(here):
  q =deque()
  for i in tree[here]:
    q.append(i)
  while q and count[0] <=2*n:
    tmp = q.popleft()
    here =tmp[0]
    go = tmp[1]
    cost = tmp[2]
    if visited[here] + cost> visited[go]:
      visited[go] = cost +visited[here]
    for i in range(len(tree[go])):
      if not tree[go][i][3]: 
        q.appendleft(tree[go][i])
        tree[go][i][3]=True
      else:
        q.append(tree[go][i])
        
    if here ==n-1 and  n<=count[0]<=2*n:
       flag[0] =True
    count[0] +=1

    
bfs(0)
print(visited[n-1] if flag[0] ==False else "inf")

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
info = [list(map(int,input().split())) for i in range(m)]

tree = [[] for i in range(n)]

for i in range(m):
  tree[info[i][0]-1].append([info[i][0]-1,info[i][1]-1,info[i][2],False])
flag= [False]
visited =[0]*n
count = [0]


def bfs(here):
  q =deque()
  for i in tree[here]:
    q.append(i)
  while q and count[0] <=2*n:
    tmp = q.popleft()
    here =tmp[0]
    go = tmp[1]
    cost = tmp[2]
    if visited[here] + cost> visited[go]:
      visited[go] = cost +visited[here]
    for i in range(len(tree[go])):
      if not tree[go][i][3]: 
        q.appendleft(tree[go][i])
        tree[go][i][3]=True
      else:
        q.append(tree[go][i])
        
    if here ==n-1 and  n<=count[0]<=2*n:
       flag[0] =True
    count[0] +=1

    
bfs(0)
print(visited[n-1] if flag[0] ==False else "inf")

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
es=[list(map(int,input().split())) for _ in range(m)]
inf=float("inf")
d=[inf]*(n+1)
d[1]=0

def bellman_ford():
  for i in range(n):
    isUpdated=False
    for e_from,e_to,cost in es:
      if d[e_to]>d[e_from]-cost:
        d[e_to]=d[e_from]-cost
        isUpdated=True
        if i==n-1:return inf
    if not isUpdated:
      break
  return -d[n]

print(bellman_ford())

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
es=[list(map(int,input().split())) for _ in range(m)]
inf=float("inf")
d=[-inf]*(n+1)
d[1]=0

def bellman_ford():
  for i in range(n):
    isUpdated=False
    for e_from,e_to,cost in es:
      if d[e_to]<d[e_from]+cost:
        d[e_to]=d[e_from]+cost
        isUpdated=True
        if i==n-1:return inf
    if not isUpdated:
      break
  return d[n]

print(bellman_ford())

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
es=[list(map(int,input().split())) for _ in range(m)]
inf=float("inf")
d=[-inf]*(n+1)
d[1]=0

def bellman_ford():
  for i in range(n):
    isUpdated=False
    for e_from,e_to,cost in es:
      if d[e_from]!=-inf and d[e_to]<d[e_from]+cost:
        d[e_to]=d[e_from]+cost
        isUpdated=True
        if i==n-1:return inf
    if not isUpdated:
      break
  return d[n]

print(bellman_ford())

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
es=[list(map(int,input().split())) for _ in range(m)]
inf=float("inf")
d=[-inf]*(n+1)
d[1]=0

def bellman_ford():
  for i in range(n):
    isUpdated=False
    for e_from,e_to,cost in es:
      if d[e_from]!=inf and d[e_to]<d[e_from]+cost:
        d[e_to]=d[e_from]+cost
        isUpdated=True
        if i==n-1:return inf
      else:
        break
  return d[n]

print(bellman_ford())

Submission status: Wrong Answer

Solution code:
def bellman_ford(N, M, edge):
    d = [10 ** 100] * N
    d[0] = 0
    infFlag = False
    for count in range(N - 1):
        for i in range(M):
            if d[edge[i][0]] + edge[i][2] < d[edge[i][1]]:
                d[edge[i][1]] = d[edge[i][0]] + edge[i][2]
                if count == N - 2 and edge[i][1] == N - 1:
                    infFlag = True
    return infFlag, -d[N - 1]


def main():
    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])
    infFlag, ans = bellman_ford(N, M, edge)
    if infFlag:
        print('inf')
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def bellman_ford(N, M, edge):
    d = [float('inf')] * N
    d[0] = 0
    infFlag = False
    for count in range(N):
        for i in range(M):
            if d[edge[i][0]] != float('inf') and d[edge[i][0]] + edge[i][2] < d[edge[i][1]]:
                d[edge[i][1]] = d[edge[i][0]] + edge[i][2]
                if count == N - 1:
                    infFlag = True
    return infFlag, -d[N - 1]


def main():
    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])
    infFlag, ans = bellman_ford(N, M, edge)
    if infFlag:
        print('inf')
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def bellman_ford(N, M, edge):
    d = [10 ** 100 for _ in range(N)]
    d[0] = 0
    infFlag = False
    for count in range(N + 1):
        for i in range(M):
            if d[edge[i][0]] + edge[i][2] < d[edge[i][1]]:
                d[edge[i][1]] = d[edge[i][0]] + edge[i][2]
                if count == N:
                    infFlag = True
    return infFlag, -d[N - 1]


def main():
    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])
    infFlag, ans = bellman_ford(N, M, edge)
    if infFlag:
        print('inf')
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def bellman_ford(N, M, edge):
    d = [10 ** 100 for _ in range(N)]
    d[0] = 0
    infFlag = False
    for count in range(N):
        for i in range(M):
            if d[edge[i][0]] + edge[i][2] < d[edge[i][1]]:
                d[edge[i][1]] = d[edge[i][0]] + edge[i][2]
                if count == N - 1:
                    infFlag = True
    return infFlag, -d[N - 1]


def main():
    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])
    infFlag, ans = bellman_ford(N, M, edge)
    if infFlag:
        print('inf')
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def bellman_ford(N, M, edge):
    d = [10 ** 10 for _ in range(N)]
    d[0] = 0
    infFlag = False
    for count in range(N):
        for i in range(M):
            fr, to, cost = edge[i]
            if d[fr] + cost < d[to]:
                d[to] = d[fr] + cost
                if count == N - 1:
                    infFlag = True
    return infFlag, -d[N - 1]


def main():
    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])
    infFlag, ans = bellman_ford(N, M, edge)
    if infFlag:
        print('inf')
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
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))
inf=99999999999999999999
d=[inf for i in range(n)]
d[0]=0
count=0
while count<n*2:
    count+=1
    update=False
    for i in range(m):
        f,t,cost=edge[i]
        if(d[t]>d[f]+cost):
            d[t]=d[f]+cost
            update=True
    if not update:
        print(-d[n-1])
        break
else:
    print('inf')

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import time
aa=time.time()
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))
inf=9999999999999999
d=[inf for i in range(n)]
d[0]=0
count=0
while count<n+1:
    count+=1
    update=False
    for i in range(m):
        f,t,cost=edge[i]
        if(d[f]!=inf and d[t]>d[f]+cost):
            d[t]=d[f]+cost
            update=True
    if not update:
        print(-d[n-1])
        break
else:
    print('inf')

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import time
aa=time.time()
n,m=map(int,input().split())
g=[[]for i in range(n)]
for i in range(m):
    a,b,c=map(int,input().split())
    g[a-1].append((b-1,c))
from collections import deque
d=deque()
d.append((0,0))
dist=[-10**20 for i in range(n)]
count=0
while d:
    count+=1
    if time.time()-aa>1.9:
        print('inf')
        exit()
    v,c=d.popleft()
    if dist[v]>c:
        continue
    dist[v]=c
    for i in range(len(g[v])):
        if dist[g[v][i][0]]<g[v][i][1]+c:
            d.append((g[v][i][0],g[v][i][1]+c))
print(dist[n-1])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import time
aa=time.time()
n,m=map(int,input().split())
g=[[]for i in range(n)]
for i in range(m):
    a,b,c=map(int,input().split())
    g[a-1].append((b-1,c))
from collections import deque
d=deque()
d.append((0,0))
dist=[-10**20 for i in range(n)]
count=0
while d:
    count+=1
    if time.time()-aa>1.9:
        print('inf')
        exit()
    v,c=d.popleft()
    if dist[v]>c:
        continue
    dist[v]=c
    for i in range(len(g[v])):
        d.append((g[v][i][0],g[v][i][1]+c))
print(dist[n-1])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import time
aa=time.time()
n,m=map(int,input().split())
g=[[]for i in range(n)]
for i in range(m):
    a,b,c=map(int,input().split())
    g[a-1].append((b-1,c))
from collections import deque
d=deque()
d.append((0,0))
dist=[-10**20 for i in range(n)]
count=0
while d:
    count+=1
    if time.time()-aa>1.9:
        print('inf')
        exit()
    v,c=d.popleft()
    if dist[v]>=c:
        continue
    dist[v]=c
    for i in range(len(g[v])):
        d.append((g[v][i][0],g[v][i][1]+c))
print(dist[n-1])

Submission status: Wrong Answer

Solution code:

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

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

    return dist

num_v, num_e = map(int, input().split())
edges = []
for _ in range(num_e):
    a, b, c = map(int, input().split())
    edges.append((a, b, -c))

dist = BellmanFord(edges, num_v, 1)
if dist == -1:
    print("inf")
else:
    print(-dist[-1])

Submission status: Wrong Answer

Solution code:
from scipy.sparse.csgraph import bellman_ford
from scipy.sparse.csgraph import NegativeCycleError
from scipy.sparse import csr_matrix

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

path1 = csr_matrix((score, (start, goal)), shape=(n, n), dtype=int)
try:
    score = bellman_ford(path1, indices=[0])
    print(-int(score[0][-1]))
except NegativeCycleError:
    print("inf")

Submission status: Wrong Answer

Solution code:
from scipy.sparse.csgraph import bellman_ford
from scipy.sparse.csgraph import NegativeCycleError
from scipy.sparse import csr_matrix

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

path1 = csr_matrix((score, (start, goal)), shape=(n, n), dtype=int)
try:
    score = bellman_ford(path1)
    print(-int(score[0][-1]))
except NegativeCycleError:
    print("inf")

Submission status: Wrong Answer

Solution code:
from scipy.sparse.csgraph import bellman_ford
from scipy.sparse import csr_matrix

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

path1 = csr_matrix((score, (start, goal)), shape=(n, n), dtype=int)
try:
    score = bellman_ford(path1, indices=0)
    print(-int(score[-1]))
except:
    print("inf")

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, i):
        self.i = i
        self.nexts = []

    def add(self, n, v):
        self.nexts.append((n, v))


def solve(N, M, ABCs):
    # print(N, ",", M, ",", ABCs)
    nodes = [Node(i) for i in range(0, N + 1)]
    max_v = sum([abc[2] for abc in ABCs if abc[2] > 0]) + 1

    for a, b, c in ABCs:
        nodes[a].add(b, c)

    scores = [-(10 ** 9 * 2000) - 1 for _ in range(0, N + 1)]
    visited_count = [0 for _ in range(0, N + 1)]

    q = [1]
    scores[1] = 0
    while len(q):
        index = q.pop()
        if scores[index] > max_v:
            return "inf"
        if visited_count[index] > (M + N):
            break
        visited_count[index] += 1
        for n, v in nodes[index].nexts:
            n_score = scores[index] + v
            if n_score > scores[n]:
                scores[n] = n_score
                q.insert(0, n)
    return scores[N]

if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    ABCs = [tuple(map(int, input().split(" "))) for _ in range(M)]
    print(solve(N, M, ABCs))

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, i):
        self.i = i
        self.nexts = []

    def add(self, n, v):
        self.nexts.append((n, v))


def solve(N, M, ABCs):
    # print(N, ",", M, ",", ABCs)
    nodes = [Node(i) for i in range(0, N + 1)]
    max_v = sum([abc[2] for abc in ABCs if abc[2] > 0]) + 1

    for a, b, c in ABCs:
        nodes[a].add(b, c)

    scores = [-(10 ** 9 * 2000) - 1 for _ in range(0, N + 1)]
    visited_count = [0 for _ in range(0, N + 1)]

    q = [1]
    scores[1] = 0
    while len(q):
        index = q.pop()
        if scores[index] > max_v:
            return "inf"
        if visited_count[index] > (M + N):
            break
        visited_count[index] += 1
        for n, v in nodes[index].nexts:
            n_score = scores[index] + v
            if n_score > scores[n]:
                scores[n] = n_score
                q.append(n)
    return scores[N]


if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    ABCs = [tuple(map(int, input().split(" "))) for _ in range(M)]
    print(solve(N, M, ABCs))

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, i):
        self.i = i
        self.nexts = []

    def add(self, n, v):
        self.nexts.append((n, v))


def solve(N, M, ABCs):
    # print(N, ",", M, ",", ABCs)
    nodes = [Node(i) for i in range(0, N + 1)]
    max_v = sum([abc[2] for abc in ABCs if abc[2] > 0]) + 1
    # min_v = sum([abc[2] for abc in ABCs if abc[2] < 0]) - 1

    for a, b, c in ABCs:
        nodes[a].add(b, c)

    q = [1]
    scores = [-(10 ** 9 * 2000) - 1 for _ in range(0, N + 1)]
    visited_count = [0 for _ in range(0, N + 1)]
    scores[1] = 0
    while len(q):
        index = q.pop()
        if scores[index] > max_v:
            return "inf"
        if visited_count[index] > M:
            break
        visited_count[index] += 1
        for n, v in nodes[index].nexts:
            n_score = scores[index] + v
            if n_score > scores[n]:
                scores[n] = n_score
                q.append(n)
    return scores[N]

if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    ABCs = [tuple(map(int, input().split(" "))) for _ in range(M)]
    print(solve(N, M, ABCs))

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, i):
        self.i = i
        self.nexts = []

    def add(self, n, v):
        self.nexts.append((n, v))


def solve(N, M, ABCs):
    # print(N, ",", M, ",", ABCs)
    nodes = [Node(i) for i in range(0, N + 1)]
    max_v = sum([abc[2] for abc in ABCs if abc[2] > 0]) + 1
    # min_v = sum([abc[2] for abc in ABCs if abc[2] < 0]) - 1

    for a, b, c in ABCs:
        nodes[a].add(b, c)

    q = [1]
    scores = [-(10 ** 13) for _ in range(0, N + 1)]
    visited_count = [0 for _ in range(0, N + 1)]
    scores[1] = 0
    while len(q):
        index = q.pop()
        if scores[index] > max_v:
            return "inf"
        if visited_count[index] > N * M:
            break
        visited_count[index] += 1
        for n, v in nodes[index].nexts:
            n_score = scores[index] + v
            if n_score > scores[n]:
                scores[n] = n_score
                q.append(n)
    return scores[N]


if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    ABCs = [tuple(map(int, input().split(" "))) for _ in range(M)]
    print(solve(N, M, ABCs))

Submission status: Wrong Answer

Solution code:
# 入力「edges:隣接行列」「num_v:頂点数」「start:始点」
def shortest_path(edges, num_v, start):
    inf = float("inf")
    dist = [inf for _ in range(num_v)]
    dist[start] = 0
    # 辺の緩和
    for i in range(num_v+1):
        for edge in edges:
            if dist[edge[0]] != inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                if i == num_v:
                    print("inf")
                    exit()
    return dist

n, m = [int(i) for i in input().split()]
edges = []
for _ in range(m):
    a, b, c = [int(i) for i in input().split()]
    edges.append([a-1, b-1, -c])
out = shortest_path(edges,n,0)
print(-out[-1])

Submission status: Wrong Answer

Solution code:
# 入力「edges:隣接行列」「num_v:頂点数」「start:始点」
def shortest_path(edges, num_v, start):
    inf = float("inf")
    dist = [inf for _ in range(num_v)]
    dist[start] = 0
    # 辺の緩和
    for i in range(num_v):
        for edge in edges:
            if dist[edge[0]] != inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
        if i == num_v - 1:
            return False
    return dist

n, m = [int(i) for i in input().split()]
edge = []
for _ in range(m):
    a, b, c = [int(i) for i in input().split()]
    edge.append([a-1, b-1, -c])
out = shortest_path(edge,n,0)
if not out:
    print("inf")
else:
    print(-out[-1])

Submission status: Wrong Answer

Solution code:
# 入力「edges:隣接行列」「num_v:頂点数」「start:始点」
def shortest_path(edges, num_v, start):
    inf = float("inf")
    dist = [inf for _ in range(num_v)]
    dist[start] = 0
    # 辺の緩和
    for i in range(num_v):
        for edge in edges:
            if dist[edge[0]] != inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                if i == num_v - 1:
                    return False
    return dist

n, m = [int(i) for i in input().split()]
edge = []
for _ in range(m):
    a, b, c = [int(i) for i in input().split()]
    edge.append([a-1, b-1, -c])
out = shortest_path(edge,n,0)
if not out:
    print("inf")
else:
    print(-out[-1])

Submission status: Wrong Answer

Solution code:
# 入力「edges:隣接行列」「num_v:頂点数」「start:始点」
def shortest_path(edges, num_v, start):
    inf = float("inf")
    dist = [inf for f in range(num_v)]
    dist[start] = 0
    # 辺の緩和
    for i in range(num_v):
        for edge in edges:
            if dist[edge[0]] != inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                if i == num_v - 1:
                    return False
    return dist

n, m = [int(i) for i in input().split()]
edge = []
for _ in range(m):
    a, b, c = [int(i) for i in input().split()]
    edge.append([a-1, b-1, -c])
out = shortest_path(edge,n,0)
if not out:
    print(-1)
else:
    print(-out[-1])

Submission status: Wrong Answer

Solution code:
# 入力「edges:隣接行列」「num_v:頂点数」「start:始点」
def shortest_path(edges, num_v, start):
    inf = float("inf")
    dist = [inf for f in range(num_v)]
    dist[start] = 0
    # 辺の緩和
    for i in range(num_v):
        for edge in edges:
            if dist[edge[0]] != inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                if i == num_v - 1:
                    return -1
    return dist
_min = -10**12
n, m = [int(i) for i in input().split()]
edge = []
for _ in range(m):
    a, b, c = [int(i) for i in input().split()]
    edge.append([a-1, b-1, -c])
out = shortest_path(edge,n,0)
if out == -1:
    print(-1)
else:
    print(-out[-1])

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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