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())
a=[]
b=[]
c=[]
for m in range (0,M):
  A,B,C=map(int, input().split() )
  a+=[A]
  b+=[B]
  c+=[C]
V=[0]*N# iに到達するときの最大得点
moji=[1]+[0]*(N-1)#infなら0をかえす
 
for n in range(N-1):
  for m in range(M):
    u=a[m]
    v=b[m]
    if moji[a[m]-1]:#共に有限の時はやる
      if moji[b[m]-1]:
        V[b[m]-1]=max(V[b[m]-1],V[a[m]-1]+c[m])
      else:
        V[b[m]-1]=V[a[m]-1]+c[m]
        moji[b[m]-1]=1

W=[0]*N
moji=[0]*(N-1)+[1]
for n in range(N):
  for m in range(M):
    u=a[m]
    v=b[m]
    if moji[a[m]-1]:#共に有限の時はやる
      if moji[b[m]-1]:
        W[b[m]-1]=max(W[b[m]-1],W[a[m]-1]+c[m])
      else:
        W[b[m]-1]=W[a[m]-1]+c[m]
        moji[b[m]-1]=1
if W[N-1]<=0:
  print(V[N-1])
else:
  print("inf")

Submission status: Wrong Answer

Solution code:

def calc_weight(pw, weights):
    new_pw = []
    k = 0
    for p, w in pw:
        if p == len(weights) - 1:
            new_pw.append([p, w])            
        li = weights[p]
        non_zero = [[i, x] for i, x in enumerate(li) if x != 0]
        for j, x in non_zero:
            new_pw.append([j, x + w])
    return new_pw

N, M = map(int, raw_input().split())
matrix = [[0 for i in range(N)] for j in range(N)]
for i in range(M):
    _a, _b, _c = map(int, raw_input().split())
    matrix[_a - 1][_b - 1] = _c

cur_pw = [[0, 0]]
for i in range(0, N):
    cur_pw = calc_weight(cur_pw, matrix)

ans = [[i, j] for i, j in cur_pw if i == N - 1]
non_ans = [[i, j] for i, j in cur_pw if i != N - 1]
if non_ans:
    print "inf"
else:
    ans.sort(key=lambda x:x[1], reverse=True)
    print ans[0][1]

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int, input().split())
a=[]
b=[]
c=[]
for m in range (0,M):
  A,B,C=map(int, input().split() )
  a+=[A]
  b+=[B]
  c+=[C]
V=[0]*N# iに到達するときの最大得点
moji=[1]+[0]*(N-1)#infなら0をかえす
 
for n in range(N-1):
  for m in range(M):
    u=a[m]
    v=b[m]
    if moji[a[m]-1]:#共に有限の時はやる
      if moji[b[m]-1]:
        V[b[m]-1]=max(V[b[m]-1],V[a[m]-1]+c[m])
      else:
        V[b[m]-1]=V[a[m]-1]+c[m]
        moji[b[m]-1]=1
 
for m in range(M):#Nが通らないループを見てるな
  u=a[m]
  v=b[m]
  if moji[b[m]-1]*moji[a[m]-1]:
    if V[b[m]-1]<V[a[m]-1]+c[m]:
      print("inf")
      sys.exit()
print(V[N-1])

Submission status: Wrong Answer

Solution code:
def calc_weight(pw, weights):
    new_pw = []
    k = 0
    for p, w in pw:
        if p == len(weights) - 1:
            new_pw.append([p, w])            
        li = weights[p]
        non_zero = [[i, x] for i, x in enumerate(li) if x != 0]
        for j, x in non_zero:
            new_pw.append([j, x + w])
    return new_pw

N, M = map(int, raw_input().split())
matrix = [[0 for i in range(N)] for j in range(N)]
for i in range(M):
    _a, _b, _c = map(int, raw_input().split())
    matrix[_a - 1][_b - 1] = _c

cur_pw = [[0, 0]]
for i in range(0, M):
    print cur_pw
    cur_pw = calc_weight(cur_pw, matrix)

ans = [[i, j] for i, j in cur_pw if i == N - 1]
non_ans = [[i, j] for i, j in cur_pw if i != N - 1]
if non_ans:
    print "inf"
else:
    ans.sort(key=lambda x:x[1], reverse=True)
    print ans[0][1]

Submission status: Wrong Answer

Solution code:
import math

INF = float("inf")

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

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

d = [-INF for _ in range(V + 1)]
d[1] = 0
cnt = 0

while True:
    update = False
    for e in edge:
        if not math.isinf(d[e[0]]) and d[e[1]] < d[e[0]] + e[2]:
            update = True
            d[e[1]] = d[e[0]] + e[2]
            if e[1] == V:
                cnt += 1
    if not update:
        break
    if cnt == V - 1:
        print("inf")
        exit()
print(d[V])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
NM = list(map(int,input().split()))
ABC = [list(map(int, input().split())) for _ in range(NM[1])]
s = 0
i = 1
now = 1
r = 0
kako = []
flag = 0
for a,b,c in ABC:
    if b in set(kako):
        print("inf")
        flag = 1
        break
    else:
        if now == a:
            if a not in kako:
                kako.append(a)
            s += c
            now = b
        else:
            flag = 2
            if r == 0:
                r = s
            elif r < s:
                r = s
                s = 0
if flag == 2:
    print(r)
elif flag == 0:
    print(s)

Submission status: Wrong Answer

Solution code:
NM = list(map(int,input().split()))
ABC = [list(map(int, input().split())) for _ in range(NM[1])]
s = 0
i = 1
now = 1
r = 0
kako = []
flag = 0
for a,b,c in ABC:
    if b in kako:
        print("inf")
        flag = 1
        break
    else:
        if now == a:
            kako.append(a)
            s += c
            now = b
        else:
            flag = 2
            if r == 0:
                r = s
            elif r < s:
                r = s
                s = 0


if flag == 2:
    print(r)
elif flag == 0:
    print(s)

Submission status: Wrong Answer

Solution code:
NM = list(map(int,input().split()))
ABC = [list(map(int, input().split())) for _ in range(NM[1])]
s = 0
i = 1
now = 1
r = 0
kako = []
flag = 0
for a,b,c in ABC:
    if b in kako:
        print("inf")
        flag = 1
        break
    else:
        if now == a:
            kako.append(a)
            s += c
            now = b
        else:
            flag = 2
            if r == 0:
                r = s
            elif r < s:
                r = s
                s = 0


if flag == 2:
    print(r)
elif flag == 0:
    print(s)

Submission status: Wrong Answer

Solution code:
n, m = map(int, raw_input().split())
a, b, c = [], [], []
for i in xrange(m):
    at, bt, ct = map(int, raw_input().split())
    a.append(at - 1)
    b.append(bt - 1)
    c.append(ct)
point = [None] * n
pointl = [[]] * n
point[0] = 0
pointl[0] = [0]
while 1:
    prevpoint = list(point)
    for i in xrange(n):
        maxp = -1e9 * m
        maxpl = []
        for j in xrange(m):
            if b[j] != i:
                continue
            if point[a[j]] == None:
                continue
            if maxp <= point[a[j]] + c[j]:
                maxp = point[a[j]] + c[j]
                maxpl = pointl[a[j]] + [b[j]]
        if len(maxpl) == 0:
            continue
        point[i] = maxp
        pointl[i] = maxpl

    if point == prevpoint:
        print(point[n-1])
        break
    elif len(set(pointl[n-1])) < len(pointl[n-1]):
        print("inf")
        break

Submission status: Wrong Answer

Solution code:
#coding:utf-8

import sys
from collections import defaultdict

if __name__ == "__main__":
    N, M = map(int, input().split(" "))

    V = set()
    E = []
    for i in range(M):
        a, b, c = map(int, input().split(" "))
        V.add(a)
        V.add(b)
        E.append((a, b, -c))

    dist = defaultdict(lambda :10**12 + 1)
    dist[1] = 0


    for i in range(1, len(V)):
        for edge in E:
            u = edge[0]
            v = edge[1]

            if dist[v] > dist[u] + edge[2]:
                dist[v] = dist[u] + edge[2]

    is_cycle = False
    for edge in E:
        u = edge[0]
        v = edge[1]
        if dist[u] + edge[2] < dist[v]:
            is_cycle = True

    if is_cycle:
        print ("inf")
    else:
        print (-dist[N])

Submission status: Wrong Answer

Solution code:
NM = list(map(int,input().split()))
ABC = [list(map(int, input().split())) for _ in range(NM[1])]
s = 0
i = 1
now = 1
r = 0
kako = []
flag = 0
for a,b,c in ABC:
    if a == NM[0] and b in kako:
        print("inf")
        flag = 1
        break
    else:
        if now == a:
            kako.append(a)
            s += c
            now = b
        else:
            kako.clear()
            flag = 2
            if r == 0:
                r = s
            elif r < s:
                r = s
                s = 0


if flag == 2:
    print(r)
elif flag == 0:
    print

Submission status: Wrong Answer

Solution code:
n, m = [int(i) for i in input().split()]

abc=[]
for i in range(m):
    abc.append([int(i) for i in input().split()])

point = [0]+[-999999999999999]*(n-1)

cnt=0
bflg=0
while True:
    for i in abc:
        cnt=cnt+1
        point[i[1]-1] = max(point[i[1]-1], point[i[0]-1] + i[2])
        if cnt > n-1:
            bflg=1
            break
    if bflg==1: break

print(point[n-1]) if cnt <= n-1 else print('inf')

Submission status: Wrong Answer

Solution code:
def main():
    N, M = map(int, input().split())
    abc = [None] * M
    for i in range(M):
        abc[i] = tuple(map(int, input().split()))

    start = 1
    goal = N
    node = 0
    point = 0
    for i in abc:
        if i[0] == start:
            node = i[1]
            point += i[2]
        elif i[0] == node:
            if i[1] == start:
                print('inf')
                break
            node = i[1]
            point += i[2]
            if i[1] == goal:
                print(point)
                break

main()

Submission status: Wrong Answer

Solution code:
#coding:utf-8

import sys
from collections import defaultdict

if __name__ == "__main__":
    N, M = map(int, input().split(" "))

    V = set()
    E = []
    for i in range(M):
        a, b, c = map(int, input().split(" "))
        V.add(a)
        V.add(b)
        E.append((a, b, -c))

    dist = defaultdict(lambda :10**9 - 1)
    dist[1] = 0


    for i in range(1, len(V)):
        for edge in E:
            u = edge[0]
            v = edge[1]

            if dist[v] > dist[u] + edge[2]:
                dist[v] = dist[u] + edge[2]

    is_cycle = False
    for edge in E:
        u = edge[0]
        v = edge[1]
        if dist[u] + edge[2] < dist[v]:
            is_cycle = True

    if is_cycle:
        print ("inf")
    else:
        print (-dist[N])

Submission status: Wrong Answer

Solution code:
import math

INF = float("inf")

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

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

d = [-INF for _ in range(V + 1)]
d[1] = 0
cnt = 0

while True:
    update = False
    for e in edge:
        if not math.isinf(d[e[0]]) and d[e[1]] < d[e[0]] + e[2]:
            update = True
            d[e[1]] = d[e[0]] + e[2]
    if not update:
        break
    if cnt == V:
        print("inf")
        exit()
    cnt += 1
print(d[V])

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int, input().split())
a=[]
b=[]
c=[]
for m in range (0,M):
  A,B,C=map(int, input().split() )
  a+=[A]
  b+=[B]
  c+=[C]
V=[0]+["inf"]*(N-1)# iに到達するときの最大得点
moji=[1]+[0]*(N-1)#infなら0をかえす

for n in range(N-1):
  for m in range(M):
    u=a[m]
    v=b[m]
    if moji[a[m]-1]:#共に有限の時はやる
      if moji[b[m]-1]:
        V[b[m]-1]=max(V[b[m]-1],V[a[m]-1]+c[m])
      else:
        V[b[m]-1]=V[a[m]-1]+c[m]
        moji[b[m]-1]=1

for m in range(M):
  u=a[m]
  v=b[m]
  if moji[b[m]-1]*moji[a[m]-1]:
    if V[b[m]-1]<V[a[m]-1]+c[m]:
      print("inf")
      sys.exit()

for n in range(N-1):
  for m in range(M):
    u=a[m]
    v=b[m]
    if moji[a[m]-1]:#共に有限の時はやる
      if moji[b[m]-1]:
        V[b[m]-1]=max(V[b[m]-1],V[a[m]-1]+c[m])
      else:
        V[b[m]-1]=V[a[m]-1]+c[m]
        moji[b[m]-1]=1

print(V[N-1])

Submission status: Wrong Answer

Solution code:
def update(edge, last, lastnode):
    v = edge[1]
    u = edge[0]
    w = edge[2]

    if (dist[v] > dist[u] + w and dist[u] != float('inf')):
        if not last:
            dist[v] = dist[u] + w
            return True
        else:
            if v == lastnode:
                return True
            else:
                return False

    return False


v, e = map(int, raw_input().split())
dist = [float('inf') for i in range(v)]
edgelist = []

for i in range(e):
    v1, v2, w = map(int, raw_input().split())
    v1 -= 1
    v2 -= 1
    w = -w
    edgelist.append( [v1, v2, w] )

dist[0] = 0
flag = False
for i in range(v-1):
    for edge in edgelist:
        up = update(edge, False, v-1)
        flag = flag | up
    if not flag:
        break

neg_cyc = False
for edge in edgelist:
    neg_cyc = neg_cyc | update(edge,True, v-1)

if (neg_cyc):
    print "inf"
else:
    print -dist[v-1]

Submission status: Wrong Answer

Solution code:
n,m = map(int, raw_input().split())
d = {}
for i in range(m):
  a,b,c = map(int, raw_input().split())
  if a in d:
    d[a].append((b,c))
  else:
    d[a] = [(b,c)]


anslist = []

# point, total, visited, False
q = [(1,0,set(), False)]

while len(q) > 0:
  p,t,v,f = q.pop(0)

  if p not in v:
    v.add(p)

  # print p,t,v
  if p not in d:
    continue

  flag = False
  for b,c in d[p]:
    if b in v:
      flag = True

  f = f or flag
  for b,c in d[p]:
    if b in v:
      continue
    q.append((b,t+c,v.copy(), f))

  if p == n:
    anslist.append((t,f))

ans = -1 * float('inf')
for t,f in anslist:
  if f:
    print 'inf'
    exit()
  ans = max(ans, t)

print ans

Submission status: Wrong Answer

Solution code:
N,M = map(int,raw_input().split())

W = [['minf' for j in range(N)] for i in range(N)]
y = ['minf' for i in range(N)]
y_t = ['minf' for i in range(N)]
y[0] = 0
y_t[0] = 0

for i in range(N):
    W[i][i]=0

for i in range(M):
    a,b,c=map(int,raw_input().split())
    W[a-1][b-1] = c

flag = False

for i in range(2*N):
    for j in range(N):
        yj_max = 'minf'
        for k in range(N):
            yj_temp = 'minf'
            if y[k]!='minf' and W[k][j]!='minf':
                yj_temp=y[k]+W[k][j]

                if yj_max=='minf':
                    yj_max = yj_temp
                elif yj_max<yj_temp:
                    yj_max = yj_temp
        y_t[j]=yj_max
    if y==y_t:
        flag = True
        break
    y = y_t

print y[N-1]

Submission status: Wrong Answer

Solution code:
N,M = map(int,raw_input().split())

W = [['inf' for j in range(N)] for i in range(N)]
y = ['inf' for i in range(N)]
y_t = ['inf' for i in range(N)]
y[0] = 0
y_t[0] = 0

for i in range(N):
    W[i][i]=0

for i in range(M):
    a,b,c=map(int,raw_input().split())
    W[a-1][b-1] = c

flag = False

for i in range(2*N):
    for j in range(N):
        yj_max = 'inf'
        for k in range(N):
            yj_temp = 'inf'
            if y[k]!='inf' and W[k][j]!='inf':
                yj_temp=y[k]+W[k][j]

                if yj_max=='inf':
                    yj_max = yj_temp
                elif yj_max<yj_temp:
                    yj_max = yj_temp
        y_t[j]=yj_max
    if y==y_t:
        flag = True
        break
    y = y_t

print y[N-1]

Submission status: Wrong Answer

Solution code:
n, m = [int(i) for i in input().split()]

abc=[]
for i in range(m):
    abc.append([int(i) for i in input().split()])

point = [0]+[-999999999999999]*(n-1)

cnt=0
while True:
    for i in abc:
        cnt=cnt+1
        point[i[1]-1] = max(point[i[1]-1], point[i[0]-1] + i[2])
    if cnt > n-1: break

print(point[n-1]) if cnt <= n-1 else print('inf')

Submission status: Wrong Answer

Solution code:
NM = list(map(int,input().split()))
ABC = [list(map(int, input().split())) for _ in range(NM[1])]
s = 0
i = 1
now = 1
r = 0
kako = []
flag = 0
for a,b,c in ABC:
    if a == NM[0] and b in kako:
        print("inf")
        flag = 1
        break
    else:
        if now == a:
            kako.append(a)
            s += c
            now = b
        else:
            flag = 2
            if r == 0:
                r = s
            elif r < s:
                r = s
                s = 0

if flag == 2:
    print(r)
elif flag == 0:
    print(s)

Submission status: Wrong Answer

Solution code:
N,M = map(int,raw_input().split())

W = [['inf' for j in range(N)] for i in range(N)]
y = ['inf' for i in range(N)]
y_t = ['inf' for i in range(N)]
y[0] = 0
y_t[0] = 0

for i in range(N):
    W[i][i]=0

for i in range(M):
    a,b,c=map(int,raw_input().split())
    W[a-1][b-1] = c

flag = False

for i in range(2*N):
    for j in range(N):
        yj_max = 'inf'
        for k in range(N):
            yj_temp = 'inf'
            if y[k]!='inf' and W[k][j]!='inf':
                yj_temp=y[k]+W[k][j]

                if yj_max=='inf':
                    yj_max = yj_temp
                elif yj_max<yj_temp:
                    yj_max = yj_temp
        y_t[j]=yj_max
    if y==y_t:
        flag = True
        break
    y = y_t


print 'inf'

Submission status: Wrong Answer

Solution code:
n, m = [int(i) for i in input().split()]

abc=[]
for i in range(m):
    abc.append([int(i) for i in input().split()])

point = [0]+[-999999999999999]*(n-1)

cnt=0
while True:
    for i in abc:
        cnt=cnt+1
        point[i[1]-1] = max(point[i[1]-1], point[i[0]-1] + i[2])
    if cnt > n-1: break

print(point[n-1]) if cnt > n-1 else print('inf')

Submission status: Wrong Answer

Solution code:
n, m = [int(i) for i in input().split()]

abc=[]
for i in range(m):
    abc.append([int(i) for i in input().split()])

point = [0]+[-999999999999999]*(n-1)

cnt=0
while True:
    for i in abc:
        cnt=cnt+1
        point[i[1]-1] = max(point[i[1]-1], point[i[0]-1] + i[2])
    if cnt > n-1: break

print(point[n-1])

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int, input().split())
a=[]
b=[]
c=[]
for m in range (0,M):
  A,B,C=map(int, input().split() )
  a+=[A]
  b+=[B]
  c+=[C]
V=[0]+["inf"]*(N-1)# iに到達するときの最大得点
moji=[1]+[0]*(N-1)#infなら0をかえす

for n in range(N-1):
  for m in range(M):
    u=a[m]
    v=b[m]
    if moji[a[m]-1]:#共に有限の時はやる
      if moji[b[m]-1]:
        V[b[m]-1]=max(V[b[m]-1],V[a[m]-1]+c[m])
      else:
        V[b[m]-1]=V[a[m]-1]+c[m]
        moji[b[m]-1]=1

for m in range(M):
  u=a[m]
  v=b[m]
  if moji[b[m]-1]*moji[a[m]-1]:
    if V[b[m]-1]<V[a[m]-1]+c[m]:
      print("inf")
      sys.exit()
print(V[N-1])

Submission status: Wrong Answer

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

nodemap = [[] for x in range(n + 1)]

traveled_template = [False for x in range(n + 1)]

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

# for nodes in nodemap:
#     print(nodes)

que = [[nodemap[1], 0, traveled_template[:]]]
maxn = -(10**10)
while que:
    nodes, value, traveled = que.pop()
    for node in nodes:
        t = traveled[:]
        now = node[0]
        next_value = value + node[1]
        # print(node, value + node[1])
        # print(node, value, now, n)
        if t[now] != False:
            if t[now] < next_value:
                print("inf")
                __import__("sys").exit()
            else:
                pass
        else:
            if now == n:
                maxn = max(maxn, next_value)
            t[now] = next_value
            que.append([nodemap[now], next_value, t])

print(maxn)

Submission status: Wrong Answer

Solution code:
N,M = map(int,raw_input().split())

W = [['inf' for j in range(N)] for i in range(N)]
y = ['inf' for i in range(N)]
y_t = ['inf' for i in range(N)]
y[0] = 0
y_t[0] = 0

for i in range(N):
    W[i][i]=0

for i in range(M):
    a,b,c=map(int,raw_input().split())
    W[a-1][b-1] = c

flag = False

for i in range(4*N):
    for j in range(N):
        yj_max = 'inf'
        for k in range(N):
            yj_temp = 'inf'
            if y[k]!='inf' and W[k][j]!='inf':
                yj_temp=y[k]+W[k][j]

                if yj_max=='inf':
                    yj_max = yj_temp
                elif yj_max<yj_temp:
                    yj_max = yj_temp
        y_t[j]=yj_max
    if y==y_t:
        flag = True
        break
    y = y_t
    
if flag:
    print y[N-1]
else:
    print 'inf'

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int, input().split())
a=[]
b=[]
c=[]
for m in range (0,M):
  A,B,C=map(int, input().split() )
  a+=[A]
  b+=[B]
  c+=[C]
V=[0]+["inf"]*(N-1)# iに到達するときの最大得点

for n in range(N-1):
  for m in range(M):
    u=a[m]
    v=b[m]
    if isinstance(V[a[m]-1],int):#共に有限の時はやる
      if isinstance(V[b[m]-1],int):
        V[b[m]-1]=max(V[b[m]-1],V[a[m]-1]+c[m])
      else:
        V[b[m]-1]=V[a[m]-1]+c[m]

for m in range(M):
  u=a[m]
  v=b[m]
  if isinstance(V[b[m]-1],int) and isinstance(V[a[m]-1],int):
    if V[b[m]-1]<V[a[m]-1]+c[m]:
      print("inf")
      sys.exit()
print(V[N-1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

def calc_weight(pw, weights):
    new_pw = []
    for p, w in pw:
        if p == len(weights) - 1:
            new_pw.append([p, w])
        li = weights[p]
        non_zero = [[i, x] for i, x in enumerate(li) if x != 0]
        for i, x in non_zero:
            new_pw.append([i, x + w])
    return new_pw

N, M = map(int, raw_input().split())
matrix = [[0 for i in range(N)] for j in range(N)]
for i in range(M):
    _a, _b, _c = map(int, raw_input().split())
    matrix[_a - 1][_b - 1] = _c

cur_pw = [[0, 0]]
# print matrix
for i in range(0, min(N * (N - 1), 2000)):
    cur_pw = calc_weight(cur_pw, matrix)

ans = [[i, j] for i, j in cur_pw if i == N - 1]
non_ans = [[i, j] for i, j in cur_pw if i != N - 1]
if non_ans:
    print "inf"
else:
    ans.sort(key=lambda x:x[1], reverse=True)
    print ans[0][1]

Submission status: Wrong Answer

Solution code:
import collections

def solve():
    n, m = map(int, input().split())

    cost = collections.defaultdict(dict)
    for _ in range(m):
        a, b, c = map(int, input().split())
        cost[a][b] = c

    goals = set()
    loops = set()

    ans = float('-inf')
    stack = [(1, 0, set())]
    while stack:
        x, cnt, memo = stack.pop()
        if x in memo:
            loops.update(memo)
            continue

        if x == n:
            ans = max(ans, cnt)
            goals.update(memo)

        for y in cost[x]:
            temp = set(memo)
            temp.add(x)
            stack.append((y, cnt + cost[x][y], temp))

    return ans if len(loops.intersection(goals)) < 1 else 'inf'

if __name__ == '__main__':
    ans = solve()
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int, input().split())
a=[]
b=[]
c=[]
for m in range (0,M):
  A,B,C=map(int, input().split() )
  a+=[A]
  b+=[B]
  c+=[C]
V=[0]+["inf"]*(N-1)# iに到達するときの最大得点

for n in range(N-1):
  for m in range(M):
    u=a[m]
    v=b[m]
    if isinstance(V[b[m]-1],int) and isinstance(V[a[m]-1],int):#共に有限の時はやる
      if V[b[m]-1]<V[a[m]-1]+c[m]:
        V[b[m]-1]=V[a[m]-1]+c[m]
    if isinstance(V[b[m]-1],str) and isinstance(V[a[m]-1],int):#始点が有限の時はやる
      V[b[m]-1]=V[a[m]-1]+c[m]

for m in range(M):
  u=a[m]
  v=b[m]
  if isinstance(V[b[m]-1],int) and isinstance(V[a[m]-1],int):
    if V[b[m]-1]<V[a[m]-1]+c[m]:
      print("inf")
      sys.exit()
print(V[N-1])

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10**7)
 
inf = 1<<100
 
def solve():
    N, M = map(int, input().split())
    edges = [None]*M
    Adj = [[] for i in range(N)]
 
    for i in range(M):
        ui, vi, ci = map(int, sys.stdin.readline().split())
        ui, vi = ui - 1, vi - 1
        edges[i] = (ui, vi, -ci)
        Adj[ui].append(vi)

    visitable = [False]*N
    dfs(N, M, Adj, visitable, 0)
 
    ans = Bellmanford(N, M, edges, visitable)
 
    if ans is not None:
        print(-ans)
    else:
        print('inf')

def dfs(N, M, Adj, visitable, u):
    visitable[u] = True

    for v in Adj[u]:
        if not visitable[v]:
            dfs(N, M, Adj, visitable, v)

def Bellmanford(N, M, edges, visitable):
    dist = [inf]*N
    dist[0] = 0
    flag = True

    cnt = 0
 
    for i in range(N):
        flag = False

        for (u, v, c) in edges:
            if dist[u] != inf and dist[v] > dist[u] + c:
                dist[v] = dist[u] + c

                if visitable[u] or visitable[v]:
                    flag = True
                    
        # print(dist)

        if not flag:
            break

        if flag and i == N - 1:
            return None

    return dist[N - 1]
 
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import math

INF = float("inf")

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

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

d = [-INF for _ in range(V + 1)]
d[1] = 0
cnt = 0

while True:
    update = False
    for e in edge:
        if not math.isinf(d[e[0]]) and d[e[1]] < d[e[0]] + e[2]:
            update = True
            d[e[1]] = d[e[0]] + e[2]
    if not update:
        break
    if cnt == V - 1:
        print("inf")
        exit()
    cnt += 1
print(d[V])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
import os
import math

N, M = map(int, input().split())
#print('N', N)
#print('M', M)

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

dists = [float('inf')] * N
dists[0] = 0
def bellman_ford(A):
    for i in range(N):
        for v in range(N):
            for edge in A[v]:
                to = edge[0]
                cost = edge[1]
                if dists[v] != float('inf') and dists[to] > dists[v] + cost:
                    dists[to] = dists[v] + cost
                    if i == N - 1:
                        return True
    return False

res = bellman_ford(A)
#print(res)

if res == True:
    print('inf')
else:
    # パスがある
    print(-dists[N-1])

Submission status: Wrong Answer

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

nodemap = [[] for x in range(n + 1)]

traveled_template = [False for x in range(n + 1)]

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

# for nodes in nodemap:
#     print(nodes)

que = [[nodemap[1], 0, traveled_template[:]]]
maxn = -(10**10)
while que:
    nodes, value, traveled = que.pop()
    for node in nodes:
        t = traveled[:]
        now = node[0]
        next_value = value + node[1]
        # print(node, value + node[1])
        # print(node, value, now, n)
        if t[now] != False:
            if t[now] <= next_value:
                print("inf")
                __import__("sys").exit()
            else:
                pass
        else:
            if now == n:
                maxn = max(maxn, next_value)
            t[now] = next_value
            que.append([nodemap[now], next_value, t])

print(maxn)

Submission status: Wrong Answer

Solution code:
N,M = map(int,raw_input().split())

W = [['inf' for j in range(N)] for i in range(N)]
y = ['inf' for i in range(N)]
y_t = ['inf' for i in range(N)]
y[0] = 0
y_t[0] = 0

for i in range(N):
    W[i][i]=0

for i in range(M):
    a,b,c=map(int,raw_input().split())
    W[a-1][b-1] = c

flag = False

for i in range(2*N):
    for j in range(N):
        yj_max = 'inf'
        for k in range(N):
            yj_temp = 'inf'
            if y[k]!='inf' and W[k][j]!='inf':
                yj_temp=y[k]+W[k][j]

                if yj_max=='inf':
                    yj_max = yj_temp
                elif yj_max<yj_temp:
                    yj_max = yj_temp
        y_t[j]=yj_max
    if y==y_t:
        flag = True
        break
    y = y_t
    
if flag:
    print y[N-1]
else:
    print 'inf'

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
dist = [-10**18] * n
edge = []
for i in range(m):
    a, b, c = map(int, input().split())
    edge.append((a - 1, b - 1, c))
dist[0] = 0
cnt = 0
while True:
    updated = False
    for i in edge:
        a, b, c = i
        if dist[a] + c > dist[b]:
            dist[b] = dist[a] + c
            updated = True
    if not updated:
        break
    cnt += 1
    if cnt > n:
        dist[n - 1] = float("inf")
        break
print(dist[n - 1])

Submission status: Wrong Answer

Solution code:
N,M = map(int,raw_input().split())

W = [['inf' for j in range(N)] for i in range(N)]
y = ['inf' for i in range(N)]
y_t = ['inf' for i in range(N)]
y[0] = 0
y_t[0] = 0

for i in range(N):
    W[i][i]=0

for i in range(M):
    a,b,c=map(int,raw_input().split())
    W[a-1][b-1] = c

flag = False

for i in range(N):
    for j in range(N):
        yj_max = 'inf'
        for k in range(N):
            yj_temp = 'inf'
            if y[k]!='inf' and W[k][j]!='inf':
                yj_temp=y[k]+W[k][j]

                if yj_max=='inf':
                    yj_max = yj_temp
                elif yj_max<yj_temp:
                    yj_max = yj_temp
        y_t[j]=yj_max
    if y==y_t:
        flag = True
        break
    y = y_t
    
if flag:
    print y[N-1]
else:
    print 'inf'

Submission status: Wrong Answer

Solution code:
def read_data():
    N, M = map(int, input().split())
    Es = [[] for _ in range(N)]
    Cs = [[float('inf')] * N for n in range(N)]
    for m in range(M):
        a, b, c = map(int, input().split())
        Es[a - 1].append(b - 1)
        Cs[a - 1][b - 1] = -c
    return N, M, Es, Cs

def solve(N, M, Es, Cs):
    dist = [float('inf')] * N
    dist[0] = 0
    for i in range(N):
        for v in range(N):
            Cv = Cs[v]
            for e in Es[v]:
                c = Cv[e]
                if dist[v] != float('inf') and dist[e] >= dist[v] + c:
                    dist[e] = dist[v] + c
                    if (i == N - 1):
                        return 'inf'
    return -dist[N - 1]

N, M, Es, Cs = read_data()
print(solve(N, M, Es, Cs))