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
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))
INF = 10**12
N, M, P = inpl()
uv = [ set() for _ in range(N) ]
vu = [ set() for _ in range(N) ]
for i in range(M):
    a, b, c = inpl()
    a -= 1
    b -= 1
    uv[a].add((b,c-P))
    vu[b].add((a,c-P))

reachable = [False]*N
pool = deque([0])
while pool:
    n = pool.popleft()
    if reachable[n] == True:
        continue
    else:
        reachable[n] = True
        for k, c in uv[n]:
            pool.append(k)
goalable = [False]*N
pool = deque([N-1])
while pool:
    n = pool.popleft()
    if goalable[n] == True:
        continue
    else:
        goalable[n] = True
        for k, c in vu[n]:
            pool.append(k)

sensible = [reachable[i] and goalable[i] for i in range(N)]
sensible_list = [i for i in range(N) if sensible[i]]
sensible_set = set(sensible_list)
for i in range(N):
    if sensible[i]:
        uv[i] = set({e for e in uv[i] if e[0] in sensible_set})
        vu[i] = set({e for e in vu[i] if e[0] in sensible_set})

state = [0]*N # 0:empty, 1:tree, -1:loop
coin = [0]*N
for i in sensible_list:
    if state[i] == 1:
        continue
    coin[i] = 0
    pool = [(i,0,0)] # (node, enter or exit, coin)
    while pool:
        n, s, d = pool.pop()
        if s == 1:
            state[n] = 1
        elif state[n] == 1:
            pass
        elif state[n] == -1:
            if d > coin[n]:
                print(-1)
                exit()
            else:
                state[n] = 1
        else: # state[n]==0 : empty
            state[n] = -1
            coin[n] = d
            pool.append((n,1,d))
            for k, c in uv[n]:
                pool.append((k,0,d+c))

cur = [-INF] * N
cur[0] = 0
ans = -INF
for t in range(N):
    prev = cur
    cur = [
        max(prev[f[0]]+f[1] for f in vu[i]) if sensible[i] and vu[i] else -INF
        for i in range(N)]
    ans = max(ans,cur[N-1])
print(max(ans,0))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))
INF = 10**12
N, M, P = inpl()
uv = [ set() for _ in range(N) ]
vu = [ set() for _ in range(N) ]
for i in range(M):
    a, b, c = inpl()
    a -= 1
    b -= 1
    uv[a].add((b,c-P))
    vu[b].add((a,c-P))

reachable = [False]*N
pool = deque([0])
while pool:
    n = pool.popleft()
    if reachable[n] == True:
        continue
    else:
        reachable[n] = True
        for k, c in uv[n]:
            pool.append(k)
goalable = [False]*N
pool = deque([N-1])
while pool:
    n = pool.popleft()
    if goalable[n] == True:
        continue
    else:
        goalable[n] = True
        for k, c in vu[n]:
            pool.append(k)

sensible = [reachable[i] and goalable[i] for i in range(N)]
sensible_list = [i for i in range(N) if sensible[i]]
sensible_set = set(sensible_list)
for i in range(N):
    if sensible[i]:
        uv[i] = set({e for e in uv[i] if e[0] in sensible_set})
        vu[i] = set({e for e in vu[i] if e[0] in sensible_set})

state = [0]*N # 0:empty, 1:tree, -1:loop
coin = [0]*N
for i in sensible_list:
    if state[i] == 1:
        continue
    coin[i] = 0
    pool = [(i,0,0)] # (node, enter or exit, coin)
    while pool:
        n, s, d = pool.pop()
        if s == 1:
            state[n] = 1
        elif state[n] == 1:
            pass
        elif state[n] == -1:
            if d > coin[n]:
                print(-1)
                exit()
            else:
                state[n] = 1
        else: # state[n]==0 : empty
            state[n] = -1
            coin[n] = d
            pool.append((n,1,d))
            for k, c in uv[n]:
                pool.append((k,0,d+c))

cur = [-INF] * N
cur[0] = 0
ans = -INF
for t in sensible_list:
    prev = cur
    cur = [
        max(prev[f[0]]+f[1] for f in vu[i]) if vu[i] else -INF
        for i in range(N)]
    ans = max(ans,cur[N-1])
print(max(ans,0))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))
INF = 10**12
N, M, P = inpl()
to = [ [] for _ in range(N) ]
for i in range(M):
    a, b, c = inpl()
    a -= 1
    b -= 1
    to[b].append((a,c-P))

cur = [-INF] * N
cur[0] = 0
ans = [-INF, -INF]
for k in range(2):
    for t in range(N):
        prev = cur
        cur = [
            max(prev[f[0]]+f[1] for f in to[i]) if to[i] else -INF
            for i in range(N)]
        ans[k] = max(ans[k],cur[N-1])
if ans[1] > ans[0]:
    print(-1)
else:
    print(max(ans[0],0))

Submission status: Wrong Answer

Solution code:
n,m,p=map(int,input().split())
data=[list(map(int,input().split())) for i in range(m)]
inf=float("inf")
dist=[inf]*(n+1)
dist[1]=0
for i in range(n):
    for a,b,c in data:
        if dist[a]!=inf:
            dist[b]=min(dist[b],dist[a]+p-c)
ans=-dist[n]
for i in range(n):
    for a,b,c in data:
        if dist[a]!=inf:
            dist[b]=min(dist[b],dist[a]+p-c)
if -dist[n]==ans:
    print(max(0,ans))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n,m,p=map(int,input().split())
data=[list(map(int,input().split())) for i in range(m)]
inf=float("inf")
dist=[inf]*(n+1)
dist[1]=0
for i in range(n-1):
    for a,b,c in data:
        if dist[a]!=inf:
            dist[b]=min(dist[b],dist[a]+p-c)
ans=-dist[-1]
for i in range(n-1):
    for a,b,c in data:
        if dist[a]!=inf:
            dist[b]=min(dist[b],dist[a]+p-c)
if -dist[-1]==ans:
    print(max(0,ans))
else:
    print(-1)

Submission status: Wrong Answer

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

que=[1]
inf=float("inf")
flag=[inf]*(n+1)
flag[1]=0
count=0
while que:
    H=[]
    count+=1
    for h in que:
        for u,c in data[h]:
            if flag[u]<flag[h]+c:
                continue
            else:
                flag[u]=flag[h]+c
                H.append(u)
    que=H
    if count==m:
        break
else:
    print(max(0,-flag[n]))
    sys.exit()
    
z=-flag[n]
flag=[inf]*(n+1)
for u in que:
    flag[u]=0
count=0
while que:
    H=[]
    count+=1
    for h in que:
        for u,c in data[h]:
            if flag[u]<flag[h]+c:
                continue
            else:
                flag[u]=flag[h]+c
                H.append(u)
    que=H
    if count==m:
        break
if flag[n]==inf:
    print(max(0,z))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
(n,m,p),*l=[list(map(int,s.split()))for s in open(0)];d=[0]*2+[9e99]*n
for i in range(n*2):
 for a,b,c in l:
  if d[b]>d[a]-c+p:d[b]=d[a]-c+p
 if i==n:x=d[n]
print([max(-x,0),-1][d[n]!=x])

Submission status: Wrong Answer

Solution code:
n,m,p=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
distance=[9999999999999999]*n
distance[0]=0
for i in range(n-1):
  for j in range(m):
    if distance[l[j][1]-1]>distance[l[j][0]-1]-l[j][2]+p:
      distance[l[j][1]-1]=distance[l[j][0]-1]-l[j][2]+p
x=distance[n-1]
for i in range(n):
  for j in range(m):
    if distance[l[j][1]-1]>distance[l[j][0]-1]-l[j][2]+p:
      distance[l[j][1]-1]=-9999999999999999
if distance[n-1]!=x:
  print(-1)
elif x>0:
  print(0)
else:
  print(max(-distance[n-1],0))

Submission status: Wrong Answer

Solution code:
n,m,p=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
distance=[9999999999999999]*n
distance[0]=0
for i in range(n-1):
  for j in range(m):
    if distance[l[j][1]-1]>distance[l[j][0]-1]-l[j][2]+p:
      distance[l[j][1]-1]=distance[l[j][0]-1]-l[j][2]+p
x=distance[n-1]
for i in range(n):
  for j in range(m):
    if distance[l[j][1]-1]>distance[l[j][0]-1]-l[j][2]+p:
      distance[l[j][1]-1]=-9999999999999999
if distance[n-1]==x:
  print(max(-distance[n-1],0))
elif x>0:
  print(0)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n,m,p=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
distance=[9999999999999999]*n
distance[0]=0
for i in range(n-1):
  for j in range(m):
    if distance[l[j][1]-1]>distance[l[j][0]-1]-l[j][2]+p:
      distance[l[j][1]-1]=distance[l[j][0]-1]-l[j][2]+p
x=distance[n-1]
for i in range(n):
  for j in range(m):
    if distance[l[j][1]-1]>distance[l[j][0]-1]-l[j][2]+p:
      distance[l[j][1]-1]=-9999999999999999
if distance[n-1]==x:
  print(max(-distance[n-1],0))
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n,m,p=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
distance=[-9999999999999999]*n
distance[0]=0
for i in range(n-1):
  for j in range(m):
    if distance[l[j][1]-1]<distance[l[j][0]-1]+l[j][2]-p:
      distance[l[j][1]-1]=distance[l[j][0]-1]+l[j][2]-p
x=distance[n-1]
for i in range(n):
  for j in range(m):
    if distance[l[j][1]-1]<distance[l[j][0]-1]+l[j][2]-p:
      distance[l[j][1]-1]=distance[l[j][0]-1]+l[j][2]-p
if distance[n-1]==x:
  print(max(distance[n-1],0))
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n,m,p=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
distance=[9999999999999999]*n
distance[0]=0
for i in range(n-1):
  for j in range(m):
    if distance[l[j][1]-1]>distance[l[j][0]-1]-l[j][2]+p:
      distance[l[j][1]-1]=distance[l[j][0]-1]-l[j][2]+p
x=distance[n-1]
for i in range(n):
  for j in range(m):
    if distance[l[j][1]-1]>distance[l[j][0]-1]-l[j][2]+p:
      distance[l[j][1]-1]=distance[l[j][0]-1]-l[j][2]+p
if distance[n-1]==x:
  print(max(-distance[n-1],0))
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

Solution code:
n,m,p=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
distance=[9999999999999999]*n
distance[0]=0
for i in range(n-1):
  for j in range(m):
    if distance[l[j][1]-1]>distance[l[j][0]-1]-l[j][2]+p:
      distance[l[j][1]-1]=distance[l[j][0]-1]-l[j][2]+p
f=[0]*n
for i in range(n):
  for j in range(m):
    if distance[l[j][1]-1]>distance[l[j][0]-1]-l[j][2]+p:
      distance[l[j][1]-1]=distance[l[j][0]-1]-l[j][2]+p
      f[l[j][1]-1]=1
    if f[l[j][0]-1]==1:
      f[l[j][1]-1]=1
if f[n-1]==0:
  print(-distance[n-1])
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
import sys
from heapq import heapify, heappop, heappush
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def tami(n, visited, root, end):
  if n == end:
    for i in range(N+1):
      if visited[i] == True:
        finished[i] = True
    return
  for x,c in root[n]:
    if visited[x] == False:
      visited[x] = True
      tami(x, visited, root, end)
      visited[x] = False

N, M, P = IL()
abc = [IL() for _ in range(M)]

finished = [False]*(N+1)
edge = [[] for _ in range(N+1)]
Redge = [[] for _ in range(N+1)]
for a,b,c in abc:
  edge[a].append((b, c-P))
  Redge[b].append((a, c-P))
visited = [False]*(N+1)
tami(1, visited, edge, N)
visited = [False]*(N+1)
tami(N, visited, Redge, 1)
#print(finished)

val = [0]*(N+1)
update = 1
for cnt in range(N):
  update = 0
  for a in range(1,N+1):
    for b,c in edge[a]:
      if finished[a] and finished[b]:
        if val[b] < val[a] + c:
          val[b] = val[a] + c
          update = 1
else:
  if update == 1:
    print(-1)
  else:
    print(val[N])
#print(val)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,p=map(int,input().split())
edges=[None for i in range(m)]
natural=[[] for i in range(n)]
reverse=[[] for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  a-=1;b-=1;c-=p
  natural[a].append(b)
  reverse[b].append(a)
  edges[i]=(a,b,-c)
from0=set([0])
fromG=set([n-1])
visited=[False]*n
visited[0]=True
queue=deque([0])
while queue:
  v=queue.popleft()
  for i in natural[v]:
    if visited[i]:continue
    visited[i]=True
    from0.add(i)
    queue.append(i)
for i in range(n):
  visited[i]=False
visited[-1]=True
queue.append(n-1)
while queue:
  v=queue.popleft()
  for i in reverse[v]:
    if visited[i]:continue
    visited[i]=True
    fromG.add(i)
    queue.append(i)
points=from0&fromG
use=[]
for i in edges:
  if i[0] in points and i[1] in points:
    use.append(i)
def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=10**15
  global dist
  dist=[inf for i in range(num_v)]
  dist[source]=0
  print(dist,edges)
  Ncycle=False
  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if edge[0]!=inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
        dist[edge[1]] = dist[edge[0]] + edge[2]
        if i==num_v-1: return False
  
  return dist[-1]
flag=0
for i in use:
  if i[2]!=0:
    flag=1
    break
if flag==0:print(0);exit()
if BellmanFord(use,n,0)==False:print(-1)
else:
  print(max(0,-BellmanFord(use,n,0)))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,p=map(int,input().split())
edges=[None for i in range(m)]
natural=[[] for i in range(n)]
reverse=[[] for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  a-=1;b-=1;c-=p
  natural[a].append(b)
  reverse[b].append(a)
  edges[i]=(a,b,-c)
from0=set([0])
fromG=set([n-1])
visited=[False]*n
visited[0]=True
queue=deque([0])
while queue:
  v=queue.popleft()
  for i in natural[v]:
    if visited[i]:continue
    visited[i]=True
    from0.add(i)
    queue.append(i)
for i in range(n):
  visited[i]=False
visited[-1]=True
queue.append(n-1)
while queue:
  v=queue.popleft()
  for i in reverse[v]:
    if visited[i]:continue
    visited[i]=True
    fromG.add(i)
    queue.append(i)
points=from0&fromG
use=[]
for i in edges:
  if i[0] in points and i[1] in points:
    use.append(i)
def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  global dist
  dist=[inf for i in range(num_v)]
  dist[source]=0
  Ncycle=False
  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if edge[0]!=inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
        dist[edge[1]] = dist[edge[0]] + edge[2]
        if i==num_v-1: return False
  
  return dist[-1]
if n==2 and m==1 and use[0][2]==0:print(0)
elif BellmanFord(use,n,0)==False:print(-1)
else:
  print(max(0,-BellmanFord(use,n,0)))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,p=map(int,input().split())
edges=[None for i in range(m)]
natural=[[] for i in range(n)]
reverse=[[] for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  a-=1;b-=1;c-=p
  natural[a].append(b)
  reverse[b].append(a)
  edges[i]=(a,b,-c)
from0=set([0])
fromG=set([n-1])
visited=[False]*n
visited[0]=True
queue=deque([0])
while queue:
  v=queue.popleft()
  for i in natural[v]:
    if visited[i]:continue
    visited[i]=True
    from0.add(i)
    queue.append(i)
for i in range(n):
  visited[i]=False
visited[-1]=True
queue.append(n-1)
while queue:
  v=queue.popleft()
  for i in reverse[v]:
    if visited[i]:continue
    visited[i]=True
    fromG.add(i)
    queue.append(i)
points=from0&fromG
use=[]
for i in edges:
  if i[0] in points and i[1] in points:
    use.append(i)
def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source]=0
  Ncycle=False
  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if edge[0]!=inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
        dist[edge[1]] = dist[edge[0]] + edge[2]
        if i==num_v-1: return False
  
  return dist[-1]
if BellmanFord(use,n,0)==False:print(-1)
else:
  print(max(0,-BellmanFord(use,n,0)))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,p=map(int,input().split())
edges=[None for i in range(m)]
natural=[[] for i in range(n)]
reverse=[[] for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  a-=1;b-=1;c-=p
  natural[a].append(b)
  reverse[b].append(a)
  edges[i]=(a,b,-c)
from0=set([0])
fromG=set([n-1])
visited=[False]*n
visited[0]=True
queue=deque([0])
while queue:
  v=queue.popleft()
  for i in natural[v]:
    if visited[i]:continue
    visited[i]=True
    from0.add(i)
    queue.append(i)
for i in range(n):
  visited[i]=False
visited[-1]=True
queue.append(n-1)
while queue:
  v=queue.popleft()
  for i in reverse[v]:
    if visited[i]:continue
    visited[i]=True
    fromG.add(i)
    queue.append(i)
points=from0&fromG
use=[]
for i in edges:
  if i[0] in points and i[1] in points:
    use.append(i)
print(use)
def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source]=0
  Ncycle=False
  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if edge[0]!=inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
        dist[edge[1]] = dist[edge[0]] + edge[2]
        if i==num_v-1: return False
  
  return dist[-1]
if BellmanFord(use,n,0)==False:print(-1)
else:
  print(max(0,-BellmanFord(use,n,0)))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,p=map(int,input().split())
edges=[None for i in range(m)]
natural=[[] for i in range(n)]
reverse=[[] for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  a-=1;b-=1;c-=p
  natural[a].append(b)
  reverse[b].append(a)
  edges[i]=(a,b,-c)
from0=set([0])
fromG=set([n-1])
visited=[False]*n
visited[0]=True
queue=deque([0])
while queue:
  v=queue.popleft()
  for i in natural[v]:
    if visited[i]:continue
    visited[i]=True
    from0.add(i)
    queue.append(i)
for i in range(n):
  visited[i]=False
visited[-1]=True
queue.append(n-1)
while queue:
  v=queue.popleft()
  for i in reverse[v]:
    if visited[i]:continue
    visited[i]=True
    fromG.add(i)
    queue.append(i)
points=from0&fromG
use=[]
for i in edges:
  if i[0] in points and i[1] in points:
    use.append(i)
def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source]=0
  
  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if edge[0]!=inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
        dist[edge[1]] = dist[edge[0]] + edge[2]
        if i==num_v-1: return -1
  
  return dist[-1]
if BellmanFord(use,n,0)==-1:print(-1)
else:
  print(max(0,-BellmanFord(use,n,0)))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,p=map(int,input().split())
edges=[None for i in range(m)]
natural=[[] for i in range(n)]
reverse=[[] for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  a-=1;b-=1;c-=p
  natural[a].append(b)
  reverse[b].append(a)
  edges[i]=(a,b,-c)
from0=set([0])
fromG=set([n-1])
visited=[False]*n
visited[0]=True
queue=deque([0])
while queue:
  v=queue.popleft()
  for i in natural[v]:
    if visited[i]:continue
    visited[i]=True
    from0.add(i)
    queue.append(i)
for i in range(n):
  visited[i]=False
visited[-1]=True
queue.append(n-1)
while queue:
  v=queue.popleft()
  for i in reverse[v]:
    if visited[i]:continue
    visited[i]=True
    fromG.add(i)
    queue.append(i)
points=from0&fromG
for i in edges:
  if i[0] in points and i[1] in points:continue
  edges.remove(i)
def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source]=0
  
  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if dist[edge[1]] > dist[edge[0]] + edge[2]:
        dist[edge[1]] = dist[edge[0]] + edge[2]
        if i==num_v-1: return -1
  
  return dist[-1]
if BellmanFord(edges,n,0)==-1 or BellmanFord(edges,n,0)==float("inf"):print(-1)
else:
  print(max(0,-BellmanFord(edges,n,0)))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,p=map(int,input().split())
edges=[None for i in range(m)]
natural=[[] for i in range(n)]
reverse=[[] for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  a-=1;b-=1;c-=p
  natural[a].append(b)
  reverse[b].append(a)
  edges[i]=(a,b,-c)
from0=set([0])
fromG=set([n-1])
visited=[False]*n
visited[0]=True
queue=deque([0])
while queue:
  v=queue.popleft()
  for i in natural[v]:
    if visited[i]:continue
    visited[i]=True
    from0.add(i)
    queue.append(i)
for i in range(n-1):
  visited[i]=False
queue.append(n-1)
while queue:
  v=queue.popleft()
  for i in reverse[v]:
    if visited[i]:continue
    visited[i]=True
    fromG.add(i)
    queue.append(i)
points=from0&fromG
for i in edges:
  if i[0] in points and i[1] in points:continue
  edges.remove(i)
def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source]=0
  
  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if dist[edge[1]] > dist[edge[0]] + edge[2]:
        dist[edge[1]] = dist[edge[0]] + edge[2]
        if i==num_v-1: return -1
  
  return dist[-1]
if BellmanFord(edges,n,0)==-1:print(-1)
else:
  print(max(0,-BellmanFord(edges,n,0)))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,p=map(int,input().split())
edges=[None for i in range(m)]
natural=[[] for i in range(n)]
reverse=[[] for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  a-=1;b-=1;c-=p
  natural[a].append(b)
  reverse[b].append(a)
  edges[i]=(a,b,-c)
from0=set([0])
fromG=set([n-1])
visited=[False]*n
visited[0]=True
queue=deque([0])
while queue:
  v=queue.popleft()
  for i in natural[v]:
    if visited[i]:continue
    visited[i]=True
    from0.add(i)
    queue.append(i)
for i in range(n-1):
  visited[i]=False
queue.append(n-1)
while queue:
  v=queue.popleft()
  for i in reverse[v]:
    if visited[i]:continue
    visited[i]=True
    fromG.add(i)
    queue.append(i)
points=from0&fromG
for i in edges:
  if i[0] in points and i[1] in points:continue
  edges.remove(i)
def BellmanFord(edges,num_v,source):
  #グラフの初期化
  inf=float("inf")
  dist=[inf for i in range(num_v)]
  dist[source]=0
  
  #辺の緩和
  for i in range(num_v):
    for edge in edges:
      if dist[edge[1]] > dist[edge[0]] + edge[2]:
        dist[edge[1]] = dist[edge[0]] + edge[2]
        if i==num_v-1: return 1
  
  return dist[-1]
if BellmanFord(edges,n,0)==-1:print(-1)
else:
  print(max(0,-BellmanFord(edges,n,0)))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    print(max(a + b, a - b, a * b))
    return

#B
def B():
    k, x = LI()
    ans = []
    for i in range(x - k + 1, x + k):
        ans.append(i)
    print(*ans)
    return

#C
def C():
    n = II()
    d = defaultdict(int)
    for _ in range(n):
        s = S()
        s.sort()
        d[tuple(s)] += 1
    ans = 0
    for i in d.values():
        ans += i * (i - 1) // 2
    print(ans)
    return

# D
# 解説AC
# 後方から見ていくのはわかったけど。。。
# i日前に選べるバイトの中から一番報酬が大きいを選んでいく
def D():
    n, m = LI()
    q = []
    d = defaultdict(list)
    ans = 0
    for _ in range(n):
        a, b = LI()
        d[a].append(b)
    for i in range(m):
        for di in d[i+1]:
            heappush(q, -di)
        if q:
            ans -= heappop(q)
    print(ans)
    return

#E
def E():
    n,m,p = LI()
    dag = [-inf] * n
    edg = LIR_(m)
    dag[0] = 0
    for i in range(n):
        for a, b, c in edg:
            c += 1
            c -= p
            if dag[a] + c > dag[b]:
                dag[b] = dag[a] + c
    ans = dag[n - 1]
    for i in range(n):
        for a, b, c in edg:
            c += 1
            c -= p
            if dag[a] + c > dag[b]:
                dag[b] = dag[a] + c
    if ans != dag[n - 1]:
        print(-1)
    else:
        print(max(ans, 0))

    return

#F
def F():
    return

#Solve
if __name__ == '__main__':
    E()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    print(max(a + b, a - b, a * b))
    return

#B
def B():
    k, x = LI()
    ans = []
    for i in range(x - k + 1, x + k):
        ans.append(i)
    print(*ans)
    return

#C
def C():
    n = II()
    d = defaultdict(int)
    for _ in range(n):
        s = S()
        s.sort()
        d[tuple(s)] += 1
    ans = 0
    for i in d.values():
        ans += i * (i - 1) // 2
    print(ans)
    return

# D
# 解説AC
# 後方から見ていくのはわかったけど。。。
# i日前に選べるバイトの中から一番報酬が大きいを選んでいく
def D():
    n, m = LI()
    q = []
    d = defaultdict(list)
    ans = 0
    for _ in range(n):
        a, b = LI()
        d[a].append(b)
    for i in range(m):
        for di in d[i+1]:
            heappush(q, -di)
        if q:
            ans -= heappop(q)
    print(ans)
    return

#E
def E():
    n,m,p = LI()
    dag = [-inf] * n
    edg = LIR_(m)
    dag[0] = 0
    for i in range(2 * n):
        for a, b, c in edg:
            c += 1
            c -= p
            if dag[a] + c > dag[b]:
                dag[b] = dag[a] + c
    ans = dag[n - 1]
    for i in range(2 * n):
        for a, b, c in edg:
            c += 1
            c -= p
            if dag[a] + c > dag[b]:
                dag[b] = dag[a] + c
    if ans != dag[n - 1]:
        print(-1)
    else:
        print(max(ans, 0))

    return

#F
def F():
    return

#Solve
if __name__ == '__main__':
    E()