Problem:


Score : 500 points

Problem Statement

There is a directed graph with N vertices numbered 1 to N and M edges.
The i-th edge is directed from Vertex A_i to Vertex B_i, and there are C_i coins placed along that edge.
Additionally, there is a button on Vertex N.

We will play a game on this graph.
You start the game on Vertex 1 with zero coins, and head for Vertex N by traversing the edges while collecting coins.
It takes one minute to traverse an edge, and you can collect the coins placed along the edge each time you traverse it.
As usual in games, even if you traverse an edge once and collect the coins, the same number of coins will reappear next time you traverse that edge, which you can collect again.

When you reach Vertex N, you can end the game by pressing the button. (You can also choose to leave Vertex N without pressing the button and continue traveling.)
However, when you end the game, you will be asked to pay T \times P coins, where T is the number of minutes elapsed since the start of the game. If you have less than T \times P coins, you will have to pay all of your coins instead.

Your score will be the number of coins you have after this payment.
Determine if there exists a maximum value of the score that can be obtained. If the answer is yes, find that maximum value.

Constraints

2 \leq N \leq 2500

1 \leq M \leq 5000

1 \leq A_i, B_i \leq N

1 \leq C_i \leq 10^5

0 \leq P \leq 10^5

All values in input are integers.

Vertex N can be reached from Vertex 1.

Input

Input is given from Standard Input in the following format:

N M P
A_1 B_1 C_1
:
A_M B_M C_M

Output

If there exists a maximum value of the score that can be obtained, print that maximum value; otherwise, print -1.

Sample Input 1

3 3 10
1 2 20
2 3 30
1 3 45

Sample Output 1

35

There are two ways to travel from Vertex 1 to Vertex 3:

Vertex 1 \rightarrow 2 \rightarrow 3: You collect 20 + 30 = 50 coins on the way. After two minutes from the start of the game, you press the button, pay 2 \times 10 = 20 coins, and you have 50 - 20 = 30 coins left.

Vertex 1 \rightarrow 2: You collect 45 coins on the way. After one minute from the start of the game, you press the button, pay 1 \times 10 = 10 coins, and you have 45 - 10 = 35 coins left.

Thus, the maximum score that can be obtained is 35.

Sample Input 2

2 2 10
1 2 100
2 2 100

Sample Output 2

-1

The edge extending from Vertex 1 takes you to Vertex 2. If you then traverse the edge extending from Vertex 2 to itself t times and press the button, your score will be 90 + 90t. Thus, you can infinitely increase your score, which means there is no maximum value of the score that can be obtained.

Sample Input 3

4 5 10
1 2 1
1 4 1
3 4 1
2 2 100
3 3 100

Sample Output 3

0

There is no way to travel from Vertex 1 to Vertex 4 other than traversing the edge leading from Vertex 1 to Vertex 4 directly. You will pick up one coin along this edge, but after being asked to paying 10 coins, your score will be 0.

Note that you can collect an infinite number of coins if you traverse the edge leading from Vertex 1 to Vertex 2, but this is pointless since you can no longer reach Vertex 4 and end the game.



Sample input:
3 3 10
1 2 20
2 3 30
1 3 45


Sample output:
35

Submission status: Wrong Answer

Solution code:


def b_ford(connections:list,N):
    inf=float('inf')
    cost =[inf]*N
    cost[0]=0
    newchanged = set()
    oldchanged = set()

    for i in range(N):
        oldchanged=newchanged
        newchanged= set()
        for A,B,C in connections:
            if cost[A]+C < cost[B]:
                newchanged.add(B)
                cost[B]=cost[A]+C
        
        if N-1 in oldchanged and not N-1 in newchanged and cost[-1]<0:
            print(-cost[-1])
            return 

    if cost[-1]<0:
        print(-1)
    else:
        print(0)






def resolve():
    N,M,P = map(int,input().split())
    
    connections = []
    for i in range(M):
        A,B,C = map(int,input().split())
        
        connections.append([A-1,B-1,P-C])

    b_ford(connections,N)










if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:



def b_ford(connections:list,N):
    inf=float('inf')
    cost =[inf]*N
    cost[0]=0
    newchanged = set()
    oldchanged = set()

    for i in range(N):
        oldchanged=newchanged
        newchanged= set()
        for A,B,C in connections:
            if cost[A]+C < cost[B]:
                newchanged.add(B)
                cost[B]=cost[A]+C
        
        if N-1 in oldchanged and not N-1 in newchanged:
            if cost[-1] > 0:
                break
            else:
                print(-cost[-1])
            return
    
    else:
        print(-1)
        return
    print(0)







def resolve():
    N,M,P = map(int,input().split())
    
    connections = []
    for i in range(M):
        A,B,C = map(int,input().split())
        
        connections.append([A-1,B-1,P-C])

    b_ford(connections,N)










if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:



def b_ford(connections:list,N):
    inf=float('inf')
    cost =[inf]*N
    cost[0]=0
    newchanged = set()
    oldchanged = set()

    for i in range(N):
        oldchanged=newchanged
        newchanged= set()
        for A,B,C in connections:
            if cost[A]+C < cost[B]:
                newchanged.add(B)
                cost[B]=cost[A]+C
        
        if N-1 in oldchanged and not N-1 in newchanged:
            if cost[-1] > 0:
                print(0)
            else:
                print(-cost[-1])
            return
    print(-1)







def resolve():
    N,M,P = map(int,input().split())
    
    connections = []
    for i in range(M):
        A,B,C = map(int,input().split())
        
        connections.append([A-1,B-1,P-C])

    b_ford(connections,N)










if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
import time
start = time.time()
n, m, p = map(int, input().split())
edges = []
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a, b, p-c))
    
INF = 10**10
dist = [INF]*n
dist[0] = 0
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c

negative = [0]*n
for i in range(10**5):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c
        negative[v2] = 1
    end = time.time() - start
    if 1800 < end*10**3:
        break

if negative[-1]:
    print(-1)
else:
    print(max(0, -dist[-1]))

Submission status: Wrong Answer

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

INF = 10**10
dist = [INF]*n
dist[0] = 0
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c

negative = [0]*n
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c
        negative[v2] = 1


if negative[-1]:
    print(-1)
else:
    print(max(0, -dist[-1]))

Submission status: Wrong Answer

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

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

  #辺の緩和
  while stack:
    u = stack.pop()
    for edge in edges[u]:
      if flag[edge[0]] == 0:
        flag[edge[0]] = 1
      stack.append(edge[0])
      c += 1
      if u != inf and dist[edge[0]] < dist[u] + edge[1]:
        dist[edge[0]] = dist[u] + edge[1]
      if c == 10**6:
        if dist[-1] <= 0:
          return [0]
        else:
          return [-1]

  return dist


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

#辺の定義
#辺E(u,v)の重みがw
for _ in range(M):
  a,b,c = map(int,input().split())
  a -= 1
  b -= 1
  E[a].append((b,c-P))

flag = [0 for _ in range(N)]

ans = BellmanFord(E,N,0)
print(ans[-1])

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
abcs = [list(map(int, input().split())) for _ in range(m)]
distance = [-1000000 * n] * (n + 1)

distance[1] = 0
for _ in range(n + 1):
    for a, b, c in abcs:
        if distance[b] < distance[a] + c - p:
            distance[b] = distance[a] + c - p

ret = distance[n]
for _ in range(n + 1):
    for a, b, c in abcs:
        if distance[b] < distance[a] + c - p:
            distance[b] = distance[a] + c - p

print(-1 if ret != distance[n] else max(ret, 0))

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
abcs = [list(map(int, input().split())) for _ in range(m)]
distance = [-100000 * n] * (n + 1)

distance[1] = 0
for _ in range(n + 1):
    for a, b, c in abcs:
        if distance[b] < distance[a] + c - p:
            distance[b] = distance[a] + c - p

ret = distance[n]
for _ in range(n + 1):
    for a, b, c in abcs:
        if distance[b] < distance[a] + c - p:
            distance[b] = distance[a] + c - p

print(-1 if ret != distance[n] else max(ret, 0))

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
abcs = [list(map(int, input().split())) for _ in range(m)]
distance = [-100000 * n] * (n + 1)
predecessor = [-1] * (n + 1)

distance[1] = 0
for _ in range(n):
    for a, b, c in abcs:
        if distance[b] < distance[a] + c - p:
            distance[b] = distance[a] + c - p
            predecessor[b] = a

ret = distance[n]
for _ in range(n):
    for a, b, c in abcs:
        if distance[b] < distance[a] + c - p:
            distance[b] = distance[a] + c - p
            predecessor[b] = a

print(-1 if ret != distance[n] else max(ret, 0))

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
abcs = [list(map(int, input().split())) for _ in range(m)]
distance = [-100000 * n] * (n + 1)
predecessor = [-1] * (n + 1)

distance[1] = 0
for _ in range(n + 1):
    for a, b, c in abcs:
        if distance[b] < distance[a] + c - p:
            distance[b] = distance[a] + c - p
            predecessor[b] = a

v = n
vs = set()
while v > 0 and v not in vs:
    vs.add(v)
    v = predecessor[v]

no_max = False
for a, b, c in abcs:
    if b in vs:
        if distance[b] < distance[a] + c - p:
            no_max = True

print(-1 if no_max else max(distance[n], 0))

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
abcs = [list(map(int, input().split())) for _ in range(m)]
distance = [-100000 * n] * (n + 1)
distance[1] = 0

for _ in range(n + 1):
    for a, b, c in abcs:
        if distance[b] < distance[a] + c - p:
            distance[b] = distance[a] + c - p

no_max = False
for a, b, c in abcs:
    if b == n:
        if distance[b] < distance[a] + c - p:
            no_max = True

print(-1 if no_max else max(distance[n], 0))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,m,p = map(int,input().split())
abd = [list(map(int,input().split())) for i in range(m)]
come = [[] for i in range(n+1)]
for a,b,d in abd:
  come[b].append(a)
visitable = [0]*(n+1)
stack = [n]
visitable[n] = 1
while stack:
  x = stack.pop()
  for y in come[x]:
    if visitable[y] == 0:
      visitable[y] = 1
      stack.append(y)
score = [10**15 for i in range(n+1)]
score[1] = 0
for j in range(n):
  flg = 0
  for i in range(m):
    v1,v2,e = abd[i]
    if visitable[v1] & visitable[v2] == 0:
      continue 
    e = -e+p
    if score[v1] != 10**15 and score[v2] > score[v1]+e:
      score[v2] = score[v1]+e
      flg = 1
      if j == n-2:
        last = -score[n]
      if j == n-1:
        onemore = -score[n]
  if flg == 0:
    break
if flg == 0:
  print(max(0,-score[n]))
else:
  if last == onemore:
    print(max(last,0))
  else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,m,p = map(int,input().split())
abd = [list(map(int,input().split())) for i in range(m)]
come = [[] for i in range(n+1)]
for a,b,d in abd:
  come[b].append(a)
visitable = [0]*(n+1)
stack = [n]
visitable[n] = 1
while stack:
  x = stack.pop()
  for y in come[x]:
    if visitable[y] == 0:
      visitable[y] = 1
      stack.append(y)
score = [10**15 for i in range(n+1)]
score[1] = 0
for j in range(n+1):
  flg = 0
  for i in range(m):
    v1,v2,e = abd[i]
    if visitable[v1] & visitable[v2] == 0:
      continue 
    e = -e+p
    if score[v1] != 10**15 and score[v2] > score[v1]+e:
      score[v2] = score[v1]+e
      flg = 1
      if j == n-1:
        last = -score[n]
      if j == n:
        onemore = -score[n]
  if flg == 0:
    break
if flg == 0:
  print(max(0,-score[n]))
else:
  if last == onemore:
    print(max(last,0))
  else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
from copy import deepcopy
sys.setrecursionlimit(10**7)
n,m,p = map(int,input().split())
E = [[] for i in range(n)]
for i in range(m):
    a,b,c = map(int,input().split())
    c -= p
    E[a-1].append((b-1,c))

visit = [0]*n

score = [-10**8]*n
q = deque([])
visit[0] = 1
q.append((0,-1,visit))

score[0] = 0
while q:
    now,bef,past = q.popleft()

    for nex,nex_p in E[now]:
        if past[nex] == -1:
            continue
        elif past[nex] == 1:
            if nex_p+score[now] > score[nex]:
                score[nex] = float("INF")
                p = deepcopy(past)
                p[nex] = -1
                q.append((nex,now,p))
                break
        else:
            if nex_p+score[now] > score[nex]:
                score[nex] = score[now]+nex_p
                p = deepcopy(past)
                p[nex] += 1
                q.append((nex,now,p))
if score[-1] == float("INF"):
    print(-1)
else:
    print(max(0,score[-1]))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
from copy import deepcopy
sys.setrecursionlimit(10**7)
n,m,p = map(int,input().split())
E = [[] for i in range(n)]
for i in range(m):
    a,b,c = map(int,input().split())
    c -= p
    E[a-1].append((b-1,c))

visit = [0]*n

score = [-10**8]*n
q = deque([])
visit[0] = 1
q.append((0,-1,visit))

score[0] = 0
while q:
    now,bef,past = q.popleft()

    for nex,nex_p in E[now]:
        if past[nex] >= 3:
            if nex_p+score[now] > score[nex]:
                score[nex] = float("INF")
                break
        else:
            if nex_p+score[now] > score[nex]:
                score[nex] = score[now]+nex_p
                p = deepcopy(past)
                p[nex] += 1
                q.append((nex,now,p))
if score[-1] == float("INF"):
    print(-1)
else:
    print(max(0,score[-1]))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
sys.setrecursionlimit(10**7)
n,m,p = map(int,input().split())
E = [[] for i in range(n)]
for i in range(m):
    a,b,c = map(int,input().split())
    c -= p
    E[a-1].append((b-1,c))

visit = [0]*n

score = [-10**8]*n
q = deque([])
visit[0] = 1
q.append((0,-1,visit))

score[0] = 0
while q:
    now,bef,past = q.popleft()

    for nex,nex_p in E[now]:
        if past[nex] == 1:
            if nex_p+score[now] > score[nex]:
                score[nex] = float("INF")
                break
        else:
            if nex_p+score[now] > score[nex]:
                score[nex] = score[now]+nex_p
                p = past
                p[nex] = 1
                q.append((nex,now,p))
if score[-1] == float("INF"):
    print(-1)
else:
    print(max(0,score[-1]))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,p = map(int,input().split())
E = [[] for i in range(n)]
for i in range(m):
    a,b,c = map(int,input().split())
    c -= p
    E[a-1].append((b-1,c))

visit = [0]*n

score = [-10**8]*n
q = deque([])
q.append((0,-1))
visit[0] = 1
score[0] = 0
while q:
    now,bef = q.popleft()

    for nex,nex_p in E[now]:
        if nex == bef and nex_p+score[now] > score[nex]:
            score[nex] = float("INF")
            break
        if nex_p+score[now] > score[nex]:
            score[nex] = score[now]+nex_p
            visit[nex] += 1
            q.append((nex,now))
if score[-1] == float("INF"):
    print(-1)
else:
    print(max(0,score[-1]))

Submission status: Wrong Answer

Solution code:
edges = []
N, M, P = map(int, input().split())
for i in range(M):
    a,b,c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a,b,-c+P))
    
# BellmanFord    
INF = 10 ** 18
def bellmanFord(n, edges, s, g):
    """
    input
        n : 頂点の数
        edges : (始点, 終点, コスト)のリスト
        s : 探索の始点
        g : 探索の終点
    output
        sから各点への最短距離
    """    
    d = [INF] * n
    d[s] = 0
    cnt = 0
    while True:
        update = False
        for a, b, c in edges:
            if d[a] != INF and d[b] > d[a] + c:
                d[b] = d[a] + c
                update = True
        if not update:
            break
        cnt += 1
        # 負閉路の存在をチェック
        if cnt == 3*n:
            tmp = d[g]
        # 負閉路の影響を受けるかチェック
        if cnt == 6*n:
            if tmp == d[g]:
                return d[g]
            else:
                print(-1)
                exit()
        # print(cnt,d)
    return d[g]

ans = bellmanFord(N,edges,0,N-1)
print(max(0,-ans))

Submission status: Wrong Answer

Solution code:
edges = []
N, M, P = map(int, input().split())
for i in range(M):
    a,b,c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a,b,-c+P))
    
# BellmanFord    
INF = 10 ** 18
def bellmanFord(n, edges, s, g):
    """
    input
        n : 頂点の数
        edges : (始点, 終点, コスト)のリスト
        s : 探索の始点
        g : 探索の終点
    output
        sから各点への最短距離
    """    
    d = [INF] * n
    d[s] = 0
    cnt = 0
    while True:
        update = False
        for a, b, c in edges:
            if d[a] != INF and d[b] > d[a] + c:
                d[b] = d[a] + c
                update = True
        if not update:
            break
        cnt += 1
        # 負閉路の存在をチェック
        if cnt == n:
            tmp = d[g]
        # 負閉路の影響を受けるかチェック
        if cnt == 3*n:
            if tmp == d[g]:
                return d[g]
            else:
                print(-1)
                exit()
        # print(cnt,d)
    return d[g]

ans = bellmanFord(N,edges,0,N-1)
print(max(0,-ans))

Submission status: Wrong Answer

Solution code:
edges = []
N, M, P = map(int, input().split())
for i in range(M):
    a,b,c = map(int, input().split())
    a -= 1
    b -= 1
    if a == b:
        edges.append((a,b,-c)) # 自己ループ
    else:
        edges.append((a,b,-c+P))
    
# BellmanFord    
INF = 10 ** 18
def bellmanFord(n, edges, s, g):
    """
    input
        n : 頂点の数
        edges : (始点, 終点, コスト)のリスト
        s : 探索の始点
        g : 探索の終点
    output
        sから各点への最短距離
    """    
    d = [INF] * n
    d[s] = 0
    cnt = 0
    while True:
        update = False
        for a, b, c in edges:
            if d[a] != INF and d[b] > d[a] + c:
                d[b] = d[a] + c
                update = True
        if not update:
            break
        cnt += 1
        # 負閉路の存在をチェック
        if cnt == 2*n:
            tmp = d[g]
        # 負閉路の影響を受けるかチェック
        if cnt == 4*n:
            if tmp == d[g]:
                return d[g]
            else:
                print(-1)
                exit()
        # print(cnt,d)
    return d[g]

ans = bellmanFord(N,edges,0,N-1)
print(max(0,-ans))

Submission status: Wrong Answer

Solution code:
edges = []
N, M, P = map(int, input().split())
for i in range(M):
    a,b,c = map(int, input().split())
    a -= 1
    b -= 1
    if a == b:
        edges.append((a,b,-c)) # 自己ループ
    else:
        edges.append((a,b,-c+P))
    
# ベルマンフォード

    
    
INF = 10 ** 18
def bellmanFord(n, edges, s, g):
    """
    input
        n : 頂点の数
        edges : (始点, 終点, コスト)のリスト
        s : 探索の始点
        g : 探索の終点
    output
        sから各点への最短距離
    """    
    d = [INF] * n
    d[s] = 0
    cnt = 0
    while True:
        update = False
        for a, b, c in edges:
            if d[a] != INF and d[b] > d[a] + c:
                d[b] = d[a] + c
                update = True
        if not update:
            break
        cnt += 1
        # 負閉路の存在をチェック
        if cnt == n:
            tmp = d[:]
        # 負閉路の影響を受けるかチェック
        if cnt == 3*n:
            if tmp[g] == d[g]:
                return d[g]
            else:
                print(-1)
                exit()
    return d[g]

ans = bellmanFord(N,edges,0,N-1)
print(max(0,-ans))

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

def Bellman_Ford(s,n,w,es):
    #s→iの最短距離
    # s:始点, n:頂点数, w:辺の数, es[i]: [辺の始点,辺の終点,辺のコスト]
    d = [Max] * n
    #d[i] : s→iの最短距離
    d[s] = 0
    po = 0
    while po < 2*N + 2*M:
        update = False
        for p,q,r in es:
        # e: 辺iについて [from,to,cost]
            if d[p] != Max and d[q] > d[p] + r:
                d[q] = d[p] + r
                update = True
                if (q == n-1 and po > N):
                    d[-1] = None
                    update = False
                    break
        if not update:
            break
        po += 1
    return d

N,M,P = m()

es = []
for _ in range(M):

    x,y,z = map(int,input().split())

    x -= 1
    y -= 1
    es.append([x,y,-z+P])



po = Bellman_Ford(0,N,M,es)
print(-1 if po[-1] == None else -po[-1] if po[-1] <= 0 else 0)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

def Bellman_Ford(s,n,w,es):
    #s→iの最短距離
    # s:始点, n:頂点数, w:辺の数, es[i]: [辺の始点,辺の終点,辺のコスト]
    d = [Max] * n
    #d[i] : s→iの最短距離
    d[s] = 0
    po = 0
    while po < 2*N + M:
        update = False
        for p,q,r in es:
        # e: 辺iについて [from,to,cost]
            if d[p] != Max and d[q] > d[p] + r:
                d[q] = d[p] + r
                update = True
                if (q == n-1 and po > N):
                    d[-1] = None
                    update = False
                    break
        if not update:
            break
        po += 1
    return d

N,M,P = m()

es = []
for _ in range(M):

    x,y,z = map(int,input().split())

    x -= 1
    y -= 1
    es.append([x,y,-z+P])



po = Bellman_Ford(0,N,M,es)
print(-1 if po[-1] == None else -po[-1] if po[-1] <= 0 else 0)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

def Bellman_Ford(s,n,w,es):
    #s→iの最短距離
    # s:始点, n:頂点数, w:辺の数, es[i]: [辺の始点,辺の終点,辺のコスト]
    d = [Max] * n
    #d[i] : s→iの最短距離
    d[s] = 0
    po = 0
    while po < 2*N + 100:
        update = False
        for p,q,r in es:
        # e: 辺iについて [from,to,cost]
            if d[p] != Max and d[q] > d[p] + r:
                d[q] = d[p] + r
                update = True
                if (q == n-1 and po > N):
                    d[-1] = None
                    update = False
                    break
        if not update:
            break
        po += 1
    return d

N,M,P = m()

es = []
for _ in range(M):

    x,y,z = map(int,input().split())

    x -= 1
    y -= 1
    es.append([x,y,-z+P])



po = Bellman_Ford(0,N,M,es)
print(-1 if po[-1] == None else -po[-1] if po[-1] <= 0 else 0)