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

def bellman_ford(s):
  # std = [[s0, t0, d0], [s1, t1, d1], ... , [sm, tm, dm]]
  # (si, ti, di): si, ti間に重さdiの辺がある
  dist = [float('inf')]*n 
  dist[s] = 0
  for u in range(n):
    for s, t, d in abc:
      if dist[t] > dist[s] + d:
        dist[t] = dist[s] + d
        if u == n-1:
          return -1
  return dist

dist = bellman_ford(0)
if dist == -1:
  print('inf')
else:
  print(-1*dist[n-1])

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split())
link = []
for i in range(m):
    tmp = list(map(int,input().split()))
    link.append([tmp[0]-1,tmp[1]-1,-tmp[2]])

def bell(edges, start,num_v):
    cost = [float('inf')] * num_v
    cost[start] = 0
    for _ in range(num_v):
        updated = False
        for a, b, c in edges:
            if cost[b] > cost[a] + c:
                cost[b] = cost[a] + c
                updated = True
        if not updated: break
    else:
        return -1,cost
    return 1,cost

ans1,ans2 = bell(link,0,n)

if ans1 == -1:
    print("inf")
else:
    print(-ans2[-1])

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,M=map(int,input().split())
s=[[]for i in range(N)]
for i in range(M):
    a,b,c=map(int,input().split())
    s[a-1].append((b-1,c))
b=[(0,{0},0)]
l=[-10**13]*N
for i in range(N):
    a=[]
    for j in b:
        for k in s[j[0]]:
            if k[0] in j[1]:
                l[k[0]]="inf"
                continue
            if l[k[0]]=="inf":
                continue
            if j[2]+k[1]>l[k[0]]:
                l[k[0]]=j[2]+k[1]
                if k[0]==N-1:
                    A=(k[0],j[1]|{k[0]},j[2]+k[1])
            a.append((k[0],j[1]|{k[0]},j[2]+k[1]))
    b=a
for i in A[1]:
    if l[i]=="inf":
        print("inf")
        break
else:
    print(A[2])

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,M=map(int,input().split())
s=[[]for i in range(N)]
for i in range(M):
    a,b,c=map(int,input().split())
    s[a-1].append((b-1,c))
b=[(0,{0},0)]
l=[-10**13]*N
for i in range(N):
    a=[]
    for j in b:
        for k in s[j[0]]:
            if k[0] in j[1]:
                l[k[0]]="inf"
                continue
            if l[k[0]]!="inf" and j[2]+k[1]>l[k[0]]:
                l[k[0]]=j[2]+k[1]
                a.append((k[0],j[1]|{k[0]},j[2]+k[1]))
                if k[0]==N-1:
                    A=a[-1]
    b=a
for i in A[1]:
    if l[i]=="inf":
        print("inf")
        break
else:
    print(A[2])

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,M=map(int,input().split())
s=[[]for i in range(N)]
for i in range(M):
    a,b,c=map(int,input().split())
    s[a-1].append((b-1,c))
b=[(0,{0},0)]
l=[-10**13]*N
for i in range(N):
    a=[]
    for j in b:
        for k in s[j[0]]:
            if k[0] in j[1]:
                break
            if j[2]+k[1]>l[k[0]]:
                l[k[0]]=j[2]+k[1]
            a.append((k[0],j[1]|{k[0]},j[2]+k[1]))
        else:
            continue
        break
    else:
        b=a
        continue
    print("inf")
    break
else:
    print(l[-1])

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,M=map(int,input().split())
s=[[]for i in range(N)]
for i in range(M):
    a,b,c=map(int,input().split())
    s[a-1].append((b-1,c))
b=[(0,{0},0)]
l=[-10**13]*N
for i in range(N):
    a=[]
    for j in b:
        for k in s[j[0]]:
            if k[0] in j[1]:
                break
            if j[2]+k[1]>l[k[0]]:
                l[k[0]]=j[2]+k[1]
                a.append((k[0],j[1]|{k[0]},j[2]+k[1]))
        else:
            continue
        break
    else:
        b=a
        continue
    print("inf")
    break
else:
    print(l[-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline

def main():
    N,M = map(int,input().split())
    
    def BellmanFord(edges,V,source):
        INF = float("inf")
        dist = [INF for _ in range(N)]
        dist[source] = 0
        
        for i in range(V):
            for now,fol,d in edges:
                if dist[now] != INF and dist[fol] > dist[now]+d:
                    dist[fol] = dist[now]+d
                    if i == V-1:
                        return -1
        
        return dist
        
    edge = []
    for _ in range(M):
        a,b,c = map(int,input().split())
        edge.append((a-1,b-1,-c))
    
    G = BellmanFord(edge,N,0)
    
    if G == -1:
        print("inf")
    else:
        print(-1*G[-1])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
g = [list(map(int,input().split())) for _ in range(m)]
def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source-1]=0
  
  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if edge[0] != inf and dist[edge[1]-1] > dist[edge[0]-1] - edge[2]:
        dist[edge[1]-1] = dist[edge[0]-1] - edge[2]
        if i==num_v-1 and edge[0] == num_v-1: 
          return -1
  return dist
if BellmanFord(g,n,1) == -1:
  print("inf")
else:
  print(-BellmanFord(g,n,1)[-1])

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
g = [list(map(int,input().split())) for _ in range(m)]
def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source-1]=0
  
  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if edge[0] != inf and dist[edge[1]-1] > dist[edge[0]-1] - edge[2]:
        dist[edge[1]-1] = dist[edge[0]-1] - edge[2]
        print(dist,edge,i)
        if i==num_v-1 and edge[0] == num_v-1: 
          print("a")
          return -1
  return dist
if BellmanFord(g,n,1) == -1:
  print("inf")
else:
  print(-BellmanFord(g,n,1)[-1])

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
g = sorted([list(map(int,input().split())) for _ in range(m)])
def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source-1]=0
  
  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if edge[0] != inf and dist[edge[1]-1] > dist[edge[0]-1] - edge[2]:
        dist[edge[1]-1] = dist[edge[0]-1] - edge[2]
        if i==num_v-1: return -1
  
  return dist
if BellmanFord(g,n,1) == -1:
  print("inf")
else:
  print(-BellmanFord(g,n,1)[-1])

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
g = [list(map(int,input().split())) for _ in range(m)]
def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source-1]=0
  
  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if edge[0] != float("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
if BellmanFord(g,n,1) == -1:
  print("inf")
else:
  print(-BellmanFord(g,n,1)[-1])

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
g = [list(map(int,input().split())) for _ in range(m)]
def shortest_path(s,n,w,es):
  d = [float("inf")] * n
  d[s] = 0
  while True:
    update = False
    for p,q,r in es:
      if d[p-1] != float("inf") and d[q-1] > d[p-1] - r:
        d[q-1] = d[p-1] - r
        update = True
    if not update:
      break
  return d
def find_negative_loop(n,w,es):
    d = [float("inf")] * n
    d[0] = 0 
    for i in range(n):
        for j in range(w):
            e = es[j]
            if d[e[1]-1] > d[e[0]-1] - e[2]:
                d[e[1]-1] = d[e[0]-1] - e[2]
                if i == n-1:
                    return True
    return False
if not find_negative_loop(n,m,g):
  print(-shortest_path(0,n,m,g)[-1])
else:
  print("inf")

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    # adj = [[], [(to, cost), (to, cost), ...], [(to, cost), (to, cost), ...], ...]
    def BellmanFord(adj, start):
        N = len(adj) - 1
        inf = 10**13
        dist = [inf] * (N + 1)
        dist[start] = 0
        neg_loop_flg = 0
        for i in range(N):
            updated = 0
            for fr in range(1, N + 1):
                if dist[fr] == inf:
                    continue
                for to, cost in adj[fr]:
                    if dist[fr] + cost < dist[to]:
                        dist[to] = dist[fr] + cost
                        updated = 1
            if not updated:
                break
            if i == N - 1:
                if updated:
                    neg_loop_flg = 1

        return dist, neg_loop_flg

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

    dist, flg = BellmanFord(adj, 1)
    if flg:
        print('inf')
    else:
        print(-dist[N])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
g = [[] for _ in range(n)]
for _ in range(m):
  a,b,c = map(int,input().split())
  g[a-1].append([b-1,-c])
cost = [float("inf")]*n
cost[0] = 0
flag = 0
for i in range(n):
  for p,q in g[i]:
    if cost[i] != float("inf") and cost[p] > cost[i]+q:
      cost[p] = cost[i]+q
      if i == n-1:
        flag = 1
if flag == 1:
  print("inf")
else:
  print(-cost[-1])

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
g = [list(map(int,input().split())) for _ in range(m)]
def shortest_path(s,n,w,es):
  d = [-float("inf")] * n
  d[s] = 0
  while True:
    update = False
    for p,q,r in es:
      if d[p-1] != -float("inf") and d[q-1] < d[p-1] + r:
        d[q-1] = d[p-1] + r
        update = True
    if not update:
      break
  return d
def find_negative_loop(n,w,es):
    d = [-float("inf")] * n
    d[0] = 0 
    for i in range(n):
        for j in range(w):
            e = es[j]
            if d[e[1]-1] < d[e[0]-1] + e[2]:
                d[e[1]-1] = d[e[0]-1] + e[2]
                if i == n-1:
                    return True
    return False
if not find_negative_loop(n,m,g):
  print(shortest_path(0,n,m,g)[-1])
else:
  print("inf")

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
g = [[] for _ in range(n)]
for _ in range(m):
  a,b,c = map(int,input().split())
  g[a-1].append([b-1,c])
cost = [-float("inf")]*n
cost[0] = 0
flag = 0
for i in range(n):
  for t,c in g[i]:
    if cost[i] != -float("inf") and cost[t] < cost[i]+c:
      cost[t] = cost[i]+c
      if i == n-1:
        flag = 1
if flag == 0:
  print(cost[-1])
else:
  print("inf")

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, M = LI()
    d = [inf for _ in range(N)]
    d[0] = 0
    edges = []
    for _ in range(M):
        a, b, c = LI_()
        c += 1
        edges.append((a, b, -c))
    for _ in range(N-1):
        for edge in edges:
            fr, to, cost = edge
            if d[to] > d[fr] + cost:
                d[to] = d[fr] + cost
    neg = [False for _ in range(N)]
    for _ in range(N):
        for edge in edges:
            fr, to, cost = edge
            if d[to] > d[fr] + cost:
                neg[to] = True
    if neg[N-1]:
        print('inf')
    else:
        print(-d[N-1])

main()

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, M = LI()
    d = [inf for _ in range(N)]
    d[0] = 0
    edges = []
    for _ in range(M):
        a, b, c = LI_()
        c += 1
        edges.append((a, b, -c))
    for _ in range(N):
        for edge in edges:
            fr, to, cost = edge
            if d[to] > d[fr] + cost:
                d[to] = d[fr] + cost
    neg = [False for _ in range(N)]
    for edge in edges:
        fr, to, cost = edge
        if d[to] > d[fr] + cost:
            neg[fr] = True
            neg[to] = True
    if neg[N-1]:
        print('inf')
    else:
        print(-d[N-1])

main()

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, M = LI()
    d = [inf for _ in range(N)]
    d[0] = 0
    edges = []
    for _ in range(M):
        a, b, c = LI_()
        c += 1
        edges.append((a, b, -c))
    for _ in range(N):
        for edge in edges:
            fr, to, cost = edge
            if d[to] > d[fr] + cost:
                d[to] = d[fr] + cost
    for edge in edges:
        fr, to, cost = edge
        if d[to] > d[fr] + cost:
            print('inf')
            return
    print(-d[N-1])

main()

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, M = LI()
    d = [inf for _ in range(N)]
    d[0] = 0
    edges = []
    for _ in range(M):
        a, b, c = LI_()
        c += 1
        edges.append((a, b, -c))
    for _ in range(N-1):
        for edge in edges:
            fr, to, cost = edge
            if d[to] > d[fr] + cost:
                d[to] = d[fr] + cost
    
    for edge in edges:
        fr, to, cost = edge
        if d[to] > d[fr] + cost:
            print('inf')
            return
    print(-d[N-1])

main()

Submission status: Wrong Answer

Solution code:
n , m = map(int,input().split())
inf = 334334334334334334334334
es = []

def bf():
    d = [inf] * n
    d[0] = 0
    i = 0
    while True:
        update = False
        i += 1
        if i > n:
            return "inf"
        
        for p,q,r in es:
            if d[p] != inf and d[q] > d[p] + r:
                d[q] = d[p] + r
                update = True
        if update == False:
            break   
            
    return d[n-1]

for _ in range(m):
    x , y , z = map(int,input().split())
    es.append([x-1,y-1,-z])
    
a = bf()

if a == "inf":
    print("inf")
else:
    print(-a)

Submission status: Wrong Answer

Solution code:
n , m = map(int,input().split())
inf = 334334334334334334334334
es = []

def bf():
    d = [inf] * n
    d[0] = 0
    i = 0
    while True:
        update = False
        i += 1
        if i > n:
            return "inf"
        
        for p,q,r in es:
            if d[p] != inf and d[q] > d[p] + r:
                d[q] = d[p] + r
                print(d)
                update = True
        if update == False:
            break   
            
    return d[n-1]

for _ in range(m):
    x , y , z = map(int,input().split())
    es.append([x-1,y-1,-z])
    
a = bf()

if a == "inf":
    print("inf")
else:
    print(-a)

Submission status: Wrong Answer

Solution code:
n , m = map(int,input().split())
ainf = -334334334334334334334334
es = []

def bf():
    d = [ainf] * n
    d[0] = 0
    i = 0
    while True:
        update = False
        i += 1
        if i > n:
            return "inf"
        
        for p,q,r in es:
            if d[p] != ainf and d[q] < d[p] + r:
                d[q] = d[p] + r
                update = True
        if update == False:
            break   
            
    return d[n-1]

for _ in range(m):
    x , y , z = map(int,input().split())
    es.append([x-1,y-1,z])
    
print(bf())

Submission status: Wrong Answer

Solution code:
INF = float('inf')
def bellman_ford(n, edges, s):
  min_dist = [INF] * n

  min_dist[s] = 0

  for i in range(n):
    for (start, end, cost) in edges:
      if min_dist[start] != INF and min_dist[start] + cost < min_dist[end]:
        min_dist[end] = min_dist[start] + cost
        if i == n-1:
          return -INF
  
  return min_dist

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

edges = []

for i in range(M):
  edge = list(map(int, input().split()))
  edge[0] -= 1
  edge[1] -= 1
  edge[2] = -edge[2]
  edges.append(edge)
min_dist = bellman_ford(N, edges, 0)

if min_dist == -INF:
  print('inf')
else:
  print(-min_dist[-1])

Submission status: Wrong Answer

Solution code:
import scipy.sparse.csgraph
n,m=map(int,input().split())
graph=[[0]*n for i in range(n)]
for i in range(m):
  u,v,c=map(int,input().split())
  graph[u-1][v-1]=-c
try:
  l=scipy.sparse.csgraph.johnson(graph)
  print(int(-l[0][-1]))
except scipy.sparse.csgraph._shortest_path.NegativeCycleError:
  print('inf')

Submission status: Wrong Answer

Solution code:
import scipy.sparse.csgraph
n,m=map(int,input().split())
graph=[[0]*n for i in range(n)]
for i in range(m):
  u,v,c=map(int,input().split())
  graph[u-1][v-1]=-c
try:
  l=scipy.sparse.csgraph.johnson(graph)
  print(-l[0][-1])
except scipy.sparse.csgraph._shortest_path.NegativeCycleError:
  print('inf')

Submission status: Wrong Answer

Solution code:
V, E = map(int, input().split())
G = [[] for i in range(V+1)]
for i in range(E):
    a, b, c = map(int, input().split())
    G[a-1].append((b-1, 0-c))
G[V-1].append((V, 0))

print("inf")

Submission status: Wrong Answer

Solution code:
V, E = map(int, input().split())
G = [[] for i in range(V+1)]
for i in range(E):
    a, b, c = map(int, input().split())
    G[a-1].append((b-1, 0-c))
G[V-1].append((V, 0))

INF = float('inf')
dist = [INF] * (V + 1)
dist[0] = 0
Flag = False

for i in range(V+1):
    UnUpdate = True
    for v, e in enumerate(G):
        for t, cost in e:
            if dist[v] != INF and dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                UnUpdate = False
    if UnUpdate:
        if i < V:
            Flag = True
        break

if Flag:
    print(0-dist[V])
else:
    print("inf")

Submission status: Wrong Answer

Solution code:
V, E = map(int, input().split())
G = [[] for i in range(V+1)]
for i in range(E):
    a, b, c = map(int, input().split())
    G[a-1].append((b-1, 0-c))
G[V-1].append((V, 0))

INF = 10**18
dist = [INF] * (V + 1)
dist[0] = 0
update = 1
Flag = False

for i in range(V+1):
    UnUpdate = True
    for v, e in enumerate(G):
        for t, cost in e:
            if dist[v] != INF and dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                UnUpdate = False
    if UnUpdate:
        if i < V:
            Flag = True
        break

if Flag:
    print(0-dist[V])
else:
    print("inf")

Submission status: Wrong Answer

Solution code:
V, E = map(int, input().split())
G = [[] for i in range(V+1)]
for i in range(E):
    a, b, c = map(int, input().split())
    G[a-1].append((b-1, 0-c))
G[V-1].append((V, 0))

INF = 10**18
dist = [INF] * (V + 1)
dist[0] = 0
update = 1
Flag = False

for _ in range(V+1):
    update = 0
    for v, e in enumerate(G):
        for t, cost in e:
            if dist[v] != INF and dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                update = 1
    if not update:
        Flag = True
        break

if Flag:
    print(0-dist[V])
else:
    print("inf")

Submission status: Wrong Answer

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

E = [[] for _ in range(n+1)]
for i in range(m):
    ai,bi,ci = map(int, input().split( ))
    ai-=1;bi-=1
    E[ai].append((bi,-ci))
#「nまでの経路で」負閉路が要る
#https://tjkendev.github.io/procon-library/python/graph/bellman-ford.html
# V: グラフの頂点数
# r: 始点
# G[v] = [(w, cost), ...]: 頂点vからコストcostで到達できる頂点w
INF = 10**18
dist = [INF] * n
dist[0] = 0
update = 1
for _ in range(n):
    update = 0
    for v, e in enumerate(E):
        for t, cost in e:
            if dist[v] != INF and dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                update = 1
    if not update:
        break

ans = dist[n-1]


for _ in range(n):
    update = 0
    for v, e in enumerate(E):
        for t, cost in e:
            if dist[v] != INF and dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                update = 1
if ans > dist[n-1]:
    print("inf")
else:
    print(ans)

Submission status: Wrong Answer

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

E = [[] for _ in range(n+1)]
for i in range(m):
    ai,bi,ci = map(int, input().split( ))
    ai-=1;bi-=1
    E[ai].append((bi,-ci))
#「nまでの経路で」負閉路が要る
#https://tjkendev.github.io/procon-library/python/graph/bellman-ford.html
# V: グラフの頂点数
# r: 始点
# G[v] = [(w, cost), ...]: 頂点vからコストcostで到達できる頂点w
INF = 10**18
dist = [INF] * n
dist[0] = 0
update = 1
for _ in range(n):
    update = 0
    for v, e in enumerate(E):
        for t, cost in e:
            if dist[v] != INF and dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                update = 1
    if not update:
        break

ans = dist[n-1]


for _ in range(n):
    update = 0
    for v, e in enumerate(E):
        for t, cost in e:
            if dist[v] != INF and dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                update = 1
if ans < dist[n-1]:
    print("inf")
else:
    print(ans)

Submission status: Wrong Answer

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

E = [[] for _ in range(n+1)]
for i in range(m):
    ai,bi,ci = map(int, input().split( ))
    ai-=1;bi-=1
    E[ai].append((bi,-ci))
#負のスコアを辺長として最短経路求める
#ベルマンフォード

#https://tjkendev.github.io/procon-library/python/graph/bellman-ford.html
# V: グラフの頂点数
# r: 始点
# G[v] = [(w, cost), ...]: 頂点vからコストcostで到達できる頂点w
INF = 10**18
dist = [INF] * n
dist[0] = 0
update = 1
for _ in range(n):
    update = 0
    for v, e in enumerate(E):
        for t, cost in e:
            if dist[v] != INF and dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                update = 1
    if not update:
        break
if update:
    print("inf")
    exit()
print(-dist[n-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(M)]

inf = 10 ** 18
cost = [inf] * (N + 1)
cost[1] = 0
loop = 0
while True:
    loop += 1
    update = False
    for a, b, c in ABC:
        if cost[b] > cost[a] - c:
            cost[b] = cost[a] - c
            update = True
    if not update:
        ans = -cost[N]
        break
    if loop > N:
        ans = "inf"
        break

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(M)]

# できるだけ得点を大きくしたい->なるべく遠回り
# コストを負にして最短路問題BellmanFord
inf = float("inf")
cost = [inf] * (N + 1)
cost[1] = 0
loop = 0
while True:
    loop += 1
    update = False
    for a, b, c in ABC:
        if cost[a] == inf:
            continue
        if cost[b] > cost[a] - c:
            cost[b] = cost[a] - c
            update = True
    if not update:
        ans = -cost[N]
        break
    if loop > N:
        ans = "inf"
        break

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(M)]

# できるだけ得点を大きくしたい->なるべく遠回り
# コストを負にして最短路問題BellmanFord
inf = 10**18
cost = [inf] * (N + 1)
cost[1] = 0
loop = 0
while True:
    loop += 1
    update = False
    for a, b, c in ABC:
        if cost[a] == inf:
            continue
        if cost[b] > cost[a] - c:
            cost[b] = cost[a] - c
            update = True
    if not update:
        ans = -cost[N]
        break
    if loop > N:
        ans = "inf"
        break

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(M)]

# できるだけ得点を大きくしたい->なるべく遠回り
# コストを負にして最短路問題BellmanFord
inf = 10**18
cost = [inf] * (N + 1)
cost[1] = 0
loop = 0
while True:
    loop += 1
    update = False
    for a, b, c in ABC:
        if cost[a] == inf:
            continue
        if cost[b] > cost[a] - c:
            cost[b] = cost[a] - c
            update = True
    if not update:
        ans = -cost[N]
        break
    if loop > M * 2:
        ans = "inf"
        break

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, M: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    inf = float("inf")
    sc = [-inf] * N
    sc[0] = 0
    for _ in range(N - 1):
        for ai, bi, ci in zip(a, b, c):
            ai -= 1
            bi -= 1
            sc[bi] = max(sc[bi], sc[ai] + ci)
    for ai, bi, ci in zip(a, b, c):
        ai -= 1
        bi -= 1
        if sc[ai] + ci > sc[bi]:
            print("inf")
            return
    print(sc[N - 1])


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    a = [int()] * (M)  # type: "List[int]"
    b = [int()] * (M)  # type: "List[int]"
    c = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
    solve(N, M, a, b, c)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, M: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    inf = float("inf")
    sc = [-inf] * N
    sc[0] = 0
    for _ in range(N):
        for ai, bi, ci in zip(a, b, c):
            ai -= 1
            bi -= 1
            sc[bi] = max(sc[bi], sc[ai] + ci)
    for ai, bi, ci in zip(a, b, c):
        ai -= 1
        bi -= 1
        if sc[ai] + ci > sc[bi]:
            print("inf")
            return
    print(sc[N - 1])


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    a = [int()] * (M)  # type: "List[int]"
    b = [int()] * (M)  # type: "List[int]"
    c = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
    solve(N, M, a, b, c)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
INF = float('inf')
# n -> ノード数, edges -> グラフ. Vec<(start, end, cost)>で構成, s -> 始点, e -> 目的地
def bellman_ford(n, edges, s, e):
    
    # 各点への最短距離を保持する配列
    min_dist = [INF] * n

    # start地点への距離は0
    min_dist[s] = 0

    for i in range(n):
        for (start, end, cost) in edges:
            if min_dist[start] != INF and min_dist[start] + cost < min_dist[end]:
                min_dist[end] = min_dist[start] + cost
                # 最短経路の更新がn回目でもまだ行われているようなら閉路あり
                if i == n - 1:
                    return INF 
    return min_dist

n, m = map(int, input().split())
# graph -> (start, end, cost) で管理する
graph = []

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

dist = bellman_ford(n, graph, 0, n - 1)
if dist == INF:
    print("inf")
else:
    print(-dist[n - 1])