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:
from collections import deque
import sys

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

flag = [ [False] * N for i in range(N) ]
crash = [False] * N
cost = [float("-inf")] * N
cost[0] = 0

dic = {}
for i in range(N):
    dic[i] = []

    

for i in range(M):

    A,B,C = map(int,input().split())
    A -= 1
    B -= 1

    dic[A].append([B,C-P])

q = deque([0])

cnt = 0

while len(q) > 0:

    now = q.popleft()

    for bc in dic[now]:
    
        b = bc[0]
        c = bc[1]

        #print (now,b,flag[now],crash[now],flag[b],crash[b])

        if b == N-1 and crash[now]:
            print (-1)
            sys.exit()

        if cost[now] + c > cost[b]:


            if flag[now][b] and not crash[b]:

                crash[b] = True

                cost[b] = cost[now] + c

                for j in range(N):

                    flag[b][j] = flag[now][j]
                flag[b][b] = True
            
                q.append(b)

            elif not flag[now][b]:
                
                cost[b] = cost[now] + c
                crash[b] = crash[now]
                cost[b] = cost[now] + c
                crash[b] = crash[now]

                for j in range(N):

                    flag[b][j] = flag[now][j]
                    
                flag[b][b] = True

                q.append(b)

    cnt += 1

    if cnt > N * N:
        print (-1)
        sys.exit()

print (max(0,cost[N-1]))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

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

flag = [ [False] * N for i in range(N) ]
crash = [False] * N
cost = [float("-inf")] * N
cost[0] = 0

dic = {}
for i in range(N):
    dic[i] = []

    

for i in range(M):

    A,B,C = map(int,input().split())
    A -= 1
    B -= 1

    dic[A].append([B,C-P])

q = deque([0])

cnt = 0

while len(q) > 0:

    now = q.popleft()

    for bc in dic[now]:
    
        b = bc[0]
        c = bc[1]

        #print (now,b,flag[now],crash[now],flag[b],crash[b])

        if b == N-1 and crash[now]:
            print (-1)
            sys.exit()

        if cost[now] + c > cost[b]:


            if flag[now][b] and not crash[b]:

                crash[b] = True

                cost[b] = cost[now] + c

                flag[b] = flag[now].copy()
                flag[b][b] = True
            
                q.append(b)

            elif not flag[now][b]:
                
                cost[b] = cost[now] + c
                crash[b] = crash[now]
                cost[b] = cost[now] + c
                crash[b] = crash[now]

                flag[b] = flag[now].copy()
                flag[b][b] = True

                q.append(b)

    cnt += 1

    if cnt > 10 * N:
        print (-1)
        sys.exit()

print (max(0,cost[N-1]))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

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

flag = [ [False] * N for i in range(N) ]
crash = [False] * N
cost = [float("-inf")] * N
cost[0] = 0

dic = {}
for i in range(N):
    dic[i] = []

    

for i in range(M):

    A,B,C = map(int,input().split())
    A -= 1
    B -= 1

    dic[A].append([B,C-P])

q = deque([0])

cnt = 0

while len(q) > 0:

    now = q.popleft()

    for bc in dic[now]:
    
        b = bc[0]
        c = bc[1]

        #print (now,b,flag[now],crash[now],flag[b],crash[b])

        if b == N-1 and crash[now]:
            print (-1)
            sys.exit()

        if cost[now] + c > cost[b]:


            if flag[now][b] and not crash[b]:

                crash[b] = True

                cost[b] = cost[now] + c

                flag[b] = flag[now].copy()
                flag[b][b] = True
            
                q.append(b)

            elif not flag[now][b]:
                
                cost[b] = cost[now] + c
                crash[b] = crash[now]
                cost[b] = cost[now] + c
                crash[b] = crash[now]

                flag[b] = flag[now].copy()
                flag[b][b] = True

                q.append(b)

    cnt += 1

    if cnt > 10 * N:
        break

print (max(0,cost[N-1]))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

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

flag = [ [False] * N for i in range(N) ]
crash = [False] * N
cost = [float("-inf")] * N
cost[0] = 0

dic = {}
for i in range(N):
    dic[i] = []

    

for i in range(M):

    A,B,C = map(int,input().split())
    A -= 1
    B -= 1

    dic[A].append([B,C-P])

q = deque([0])

cnt = 0

while len(q) > 0:

    now = q.popleft()

    for bc in dic[now]:
    
        b = bc[0]
        c = bc[1]

        if b == N-1 and crash[b]:
            print (-1)
            sys.exit()

        if cost[now] + c > cost[b]:
            cost[b] = cost[now] + c
            crash[b] = crash[now]

            if flag[now][b]:

                crash[b] = True

            flag[b] = flag[now].copy()
            flag[b][b] = True
            
            q.append(b)

    cnt += 1

    if cnt > N ** 2:
        break

print (max(0,cost[N-1]))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
import copy

N,M,P=map(int, sys.stdin.readline().split())

al=[ [] for i in range(N+1) ]		#隣接リスト
rev_al=[ [] for i in range(N+1) ]	#reverse隣接リスト
#辺のリスト
edge=[]

for i in range(M):
	a,b,c=map(int, sys.stdin.readline().split())
	al[a].append(b)
	rev_al[b].append(a)
	edge.append((a,b,c-P))


#ゴールからBFS
q=deque()
q.append(N)
#訪問した頂点
Visit=[ 0 for _ in range(N+1) ]
while q:
	fro=q.popleft()
	Visit[fro]=1
	for to in rev_al[fro]:
		if Visit[to]==0:
			q.append(to)
			
Visit1=Visit

#スタートからBFS
q=deque()
q.append(1)
#訪問した頂点
Visit=[ 0 for _ in range(N+1) ]
while q:
	fro=q.popleft()
	Visit[fro]=1
	for to in al[fro]:
		if Visit[to]==0:
			q.append(to)

Visit2=Visit

#1からもNからも到達可能な頂点の集合
V=[]
for v1,v2 in zip(Visit1,Visit2):
	V.append(v1&v2)

new_N=sum(V)

D=[ float("-inf") for _ in range(N+1) ]	#頂点１からの距離 1-indexed
D[1]=0

fedge=[]	#必要な要素だけをフィルターした辺集合
for a,b,c in edge:
	if V[a]==1 and V[b]==1:
		fedge.append((a,b,c))

#ベルマンフォード
for cnt in range(new_N):
	for a,b,c in fedge:
		alt=D[a]+c
		if D[b]<alt:
			D[b]=alt
			if cnt==N-1:
				print -1
				quit()

print max(0,D[N])

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

N,M,P=map(int, sys.stdin.readline().split())

al=[ [] for _ in range(N+1) ]
Visit=[ float("-inf") for _ in range(N+1) ]
Visit[0]=None
Visit[1]=0

for _ in range(M):
	a,b,c=map(int, sys.stdin.readline().split())
	al[a].append((b,c-P))

q=deque()
q.append(1)

while q:
	fro=q.popleft()
	for to,kyori in al[fro]:
		alt=Visit[fro]+kyori
		if Visit[to]<alt and fro!=to:
			Visit[to]=alt
			q.append(to)

v1=Visit[N]


q=deque()
q.append(N)
cnt=0

while q and cnt<=M+1:
	fro=q.popleft()
	for to,kyori in al[fro]:
		cnt+=1
		alt=Visit[fro]+kyori
		if Visit[to]<alt:
			Visit[to]=alt
			q.append(to)

v2=Visit[N]

if v1==v2:
	print max(0,v1)
else:
	print -1

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

N,M,P=map(int, sys.stdin.readline().split())

al=[ [] for _ in range(N+1) ]
Visit=[ float("-inf") for _ in range(N+1) ]
Visit[0]=None
Visit[1]=0

for _ in range(M):
	a,b,c=map(int, sys.stdin.readline().split())
	al[a].append((b,c-P))

q=deque()
q.append(1)

cnt=0

while q and cnt<=M+1:
#for _ in range(M+1):
	fro=q.popleft()
	for to,kyori in al[fro]:
		cnt+=1
		alt=Visit[fro]+kyori
		if Visit[to]<alt:
			Visit[to]=alt
			q.append(to)

if M+1<=cnt:
	if Visit[N]>0:
		print -1
	else:
		print max(0,Visit[N])
else:
	print max(0,Visit[N])

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N,M,P = map(int,readline().split())
ABC = np.array(read().split(),np.int64)

A = ABC[::3]
B = ABC[1::3]
C = ABC[2::3]

C = P-C

# bellman fordでAからBへの最短距離を求める

INF = 10**18
dist = np.full(N+1,INF,np.int64)
dist[1] = 0
for T in range(N+N+10):
    update = (dist[B] > dist[A] + C) & (dist[A] < INF)
    if T > N:
        dist[B[update]] = -INF
    else:
        dist[B[update]] = dist[A[update]] + C[update]

assert dist[N] < INF

answer = -dist[N]
if answer < 0:
    answer = 0
elif answer == INF:
    answer = -1
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N,M,P = map(int,readline().split())
ABC = np.array(read().split(),np.int64)

A = ABC[::3]
B = ABC[1::3]
C = ABC[2::3]

C = P-C

# bellman fordでAからBへの最短距離を求める

INF = 10**18
dist = np.full(N+1,INF,np.int64)
dist[1] = 0
for T in range(N+N+10):
    update = (dist[B] > dist[A] + C) & (dist[A] < INF)
    if T > N:
        dist[B[update]] = -INF
    else:
        dist[B[update]] = dist[A[update]] + C[update]

answer = -dist[N]
if answer < 0:
    answer = 0
elif answer == INF:
    answer = -1
print(answer)

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[-inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        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 i>=num_v:
                    dist[edge[1]-1] =inf
        if i==num_v-1:
            buf=dist[-1]
    if dist[-1]!=buf or dist[-1]==-inf:
        return False
    return dist[-1]

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(max(x,0))

Submission status: Wrong Answer

Solution code:

n,m,p=map(int,input().split())
A=[]
for i in range(m):
    a,b,c=map(int,input().split())
    A.append((a,b,c-p))

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[-inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        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 i>=num_v:
                    dist[edge[1]-1] =inf
        if i==num_v-1:
            buf=dist[-1]
    if dist[-1]!=buf:
        return False
    return dist[-1]

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(max(x,0))

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[-inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        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 i>=num_v:
                    dist[edge[1]-1] =inf
        if i==num_v-1:
            buf=dist[-1]
    if dist[-1]!=buf or dist[-1]==inf or dist[-1]==-inf:
        return False
    return dist[-1]

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(max(x,0))

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        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 i>=num_v:
                    dist[edge[1]-1] =-inf
        if i==num_v-1:
            buf=dist[-1]
    if dist[-1]!=buf or dist[-1]==inf or dist[-1]==-inf:
        return False
    return dist[-1]*-1

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(max(x,0))

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        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 i>=num_v:
                    dist[edge[1]-1] =-inf
        if i==num_v-1:
            buf=dist[-1]
    if dist[-1]!=buf or dist[-1]==inf:
        return False
    return dist[-1]*-1

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(max(x,0))

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        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 i>=num_v:
                    dist[edge[1]-1] =-inf
        if i==num_v-2:
            buf=dist[-1]
    if dist[-1]!=buf or dist[-1]==inf:
        return False
    return dist[-1]*-1

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(max(x,0))

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        for edge in edges:
            if dist[edge[0]-1] != inf and dist[edge[1]-1] > dist[edge[0]-1] + edge[2]:
                dist[edge[1]-1] = dist[edge[0]-1] + edge[2]
                if i>=num_v:
                    dist[edge[1]-1] =-inf
        if i==num_v-2:
            buf=dist[-1]
    if dist[-1]!=buf or dist[-1]==inf:
        return False
    return dist[-1]*-1

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        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:
                    dist[edge[1]-1] =-inf
        if i==num_v-2:
            buf=dist[-1]
    if dist[-1]!=buf or dist[-1]==inf:
        return False
    return dist[-1]*-1

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        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:
                    dist[edge[1]-1] =-inf
        if i==num_v-2:
            buf=dist[-1]
    if dist[-1]!=buf:
        return False
    return dist[-1]*-1

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        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:
                    dist[edge[1]-1] =-inf
        if i==num_v-1:
            buf=dist[-1]
    if dist[-1]!=buf:
        return False
    return dist[-1]*-1

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[-inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        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:
            buf=dist[-1]
    if dist[-1]!=buf:
        return False
    return dist[-1]*-1

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        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:
            buf=dist[-1]
    if dist[-1]==inf or dist[-1]!=buf:
        return False
    return dist[-1]*-1

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
    #グラフの初期化
    inf=float("inf")
    dist=[inf for i in range(num_v)]
    dist[source-1]=0
  
    #辺の緩和
    buf=0
    for i in range(num_v*2):
        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:
                    dist[edge[1]-1] =inf
        if i==num_v-2:
            buf=dist[-1]
    if dist[-1]==inf or dist[-1]!=buf:
        return False
    return dist[-1]*-1

x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source-1]=0
  
  #辺の緩和
  buf=0
  for i in range(num_v*2):
    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:
            dist[edge[1]-1] =inf
        if i==num_v-1:
            dist[edge[1]-1]=buf
  if dist[-1]==inf or dist[-1]!=buf:
      return False
  return dist[-1]*-1
x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

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*2):
    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:
            dist[edge[1]-1] =inf
  if dist[-1]==inf:
      return False
  return dist[-1]*-1
x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

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:
            dist[edge[1]-1] =inf
  if dist[-1]==inf:
      return False
  return dist[-1]*-1
x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source-1]=0
  
  #辺の緩和
  buf=0
  for i in range(2*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:
            buf=dist[-1] 
        if i==num_v*2-1:
            if buf!=dist[-1]:
                return False
        #print(dist)
  #print(dist)
  if dist[-1]==inf:
      return False
  return dist[-1]*-1
x=BellmanFord(A,n,1)
if x==False:
    print(-1) 
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source-1]=0
  
  #辺の緩和
  buf=0
  for i in range(2*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:
            buf=dist[-1] 
        if i==num_v*2-1:
            if buf!=dist[-1]:
                return False
        #print(dist)
  #print(dist)
  return dist[-1]*-1
x=BellmanFord(A,n,1)
if x==False:
    print(-1)
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source-1]=0
  
  #辺の緩和
  buf=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-2:
            buf=dist[-1] 
        if i==num_v-1:
            if buf!=dist[-1]:
                return False
        #print(dist)
  #print(dist)
  return dist[-1]*-1
x=BellmanFord(A,n,1)
if x==False:
    print(-1)
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

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

def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source-1]=0
  
  #辺の緩和
  buf=0
  for i in range(num_v+1):
    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:
            buf=dist[-1] 
        if i==num_v:
            if buf!=dist[-1]:
                return False
        #print(dist)
  #print(dist)
  return dist[-1]*-1
x=BellmanFord(A,n,1)
if x==False:
    print(-1)
else:
    print(x if x>0 else 0)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline  #危険！基本オフにしろ！


def BELLMANFORD(point,n,d,p):
    cost=[1e18]*(n+1)
    cost[point]=0
    for i in range(n-1):
        for (nownode,nextnode,c) in d:
            if cost[nextnode]>cost[nownode]+c+p:  #infが定数なので論理が逆
                cost[nextnode]=cost[nownode]+c+p
    
    negative=[0]*(n+1)
    for i in range(n):
        for (nownode,nextnode,c) in d:
            if negative[nownode]:
                negative[nextnode]=1
            if cost[nextnode]>cost[nownode]+c+p:
                cost[nextnode]=cost[nownode]+c+p
                negative[nownode]=1

    return cost,negative


def main():
    n,m,p=map(int,input().split())
    
    d=[]
    for i in range(m):
        a,b,c=map(int,input().split())
        d.append((a,b,-c))
    
    x,y=BELLMANFORD(1,n,d,p)
    if y[n]:
        print(-1)
    else:
        print(max(-x[n],0))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

import sys
input=sys.stdin.readline  #危険！基本オフにしろ！

def BELLMANFORD(point,n,d,p):
    cost       = [-float("inf")]*(n+1)  #infじゃないと閉路検出がうまくいかない
    cost[point]= 0
    for i in range(n-1):                                  #経路の長さは最大n-1であるため
        for now,lis in d.items():
            for nex,c in lis:
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p

    negative=[0]*(n+1)                                        #負閉路の長さは最大nであるため
    
    for i in range(n):                  #閉路判定 negativeの1は閉路に入っている部分を表す
        for now,lis in d.items():
            for nex,c in lis:
                if negative[now]==1:
                    negative[nex]=1
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p
                    negative[now]=1
 
    return cost,negative

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

    d={}
    for s in range(m):
        a,b,c=map(int,input().split())
        if a in d:
            d[a].append((b,c))
        else:
            d[a]=[(b,c)]
    
    x,y=(BELLMANFORD(1,d,n,p))
    
    if y[n]==1:
        print(-1)
    else:
        print(max(x[n],0))

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline  #危険！基本オフにしろ！


def BELLMANFORD(point,n,d,p):
    cost=[1e18]*(n+1)
    cost[point]=0
    for i in range(n-1):
        for (nownode,nextnode,c) in d:
            if cost[nextnode]>cost[nownode]+c+p:  #infが定数なので論理が逆
                cost[nextnode]=cost[nownode]+c+p
    
    negative=[0]*(n+1)
    for (nownode,nextnode,c) in d:
        if negative[nownode]:
            negative[nextnode]=1
        if cost[nextnode]>cost[nownode]+c+p:
            cost[nextnode]=cost[nownode]+c+p
            negative[nownode]=1

    return negative,cost


def main():
    n,m,p=map(int,input().split())
    
    d=[]
    for i in range(m):
        a,b,c=map(int,input().split())
        d.append((a,b,-c))
    
    x,y=BELLMANFORD(1,n,d,p)
    if x[n]:
        print(-1)
    else:
        print(max(-y[n],0))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline  #危険！基本オフにしろ！


def BELLMANFORD(point,n,d,p):
    cost=[1e18]*(n+1)
    cost[point]=0
    for i in range(n-1):
        for (nownode,nextnode,c) in d:
            if cost[nextnode]>cost[nownode]+c+p:  #infが定数なので論理が逆
                cost[nextnode]=cost[nownode]+c+p
    for (nownode,nextnode,c) in d:
        if cost[nextnode]>cost[nownode]+c+p:
            if nextnode==n:
                return "ne"
            cost[nextnode]=cost[nownode]+c+p
            
    return -cost[n]


def main():
    n,m,p=map(int,input().split())
    
    d=[]
    for i in range(m):
        a,b,c=map(int,input().split())
        d.append((a,b,-c))
    
    x=BELLMANFORD(1,n,d,p)
    if x=="ne":
        print(-1)
    else:
        print(max(x,0))


if __name__ == '__main__':
    main()