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 BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[inf for i in range(num_v)]
    dist[source-1]=0
    flag = False

    #辺の緩和
    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:
                if edge[1] == num_v:
                    return -1
                else:
                    flag = True
    if flag:
        for i in range(num_v):
            for edge in edges:
                if dist[edge[1]-1]>dist[edge[0]-1] + edge[2]:
                    dist[edge[1]-1] = dist[edge[0]-1] + edge[2]
                    if edge[1] == num_v :
                        return -1

    return dist

N,M,P = [int(x) for x in input().split()]
gp = []
for _ in range(M):
    A,B,C = [int(x) for x in input().split()]
    gp.append([A,B,P-C])

li = BellmanFord(gp,N,1)
if(li==-1):
    print(-1)
else:
    print(max(0,-li[N-1]))

Submission status: Wrong Answer

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

edge_dic = {}

for _ in range(M):
    A,B,C = map(int,input().split())
    if not A - 1 in edge_dic:
        edge_dic[A-1] = [[B-1,C-P]]
    else:
        edge_dic[A-1].append([B-1,C-P])

# print(edge_dic)

INF = 10 ** 10

ans_array = [-INF] * N
can_visit = [0] * N
ans_array[0] = 0
can_visit[0] = 1 
last_ans_change = -1

for i in range(N*2):
    change_flag = 0
    for key, value in edge_dic.items():
        if can_visit[key] == 0:
            continue
        for v in value:
            goal, cost = v
            if ans_array[goal] < ans_array[key] + cost:
                ans_array[goal] = ans_array[key] + cost
                can_visit[goal] = 1
                change_flag = 1
                if goal == N - 1:
                    last_ans_change = i
    if change_flag == 0:
        break

if last_ans_change >= N:
    ans = -1
else:
    ans = max(0,ans_array[N-1])

print(ans)

Submission status: Wrong Answer

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

  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if 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[1]==num_v : return -1
  return dist

N,M,P = [int(x) for x in input().split()]
gp = []
for _ in range(M):
    A,B,C = [int(x) for x in input().split()]
    gp.append([A,B,P-C])

li = BellmanFord(gp,N,1)
if(li==-1):
    print(-1)
else:
    print(max(0,-li[N-1]))

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
ABC = []
for _ in range(M):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
import sys
INT_MAX = sys.maxsize # 9223372036854775807        
    
def bellman_ford(edges,num_v,source):

    dist=[INT_MAX] * num_v
    dist[source]=0

    #辺の緩和
    has_negative = False
    for i in range(num_v):
        for edge in edges:
            if edge[0] != INT_MAX and dist[edge[1]] > dist[edge[0]] + edge[2]:
                if dist[edge[0]] != INT_MAX:
                    dist[edge[1]] = dist[edge[0]] + edge[2]
                if i==num_v-1:
                    has_negative = True
    
    if has_negative == False:
        return dist
    
    negative = [False] * num_v
    for i in range(num_v):
        for from_edge, to_edge, cost in edges:
            #if dist[from_edge] == INT_MAX:
            #    continue
            #if dist[to_edge] > dist[from_edge] + cost:
            #    dist[to_edge] = dist[from_edge] + cost
            #    negative[to_edge] = True
            #
            #if negative[from_edge] == INT_MAX:
            #    negative[to_edge] = True
            if dist[to_edge] <= dist[from_edge] + cost:
                continue
            dist[from_edge] = -1 * INT_MAX
                
    #for i, is_negative in enumerate(negative):
    #    if is_negative:
    #        dist[i] = -1 * INT_MAX
        
    return dist

edges = ABC[:]
edges = [(a, b, -c+P) for a, b, c in edges]

shortest_path = bellman_ford(edges, N+1, 1)
rev_shortest_path = bellman_ford(edges, N+1, N)
ans = -1 * shortest_path[N]
rev = -1 * rev_shortest_path[1]
#print(shortest_path)
if ans == -INT_MAX:
    print(-1)
elif ans < 0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
ABC = []
for _ in range(M):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
    
import sys
INT_MAX = sys.maxsize # 9223372036854775807        
    
def bellman_ford(edges,num_v,source):

    dist=[INT_MAX] * num_v
    dist[source]=0

    #辺の緩和
    has_negative = False
    for i in range(num_v):
        for edge in edges:
            if edge[0] != INT_MAX and dist[edge[1]] > dist[edge[0]] + edge[2]:
                if dist[edge[0]] != INT_MAX:
                    dist[edge[1]] = dist[edge[0]] + edge[2]
                if i==num_v-1:
                    has_negative = True
    
    if has_negative == False:
        return dist
    
    negative = [False] * num_v
    for i in range(num_v):
        for from_edge, to_edge, cost in edges:
            #if dist[from_edge] == INT_MAX:
            #    continue
            #if dist[to_edge] > dist[from_edge] + cost:
            #    dist[to_edge] = dist[from_edge] + cost
            #    negative[to_edge] = True
            #
            #if negative[from_edge] == INT_MAX:
            #    negative[to_edge] = True
            if dist[to_edge] <= dist[from_edge] + cost:
                continue
            dist[from_edge] = -1 * INT_MAX
                
    #for i, is_negative in enumerate(negative):
    #    if is_negative:
    #        dist[i] = -1 * INT_MAX
        
    return dist

edges = ABC[:]
edges = [(a, b, -c+P) for a, b, c in edges]

shortest_path = bellman_ford(edges, N+1, 1)
rev_shortest_path = bellman_ford(edges, N+1, N)
ans = -1 * shortest_path[N]
rev = -1 * rev_shortest_path[1]
print(shortest_path)
if ans == -INT_MAX:
    print(-1)
elif ans < 0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

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

edge_dic = {}

for _ in range(M):
    A,B,C = map(int,input().split())
    if not A - 1 in edge_dic:
        edge_dic[A-1] = [[B-1,C-P]]
    else:
        edge_dic[A-1].append([B-1,C-P])

# print(edge_dic)

INF = 10 ** 10

ans_array = [-INF] * N

ans_array[0] = 0
last_ans_change = -1

for i in range(N*3):
    change_flag = 0
    for key, value in edge_dic.items():
        for v in value:
            goal, cost = v
            if ans_array[goal] < ans_array[key] + cost:
                ans_array[goal] = ans_array[key] + cost
                change_flag = 1
                if goal == N - 1:
                    last_ans_change = i
    if change_flag == 0:
        break

if last_ans_change >= N * 2:
    ans = -1
else:
    ans = max(0,ans_array[N-1])

print(ans)

Submission status: Wrong Answer

Solution code:
words = lambda t : list(map(t, input().split()))
n,m,p = words(int)

def bellman_ford(edges, numv, stt, target):
    inf = float("inf")
    dist = [inf for i in range(numv)]
    dist[stt - 1] = 0

    for i in range(numv):
        for (f,t,c) in edges:
            if dist[t-1] > dist[f-1] + c:
                dist[t-1] = dist[f-1] + c
                if i==numv-1 and t == target:
                    return -1
    return dist


edges = []
for i in range(m):
    a,b,c = words(int)
    edges.append((a,b,-c+p))

ans = bellman_ford(edges, n, 1, n)
if ans == -1:
    print(-1)
else:
    print(max(0,-ans[n-1]))

Submission status: Wrong Answer

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

edge_dic = {}

for _ in range(M):
    A,B,C = map(int,input().split())
    if not A - 1 in edge_dic:
        edge_dic[A-1] = [[B-1,C-P]]
    else:
        edge_dic[A-1].append([B-1,C-P])

# print(edge_dic)

INF = 10 ** 10

ans_array = [-INF] * N

ans_array[0] = 0
last_ans_change = -1

for i in range(N*2):
    change_flag = 0
    for key, value in edge_dic.items():
        for v in value:
            goal, cost = v
            if ans_array[goal] < ans_array[key] + cost:
                ans_array[goal] = ans_array[key] + cost
                change_flag = 1
                if goal == N - 1:
                    last_ans_change = i
    if change_flag == 0:
        break

if last_ans_change >= N:
    ans = -1
else:
    ans = max(0,ans_array[N-1])

print(ans)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
ABC = []
for _ in range(M):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
    
import sys
INT_MAX = sys.maxsize # 9223372036854775807        
    
def bellman_ford(edges,num_v,source):

    dist=[INT_MAX] * num_v
    dist[source]=0

    #辺の緩和
    has_negative = False
    for i in range(num_v):
        for edge in edges:
            if edge[0] != INT_MAX and dist[edge[1]] > dist[edge[0]] + edge[2]:
                if dist[edge[0]] != INT_MAX:
                    dist[edge[1]] = dist[edge[0]] + edge[2]
                if i==num_v-1:
                    has_negative = True
    
    if has_negative == False:
        return dist
    
    negative = [False] * num_v
    for i in range(num_v):
        for from_edge, to_edge, cost in edges:
            if dist[to_edge] <= dist[from_edge] + cost:
                continue
            dist[to_edge] = -INT_MAX

    return dist

edges = ABC[:]
edges = [(a, b, -c+P) for a, b, c in edges]

shortest_path = bellman_ford(edges, N+1, 1)
rev_shortest_path = bellman_ford(edges, N+1, N)
ans = -1 * shortest_path[N]
rev = -1 * rev_shortest_path[1]
#print(shortest_path)
if ans == INT_MAX:
    print(-1)
elif ans < 0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
ABC = []
for _ in range(M):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
    
import sys
INT_MAX = sys.maxsize # 9223372036854775807        
    
def bellman_ford(edges,num_v,source):

    dist=[INT_MAX] * num_v
    dist[source]=0

    #辺の緩和
    has_negative = False
    for i in range(num_v):
        for edge in edges:
            if edge[0] != INT_MAX and dist[edge[1]] > dist[edge[0]] + edge[2]:
                if dist[edge[0]] != INT_MAX:
                    dist[edge[1]] = dist[edge[0]] + edge[2]
                if i==num_v-1:
                    has_negative = True
    
    if has_negative == False:
        return dist
    
    negative = [False] * num_v
    for i in range(num_v):
        for from_edge, to_edge, cost in edges:
            if dist[to_edge] <= dist[from_edge] + cost:
                continue
            dist[to_edge] = -INT_MAX

    return dist

edges = ABC[:]
edges = [(a, b, -c+P) for a, b, c in edges]

shortest_path = bellman_ford(edges, N+1, 1)
rev_shortest_path = bellman_ford(edges, N+1, N)
ans = -1 * shortest_path[N]
rev = -1 * rev_shortest_path[1]
#print(shortest_path)
if ans == INT_MAX or ans == -INT_MAX:
    print(-1)
elif ans < 0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:

# グラフ定義
class Edge:
    def __init__(self, f: int, t: int, cost: int):
        self._t = t
        self._cost = cost
        self._f = f

    @property
    def t(self):
        return self._t

    @property
    def f(self):
        return self._f

    @property
    def cost(self):
        return self._cost


INF = 1 << 28


# ここから本実装
def bellman_ford(n: int, edges, s: int, g: int):
    m = len(edges)
    dist = [INF for _ in range(n)]
    looped = [False] * n

    dist[s] = 0

    for i in range(2 * n):
        for e in edges:

            if dist[e.t] > dist[e.f] + e.cost:
                dist[e.t] = dist[e.f] + e.cost
                if i >= n - 1:
                    # negative roop
                    looped[e.t] = True

    return dist[g], looped[g]

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(Edge(a, b, -c + p))

c, looped = bellman_ford(n, edges, 0, n - 1)

if looped:
    print(-1)
else:
    print(max(0, -c))

Submission status: Wrong Answer

Solution code:
from collections import namedtuple
#Edge v1,v2,cost
Edge = namedtuple('Edge','v1 v2 score') 
N,M,P = map(int,input().split())
Edges =[]
for i in range(M):
    v1,v2,score = map(int,input().split())
    #インデックスを合わせるために-1,最大化のために-(score-P)
    Edges.append(Edge(v1-1,v2-1,P-score))

def bellman_ford(N,edges):
    #各点の重み
    inf = float('inf')
    dist = [inf for i in range(N)]
    dist[0] = 0
    for i in range(N):
        for e in edges:
            if dist[e.v1] != inf and dist[e.v2] > dist[e.v1]+e.score:
                dist[e.v2] = dist[e.v1] + e.score
                if i==N-1 and e.v2 == N-1:
                    return None
    return -dist[N-1]

ans = bellman_ford(N,Edges)
if ans is None:
	print("-1")
elif ans < 0:
	print("0")
else:
	print(ans)

Submission status: Wrong Answer

Solution code:

# グラフ定義
class Edge:
    def __init__(self, f: int, t: int, cost: int):
        self._t = t
        self._cost = cost
        self._f = f

    @property
    def t(self):
        return self._t

    @property
    def f(self):
        return self._f

    @property
    def cost(self):
        return self._cost


INF = 1 << 28


# ここから本実装
def bellman_ford(n: int, edges, s: int, g: int):
    m = len(edges)
    dist = [INF for _ in range(n)]
    looped = [False] * n

    dist[s] = 0

    for i in range(2 * n):
        for e in edges:

            if dist[e.t] > dist[e.f] + e.cost:
                dist[e.t] = dist[e.f] + e.cost
                if i == n - 1:
                    # negative roop
                    looped[e.t] = True

    return dist[g], looped[g]

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(Edge(a, b, -c + p))

c, looped = bellman_ford(n, edges, 0, n - 1)

if looped:
    print(-1)
else:
    print(max(0, -c))

Submission status: Wrong Answer

Solution code:
from collections import namedtuple
#Edge v1,v2,cost
Edge = namedtuple('Edge','v1 v2 score') 
N,M,P = map(int,input().split())
Edges =[]
for i in range(M):
    v1,v2,score = map(int,input().split())
    #インデックスを合わせるために-1,最大化のために-(score-P)
    Edges.append(Edge(v1-1,v2-1,P-score))

def bellman_ford(N,edges):
    #各点の重み
    inf = float('inf')
    dist = [inf for i in range(N)]
    dist[0] = 0
    for i in range(N):
        for e in edges:
            if dist[e.v1] != inf and dist[e.v2] > dist[e.v1]+e.score:
                dist[e.v2] = dist[e.v1] + e.score
                if i==N-1 and e.v2 == N-1:
                    return None
    return -dist[N-1]

Submission status: Wrong Answer

Solution code:

# グラフ定義
class Edge:
    def __init__(self, f: int, t: int, cost: int):
        self._t = t
        self._cost = cost
        self._f = f

    @property
    def t(self):
        return self._t

    @property
    def f(self):
        return self._f

    @property
    def cost(self):
        return self._cost


INF = 1 << 28


# ここから本実装
def bellman_ford(n: int, edges, s: int, g: int):
    m = len(edges)
    dist = [INF for _ in range(n)]
    looped = [False] * n

    dist[s] = 0

    for i in range(n):
        for e in edges:

            if dist[e.t] > dist[e.f] + e.cost:
                dist[e.t] = dist[e.f] + e.cost
                if i == n - 1:
                    # negative roop
                    looped[e.t] = True

    return dist[g], looped[g]

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(Edge(a, b, -c + p))

c, looped = bellman_ford(n, edges, 0, n - 1)

if looped:
    print(-1)
else:
    print(max(0, -c))

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

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

#ベルマンフォード法
#e_listの用意
vi=0
min_dis_list = [10**27]*N
min_dis_list[vi] = 0

for i in range(N-1):
    for e in e_list:
        u,v,d = e
        if min_dis_list[v]>min_dis_list[u]+d:
            min_dis_list[v]=min_dis_list[u]+d

neg_loop_flag=[False]*N

for i in range(N):
    for e in e_list:
        u,v,d = e
        if min_dis_list[u] < 10**27:
            if min_dis_list[u] + d < min_dis_list[v]:
                neg_loop_flag[v]=True
                min_dis_list[v] = min_dis_list[u] + d
            if neg_loop_flag[u]:
                neg_loop_flag[v]=True

if neg_loop_flag[0]:
    print(-1)
else:
    print(max(-min_dis_list[N-1],0))

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

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

#ベルマンフォード法
#e_listの用意
vi=0
min_dis_list = [10**27]*N
min_dis_list[vi] = 0

for i in range(N-1):
    for e in e_list:
        u,v,d = e
        if min_dis_list[v]>min_dis_list[u]+d:
            min_dis_list[v]=min_dis_list[u]+d

neg_loop_flag=[False]*N

for i in range(N):
    for e in e_list:
        u,v,d = e
        if min_dis_list[u] < 10**27:
            if min_dis_list[u] + d < min_dis_list[v]:
                neg_loop_flag[v]=True
                min_dis_list[v] = min_dis_list[u] + d
            if neg_loop_flag[u]:
                neg_loop_flag[v]=True

if neg_loop_flag[0]:
    print(-1)
else:
    print(-min_dis_list[N-1])

Submission status: Wrong Answer

Solution code:
##sppedtest##
import sys
input=sys.stdin.readline
n,m,p=map(int,input().split())
graph=[]
gappend=graph.append
for _ in range(m):
    a,b,c=map(int,input().split())
    gappend([a-1,b-1,p-c])
points=[float("inf")]*n
points[0]=0
for _ in range(n-1):
    for i in range(m):
        if points[graph[i][1]]>points[graph[i][0]]+graph[i][2]:
            points[graph[i][1]]=points[graph[i][0]]+graph[i][2]
score=-points[n-1]
for _ in range(m):
    for i in range(m):
        if points[graph[i][1]]>points[graph[i][0]]+graph[i][2]:
            points[graph[i][1]]=points[graph[i][0]]+graph[i][2]
newscore=-points[n-1]
if score<newscore:
    print(-1)
else:
    print(max(0,score))

Submission status: Wrong Answer

Solution code:
n, m, p = [ int(v) for v in input().split() ]
node_list = []
for i in range(m):
    a, b, c = [ int(v) for v in input().split() ]
    node_list.append((a-1,b-1,p-c))
distance_list = [ 10**15 for i in range(n) ]
parent_list = [ -1 for i in range(n) ]

distance_list[0] = 0
parent_list[0] = -1



for i in range(n-1):
    new_distanece_list = distance_list
    for j in node_list:
        a, b, c = j
        if distance_list[a] != 10**15:
            if new_distanece_list[b] > distance_list[a] + c:
                new_distanece_list[b] = distance_list[a] + c
                parent_list[b] = a
                change_log = 1
    distance_list = new_distanece_list
distance_list_1 = [ i for i in distance_list]

for i in range(n):
    new_distanece_list = distance_list
    for j in node_list:
        a, b, c = j
        if distance_list[a] != 10**15:
            if new_distanece_list[b] > distance_list[a] + c:
                new_distanece_list[b] = distance_list[a] + c
                parent_list[b] = a
                change_log = 1
    distance_list = new_distanece_list
distance_list_2 = distance_list



if distance_list_1[-1] == distance_list_2[-1]:
    print(-min(0,distance_list_1[-1]))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n, m, p = [ int(v) for v in input().split() ]
node_list = []
for i in range(m):
    a, b, c = [ int(v) for v in input().split() ]
    node_list.append((a-1,b-1,p-c))
distance_list = [ 10**15 for i in range(n) ]
parent_list = [ -1 for i in range(n) ]

distance_list[0] = 0
parent_list[0] = -1



for i in range(n-1):
    new_distanece_list = distance_list
    for j in node_list:
        a, b, c = j
        if distance_list[a] != 10**15:
            if new_distanece_list[b] > distance_list[a] + c:
                new_distanece_list[b] = distance_list[a] + c
                parent_list[b] = a
                change_log = 1
    distance_list = new_distanece_list
distance_list_1 = [ i for i in distance_list]

for i in range(n):
    new_distanece_list = distance_list
    for j in node_list:
        a, b, c = j
        if distance_list[a] != 10**15:
            if new_distanece_list[b] > distance_list[a] + c:
                new_distanece_list[b] = distance_list[a] + c
                parent_list[b] = a
                change_log = 1
    distance_list = new_distanece_list
distance_list_2 = distance_list



if distance_list_1[-1] == distance_list_2[-1]:
    print(-distance_list_1[-1])
else:
    print(-1)

Submission status: Wrong Answer

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

N, M, P = [int(item) for item in input().split()]

from collections import defaultdict
next_b = defaultdict(lambda: [])

for i in range(M):
    a, b, c = [int(item) for item in input().split()]
    next_b[a].append([b, c - P])

max_sc = [0] + [int(-1e10)] * (N - 1)
prev_sc = [0] + [int(-1e10) - 1] * (N - 1)
route = [[0] * N] * N

for loop in range(N):
    # print(max_sc)
    if max_sc == prev_sc:
        print(max(max_sc[N - 1], 0))
        break
    prev_sc = copy.deepcopy(max_sc)
    for i in range(N):
        for d, scr in next_b[i + 1]:
            # print(route, d - 1, i)
            if route[d - 1][i] == 0:
                new_sc = prev_sc[i] + scr
                if new_sc > max_sc[d - 1]:
                    max_sc[d - 1] = new_sc
                    route[d - 1] = copy.deepcopy(route[i])
                    route[d - 1][i] = 1
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

def Bellman_Ford(g,start):
    n = len(g)    
    dist = [float("inf")]*n
    dist[start] = 0
    a0 = float("inf")
    
    for i in range(n<<1):
        updated = False
        for v in range(n):
            for to, cost in g[v]:
                if dist[to] <= dist[v] + cost: continue
                dist[to] = dist[v] + cost
                updated = True
        if not updated: break
        if i <= n: a0 = dist[-1]
    if a0 != dist[-1]: return None, False
    return dist, True


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

n,m,p = [int(i) for i in readline().split()]
g = [[] for _ in range(n)]
for i in range(m):
    a,b,c = [int(i) for i in readline().split()]
    g[a-1].append((b-1,-c+p))
#    g[b-1].append((a-1,c))

dist, TF = Bellman_Ford(g, 0)

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def BellmanFord(N, e, s, goal):
    dist = [sys.maxsize] * (N+1)
    dist[s] = 0
    for i in range(N+1):
        for u, v, c in e:
            if dist[v] > dist[u] + c:
                if i == N:
                    return 'inf'
                dist[v] = dist[u] + c
    return dist

@mt
def slv(N, M, P, ABC):
    g = [[] for _ in range(N+1)]
    for a, b, c in ABC:
        g[a].append(b)

    def r(s, e):
        q = deque()
        q.append(s)
        done = set()
        while q:
            u = q.popleft()
            for v in g[u]:
                if v == e:
                    return True
                if v not in done:
                    done.add(v)
                    q.append(v)
        return False

    d = set()
    for i in range(2, N):
        if not r(i, N):
            d.add(i)
    
    e = []
    for a, b, c in ABC:
        if a not in d and b not in d:
            e.append((a, b, -(c - P)))

    d = BellmanFord(N, e, 1, N)
    if d == 'inf':
        return -1
    return max(0, -d[N])


def main():
    N, M, P = read_int_n()
    ABC = [read_int_n() for _ in range(M)]
    print(slv(N, M, P, ABC))

    # N = 2500
    # M = 5000
    # P = random.randint(0, 10**5)
    # P= 0
    # ABC = [
    #     [random.randint(1, N), random.randint(1, N), random.randint(1, 10**5)] for _ in range(M)
    # ]
    # print(slv(N, M, P, ABC))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def BellmanFord(N, e, s, goal):
    dist = [sys.maxsize] * (N+1)
    dist[s] = 0
    for _ in range(N+1):
        for u, v, c in e:
            if dist[v] > dist[u] + c:
                dist[v] = dist[u] + c
    
    d = dist[goal]
    for _ in range(N+1):
        for u, v, c in e:
            if dist[v] > dist[u] + c:
                dist[v] = dist[u] + c
    return dist, d != dist[goal]

@mt
def slv(N, M, P, ABC):

    e = []
    for a, b, c in ABC:
        e.append((a, b, -(c - P)))

    d, loop = BellmanFord(N, e, 1, N)
    return max(0, -d[N]) if not loop else -1


def main():
    N, M, P = read_int_n()
    ABC = [read_int_n() for _ in range(M)]
    print(slv(N, M, P, ABC))

    # N = 2500
    # M = 5000
    # P = random.randint(0, 10**5)
    # P= 0
    # ABC = [
    #     [random.randint(1, N), random.randint(1, N), random.randint(1, 10**5)] for _ in range(M)
    # ]
    # print(slv(N, M, P, ABC))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, m, p = [ int(v) for v in input().split() ]
node_list = []
for i in range(m):
    a, b, c = [ int(v) for v in input().split() ]
    if a != b:
        node_list.append((a-1,b-1,p-c))
distance_list = [ 10**15 for i in range(n) ]
parent_list = [ -1 for i in range(n) ]

distance_list[0] = 0
parent_list[0] = -1


turn = 0
for i in range(n+1):
    turn += 1 
    change_log = 0
    new_distanece_list = distance_list
    for j in node_list:
        a, b, c = j
        if distance_list[a] != 10**15:
            if new_distanece_list[b] > distance_list[a] + c:
                new_distanece_list[b] = distance_list[a] + c
                parent_list[b] = a
                change_log = 1
    distance_list = new_distanece_list

    
def parent(a):
    for i in range(n-1):
        if a == parent_list[a] or a == -1:
            break
        else:
            b = parent_list[a]
            a = b
    return a


if parent(n-1) == -1:
    print(max(-distance_list[-1],0))
else:
    print(-1)