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 solve():
    ans = 0
    N, M, P = map(int, input().split())
    edge = []
    for i in range(M):
        a,b,c = map(int, input().split())
        edge.append([P-c,a-1,b-1])
    d = [10**10]*N
    d[0]=0
    n = 0
    while True:
        n += 1
        update = False
        if n==N:
            updateN = False
        for i in range(M):
            w,u,v = edge[i]
            if d[u]<10**10 and d[v]>d[u]+w:
                d[v] = d[u]+w
                update = True
                if n>=N and v==N-1:
                    updateN = True
        if not update:
            break
        if n==2*N:
            if updateN:
                return -1
            break
    return max(0,-d[-1])
print(solve())

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7
INF = 10 ** 18

N, M, P = map(int, input().split())
G = []
G_append = G.append

g = [[] for _ in range(N + 1)]
for _ in range(M):
    A, B, C = map(int, input().split())
    G_append((A, B, -C + P))
    g[A].append(B)

d = [INF] * (N + 1) #最短距離を記録しておくリスト
d[1] = 0
def find_negative_loop1(): #負の閉路が存在するかを確認
    for i in range(N):
        for j in range(M): #無向グラフのときは辺は実質的には2倍
            e = G[j]
            if d[e[0]] != INF and d[e[1]] > d[e[0]] + e[2]:
                d[e[1]] = d[e[0]] + e[2]
                #頂点の回数と同じだけループを回しても更新が起きる-->負の閉回路が存在
                if i == N-1:
                    return True #Trueの時, 負の閉路が存在
    return False #Falseの時, 負の閉路が存在しない

def find_negative_loop2(): #負の閉路が存在するかを確認
    check = [False] * (N + 1)
    for _ in range(N):
        for j in range(M): #無向グラフのときは辺は実質的には2倍
            e = G[j]
            if d[e[0]] != INF and d[e[1]] > d[e[0]] + e[2]:
                d[e[1]] = d[e[0]] + e[2]
                check[e[1]] = True
    return check[N]

#呼び出しスタート地点を入れる
if find_negative_loop1():
    if find_negative_loop2():
        print (-1)
    else:
        print (max(0, -d[N]))
else:
    print (max(0, -d[N]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7
INF = 10 ** 18

N, M, P = map(int, input().split())
G = []
G_append = G.append

g = [[] for _ in range(N + 1)]
for _ in range(M):
    A, B, C = map(int, input().split())
    G_append((A, B, -C + P))
    g[A].append(B)

d = [INF] * (N + 1) #最短距離を記録しておくリスト
d[1] = 0
def find_negative_loop1(): #負の閉路が存在するかを確認
    for i in range(N):
        for j in range(M): #無向グラフのときは辺は実質的には2倍
            e = G[j]
            if d[e[1]] > d[e[0]] + e[2]:
                d[e[1]] = d[e[0]] + e[2]
                #頂点の回数と同じだけループを回しても更新が起きる-->負の閉回路が存在
                if i == N-1:
                    return True #Trueの時, 負の閉路が存在
    return False #Falseの時, 負の閉路が存在しない

def find_negative_loop2(): #負の閉路が存在するかを確認
    check = [False] * (N + 1)
    for _ in range(N):
        for j in range(M): #無向グラフのときは辺は実質的には2倍
            e = G[j]
            if d[e[1]] > d[e[0]] + e[2]:
                d[e[1]] = d[e[0]] + e[2]
                check[e[1]] = True
    return check[N]

#呼び出しスタート地点を入れる
if find_negative_loop1():
    if find_negative_loop2():
        print (-1)
    else:
        print (max(0, -d[N]))
else:
    print (max(0, -d[N]))

Submission status: Wrong Answer

Solution code:
def solve():
    ans = 0
    N, M, P = map(int, input().split())
    edge = []
    for i in range(M):
        a,b,c = map(int, input().split())
        edge.append([P-c,a-1,b-1])
    d = [10**10]*N
    d[0]=0
    n = 0
    while True:
        update = False
        if n==N-1:
            updateN = False
        for i in range(M):
            w,u,v = edge[i]
            if d[u]<10**10 and d[v]>d[u]+w:
                d[v] = d[u]+w
                update = True
                if n>=N-1 and v==N-1:
                    updateN = True
        if not update:
            break
        n += 1
        if n==2*N:
            if updateN:
                return -1
            break
    return max(0,-d[-1])
print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
    ans = 0
    N, M, P = map(int, input().split())
    edge = []
    for i in range(M):
        a,b,c = map(int, input().split())
        edge.append([P-c,a-1,b-1])
    d = [10**10]*N
    d[0]=0
    n = 0
    while True:
        update = False
        if n==N-1:
            updateN = False
        for i in range(M):
            w,u,v = edge[i]
            if d[u]<10**10 and d[v]>d[u]+w:
                d[v] = d[u]+w
                update = True
                if n>=N and v==N-1:
                    updateN = True
        if not update:
            break
        n += 1
        if n==2*N:
            if updateN:
                return -1
            break
    return max(0,-d[-1])
print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
    ans = 0
    N, M, P = map(int, input().split())
    edge = []
    for i in range(M):
        a,b,c = map(int, input().split())
        edge.append([P-c,a-1,b-1])
    d = [10**10]*N
    d[0]=0
    n = 0
    while True:
        update = False
        updateN = False
        for i in range(M):
            w,u,v = edge[i]
            if d[u]<10**10 and d[v]>d[u]+w:
                d[v] = d[u]+w
                update = True
                if v==N-1:
                    updateN = True
        if not update:
            break
        n += 1
        if n==N:
            if updateN:
                return -1
            break
    return max(0,-d[-1])
print(solve())

Submission status: Wrong Answer

Solution code:
N,M,P = map(int, input().split())
E = [[] for _ in range(N)]
for _ in range(M):
    a,b,c = map(int, input().split())
    E[a-1].append((b-1, -c+P))

def BellmanFord(E, N, source):
    inf = 10**20
    d = [inf for _ in range(N)]
    d[source] = 0
    
    for i in range(2*N):
        for j in range(N):
            e = E[j]
            if len(e) == 0:
                continue
            for edge in e:
                if d[edge[0]] > d[j] + edge[1]:
                    d[edge[0]] = d[j] + edge[1]
                    if i == 2*N-1 and edge[0] == N-1:
                        return -1
    
    return d

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

Submission status: Wrong Answer

Solution code:
N,M,P = map(int, input().split())
E = [[] for _ in range(N)]
for _ in range(M):
    a,b,c = map(int, input().split())
    E[a-1].append((b-1, -c+P))

def BellmanFord(E, N, source):
    inf = 10**20
    d = [inf for _ in range(N)]
    d[source] = 0
    
    for i in range(N+1):
        for j in range(N):
            e = E[j]
            if len(e) == 0:
                continue
            for edge in e:
                if d[edge[0]] > d[j] + edge[1]:
                    d[edge[0]] = d[j] + edge[1]
                    if i == N and edge[0] == N-1:
                        return -1
    
    return d

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

Submission status: Wrong Answer

Solution code:
N,M,P = map(int, input().split())
E = [[] for _ in range(N)]
for _ in range(M):
    a,b,c = map(int, input().split())
    E[a-1].append((b-1, -c+P))

def BellmanFord(E, N, source):
    inf = 10**20
    d = [inf for _ in range(N)]
    d[source] = 0
    
    for i in range(N):
        for j in range(N):
            e = E[j]
            if len(e) == 0:
                continue
            for edge in e:
                if d[edge[0]] > d[j] + edge[1]:
                    d[edge[0]] = d[j] + edge[1]
                    if i == N-1 and edge[0] == N-1:
                        return -1
    
    return d

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

Submission status: Wrong Answer

Solution code:
import heapq
import sys
from collections import deque
input = sys.stdin.readline

INF = 10 ** 4

class Edge():
    def __init__(self, start, end, cost):
        self.start = start
        self.end = end
        self.cost = cost

def dijkstra(n, G, s):
    dist = [INF] * n
    dist[s] = 0
    h = []
    heapq.heappush(h, (0, s))

    while h:
        # 使っていない頂点のうち、現時点で最も距離の近いものを選びvとする
        d, v = heapq.heappop(h)
        if dist[v] < d:
            continue
        # vから到達可能な頂点について、距離が短くなれば更新
        for e in G[v]:
            if dist[e.end] > dist[v] + e.cost:
                dist[e.end] = dist[v] + e.cost
                heapq.heappush(h, (dist[e.end], e.end))
    return dist

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

g1 = [[] for _ in range(n)]
g2 = [[] for _ in range(n)]
es = []

abc = []

for _ in range(m):
    a, b, c = map(int, input().split())
    abc.append((a-1, b-1, c))
    g1[a-1].append(Edge(a-1, b-1, 1))
    g2[b-1].append(Edge(b-1, a-1, 1))

dist1 = dijkstra(n, g1, 0)
dist2 = dijkstra(n, g2, n-1)

# print(dist1)
# print(dist2)

ng = set({})

for i in range(n):
    if dist1[i] == INF or dist2[i] == INF:
        ng.add(i)

for a, b, c in abc:
    if a in ng or b in ng:
        continue
    es.append(Edge(a, b, p-c))

def bellman_ford(n, es, s):
    dist = [INF] * n
    dist[s] = 0
    # 更新が止まるまで繰り返す O(V*E)
    # i度目の更新までにsを始点として0~i個の辺からなる経路が探索され、その時点の最短経路が計算される
    # 最短経路は最大でもV-1個の頂点しか通らないため、最悪でもv-1回で収束する
    while True:
        update = False
        # 辺をすべて見る
        for e in es:
            # aに到達可能かつa経由の方が近ければ更新する
            if dist[e.start] != INF and dist[e.end] > dist[e.start] + e.cost:
                dist[e.end] = dist[e.start] + e.cost
                update = True
        # 一度も更新されなければ終了
        if not update:
            break
    return dist

def find_negative_loop(n, es):
    dist = [0] * n
    for i in range(n):
        for e in es:
            if dist[e.end] > dist[e.start] + e.cost:
                dist[e.end] = dist[e.start] + e.cost
                if i == n - 1:
                    return True
    return False

if find_negative_loop(n, es):
    print(-1)
    sys.exit()

dist = bellman_ford(n, es, 0)
print(max(0, -dist[n-1]))

Submission status: Wrong Answer

Solution code:
import heapq
import sys
from collections import deque
input = sys.stdin.readline

INF = 10 ** 4

class Edge():
    def __init__(self, end, cost):
        self.end = end
        self.cost = cost

def dijkstra(n, G, s):
    dist = [INF] * n
    dist[s] = 0
    h = []
    heapq.heappush(h, (0, s))

    while h:
        # 使っていない頂点のうち、現時点で最も距離の近いものを選びvとする
        d, v = heapq.heappop(h)
        if dist[v] < d:
            continue
        # vから到達可能な頂点について、距離が短くなれば更新
        for e in G[v]:
            if dist[e.end] > dist[v] + e.cost:
                dist[e.end] = dist[v] + e.cost
                heapq.heappush(h, (dist[e.end], e.end))
    return dist

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

g1 = [[] for _ in range(n)]
g2 = [[] for _ in range(n)]
g3 = [[] for _ in range(n)]
g4 = [[] for _ in range(n)]

abc = []

for _ in range(m):
    a, b, c = map(int, input().split())
    abc.append((a-1, b-1, c))
    g1[a-1].append(Edge(b-1, 1))
    g2[b-1].append(Edge(a-1, 1))

dist1 = dijkstra(n, g1, 0)
dist2 = dijkstra(n, g2, n-1)

# print(dist1)
# print(dist2)

ng = set({})

for i in range(n):
    if dist1[i] == INF or dist2[i] == INF:
        ng.add(i)

H = [0] * n
cnt = 0
for a, b, c in abc:
    if a in ng or b in ng:
        continue
    cnt += 1
    g3[a].append(Edge(b, c))
    g4[b].append(Edge(a, c-p))
    H[b] += 1

stack = deque([])
topo = []

for i, hi in enumerate(H):
    if hi == 0:
        stack.append(i)

while stack:
    v = stack.pop()
    topo.append(v)
    for e in g3[v]:
        H[e.end] -= 1
        if H[e.end] == 0:
            stack.append(e.end)   

if len(topo) < cnt:
    print(-1)
    sys.exit()

f = 0
dp = ['null'] * n
dp[0] = 0

for v in topo:
    if f:
        dp[v] = 0
        for e in g4[v]:
            if e.end == 0 or dp[e.end] != "null":
                dp[v] = max(dp[v], dp[e.end] + e.cost)
    if v == 0:
        f = 1

print(dp[n-1])

Submission status: Wrong Answer

Solution code:
import heapq
import sys
from collections import deque
input = sys.stdin.readline

INF = 10 ** 4

class Edge():
    def __init__(self, end, cost):
        self.end = end
        self.cost = cost

def dijkstra(n, G, s):
    dist = [INF] * n
    dist[s] = 0
    h = []
    heapq.heappush(h, (0, s))

    while h:
        # 使っていない頂点のうち、現時点で最も距離の近いものを選びvとする
        d, v = heapq.heappop(h)
        if dist[v] < d:
            continue
        # vから到達可能な頂点について、距離が短くなれば更新
        for e in G[v]:
            if dist[e.end] > dist[v] + e.cost:
                dist[e.end] = dist[v] + e.cost
                heapq.heappush(h, (dist[e.end], e.end))
    return dist

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

g1 = [[] for _ in range(n)]
g2 = [[] for _ in range(n)]
g3 = [[] for _ in range(n)]
g4 = [[] for _ in range(n)]

abc = []

for _ in range(m):
    a, b, c = map(int, input().split())
    abc.append((a-1, b-1, c))
    g1[a-1].append(Edge(b-1, c))
    g2[b-1].append(Edge(a-1, c))

dist1 = dijkstra(n, g1, 0)
dist2 = dijkstra(n, g2, n-1)

ng = set({})

for i in range(n):
    if dist1[i] == INF or dist2[i] == INF:
        ng.add(i)

H = [0] * n
cnt = 0
for a, b, c in abc:
    if a in ng or b in ng:
        continue
    cnt += 1
    g3[a].append(Edge(b, c))
    g4[b].append(Edge(a, c-p))
    H[b] += 1

stack = deque([])
topo = []

for i, hi in enumerate(H):
    if hi == 0:
        stack.append(i)

while stack:
    v = stack.pop()
    topo.append(v)
    for e in g3[v]:
        H[e.end] -= 1
        if H[e.end] == 0:
            stack.append(e.end)   

# if len(topo) < cnt:
#     print(-1)
#     sys.exit()

f = 0
dp = ['null'] * n
dp[0] = 0

for v in topo:
    if f:
        dp[v] = 0
        for e in g4[v]:
            if e.end == 0 or dp[e.end] != "null":
                dp[v] = max(dp[v], dp[e.end] + e.cost)
    if v == 0:
        f = 1

print(dp[n-1])

Submission status: Wrong Answer

Solution code:
import heapq
import sys
from collections import deque
input = sys.stdin.readline

INF = 10 ** 4

class Edge():
    def __init__(self, end, cost):
        self.end = end
        self.cost = cost

def dijkstra(n, G, s):
    dist = [INF] * n
    dist[s] = 0
    h = []
    heapq.heappush(h, (0, s))

    while h:
        # 使っていない頂点のうち、現時点で最も距離の近いものを選びvとする
        d, v = heapq.heappop(h)
        if dist[v] < d:
            continue
        # vから到達可能な頂点について、距離が短くなれば更新
        for e in G[v]:
            if dist[e.end] > dist[v] + e.cost:
                dist[e.end] = dist[v] + e.cost
                heapq.heappush(h, (dist[e.end], e.end))
    return dist

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

g1 = [[] for _ in range(n)]
g2 = [[] for _ in range(n)]
g3 = [[] for _ in range(n)]
g4 = [[] for _ in range(n)]

abc = []

for _ in range(m):
    a, b, c = map(int, input().split())
    abc.append((a-1, b-1, c))
    g1[a-1].append(Edge(b-1, c))
    g2[b-1].append(Edge(a-1, c))

dist1 = dijkstra(n, g1, 0)
dist2 = dijkstra(n, g2, n-1)

ng = set({})

for i in range(n):
    if dist1[i] == INF or dist2[i] == INF:
        ng.add(i)

H = [0] * n
cnt = 0
for a, b, c in abc:
    if a in ng or b in ng:
        continue
    cnt += 1
    g3[a].append(Edge(b, c))
    g4[b].append(Edge(a, c-p))
    H[b] += 1

stack = deque([])
topo = []

for i, hi in enumerate(H):
    if hi == 0:
        stack.append(i)

while stack:
    v = stack.pop()
    topo.append(v)
    for e in g3[v]:
        H[e.end] -= 1
        if H[e.end] == 0:
            stack.append(e.end)   

if len(topo) < cnt:
    print(-1)
    sys.exit()

f = 0
dp = ['null'] * n
dp[0] = 0

for v in topo:
    if f:
        dp[v] = 0
        for e in g4[v]:
            if e.end == 0 or dp[e.end] != "null":
                dp[v] = max(dp[v], dp[e.end] + e.cost)
    if v == 0:
        f = 1

print(dp[n-1])

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())
def LN(n):return [N() for i in range(n)]
def LL(n):return [L() for i in range(n)]
n,m,p=NM()
edge=[]
for i in range(m):
    a,b,c=map(int,input().split())
    edge.append((a,b,p-c))
INF=float('inf')
def bellmanford(s,n):
    d = [INF for i in range(n+1)]
    d[s]=0
    q=[s]
    for i in range(n+1):
        for u,v,cost in edge:
                if d[v]>d[u]+cost:
                    d[v]=d[u]+cost
                    if i==n:
                        d[v]=-INF
    return d
d=bellmanford(1,n)
if d[n]==-INF:
    print(-1)
elif d[n]>=0:
    print(0)
else:
    print(-d[n])

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())
def LN(n):return [N() for i in range(n)]
def LL(n):return [L() for i in range(n)]
n,m,p=NM()
edge=[[] for i in range(n+1)]
for i in range(m):
    a,b,c=map(int,input().split())
    edge[a].append((b,p-c))
INF=float('inf')
def bellmanford(s,n):
    d = [INF for i in range(n+1)]
    renew=[0]*(n+1)
    d[s]=0
    q=[s]
    for i in range(n+1):
        for x,e in enumerate(edge):
            dist=d[x]
            for v,cost in e:
                if d[v]>dist+cost:
                    d[v]=dist+cost
                    if i==n:
                        d[v]=-INF
    return d
d=bellmanford(1,n)
if d[n]==-INF:
    print(-1)
elif d[n]>0:
    print(0)
else:
    print(-d[n])

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())
def LN(n):return [N() for i in range(n)]
def LL(n):return [L() for i in range(n)]
n,m,p=NM()
edge=[[] for i in range(n+1)]
for i in range(m):
    a,b,c=map(int,input().split())
    edge[a].append((b,p-c))
renew=[0]*(n+1)
INF=float('inf')
def bellmanford(s,n):
    d = [INF for i in range(n+1)]
    d[s]=0
    q=[s]
    while q:
        x=q.pop()
        dist=d[x]
        for v,cost in edge[x]:
                if d[v]>dist+cost:
                    d[v]=dist+cost
                    q.append(v)
                    renew[v]+=1
                    if renew[v]>n:
                        d[v]=-INF
    return d
d=bellmanford(1,n)
if d[n]==-INF:
    print(-1)
elif d[n]>0:
    print(0)
else:
    print(-d[n])

Submission status: Wrong Answer

Solution code:
n,m,p=map(int,input().split())
abc=[]
for i in range(m):
    a,b,c=map(int,input().split())
    abc.append([a-1,b-1,-c])
abc.sort()
INF=10**18
d=[INF]*n
chk=[False]*n
d[0]=0
for i in range(n-1):
    for a,b,c in abc:
        if d[b]>d[a]+c+p:
            d[b]=d[a]+c+p
for i in range(n):
    for a,b,c in abc:
        if d[b]>d[a]+c+p:
            d[b]=d[a]+c+p
            chk[b]=True
            chk[a]=True
if chk[n-1]==True:
    print(-1)
else:
    print(max(0,-d[n-1]))

Submission status: Wrong Answer

Solution code:
n,m,p=map(int,input().split())
abc=[]
for i in range(m):
    a,b,c=map(int,input().split())
    abc.append([min(a-1,b-1),max(a-1,b-1),-c])
abc.sort()
INF=10**18
d=[INF]*n
chk=[False]*n
d[0]=0
for i in range(n-1):
    for a,b,c in abc:
        if d[b]>d[a]+c+p:
            d[b]=d[a]+c+p
for i in range(n):
    for a,b,c in abc:
        if d[b]>d[a]+c+p:
            d[b]=d[a]+c+p
            chk[b]=True
            chk[a]=True
if chk[n-1]==True:
    print(-1)
else:
    print(max(0,-d[n-1]))

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
edge = []

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

d = [float("inf") for _ in range(n)]
d[0] = 0

# check = [0 for _ in range(n)]

for i in range(n+1):
    for now, next, weight in edge:
        if d[next] > d[now] + weight + p:
            d[next] = d[now] + weight + p
            # n+1回目で更新された場合はループしているのでチェックを入れる
            if i > n-1:
                d[next] = -float("inf")
                # check[next] = 1
        if i == n-1:
            prev = d[-1]
            # if check[now]:
            #     check[next] = 1
        # print(d)

# print(check)
# if check[-1]:
if prev != d[-1]:
    print("-1")
else:
    print(max(-prev, 0))
    # print(prev)

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
edge = []

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

d = [float("inf") for _ in range(n)]
d[0] = 0

# check = [0 for _ in range(n)]

for i in range(n*2):
    for now, next, weight in edge:
        if d[next] > d[now] + weight + p:
            d[next] = d[now] + weight + p
            # n+1回目で更新された場合はループしているのでチェックを入れる
            if i == n-1:
                d[next] = -float("inf")
                # check[next] = 1
        if i == n-1:
            prev = d[-1]
            # if check[now]:
            #     check[next] = 1
        # print(d)

# print(check)
# if check[-1]:
if prev != d[-1]:
    print("-1")
else:
    print(max(-d[-1], 0))

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
edge = []

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

d = [float("inf") for _ in range(n)]
d[0] = 0

check = [0 for _ in range(n)]

for i in range(n):
    for now, next, weight in edge:
        if d[next] > d[now] + weight + p:
            d[next] = d[now] + weight + p
            # n+1回目で更新された場合はループしているのでチェックを入れる
            if i == n-1:
                d[next] = -float("inf")
                check[next] = 1
        if i == n-1:
            if check[now]:
                check[next] = 1
        # print(d)

# print(check)
if check[-1]:
    print("-1")
else:
    print(max(-d[-1], 0))

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
edge = []

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

d = [float("inf") for _ in range(n)]
d[0] = 0

check = [0 for _ in range(n)]

for i in range(n):
    for now, next, weight in edge:
        if d[next] > min(d[now] + weight + p, d[next]):
            d[next] = min(d[now] + weight + p, d[next])
            # n+1回目で更新された場合はループしているのでチェックを入れる
            if i == n-1:
                check[next] = 1
        if i == n-1:
            if check[now]:
                check[next] = 1
        # print(d)

# print(check)
if check[-1]:
    print("-1")
else:
    print(max(-d[-1], 0))

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
edge = []

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

d = [float("inf") for _ in range(n)]
d[0] = 0

check = [0 for _ in range(n)]

for i in range(n):
    for now, next, weight in edge:
        if d[next] > d[now] + weight + p:
            d[next] = d[now] + weight + p
            # n+1回目で更新された場合はループしているのでチェックを入れる
            if i == n-1:
                check[next] = 1
        if i == n-1:
            if check[now]:
                check[next] = 1
        # print(d)

# print(check)
if check[-1]:
    print("-1")
else:
    print(max(-d[-1], 0))

Submission status: Wrong Answer

Solution code:
from collections import deque
def use_path(n,start,es):
    q = deque()
    chk = [False] * n
    q.append(start)
    chk[start] = True
    used = {start}
    while len(q) > 0:
        node = q.popleft()
        for nex in es[node]:
            if not chk[nex]:
                chk[nex] = True
                q.append(nex)
                used.add(nex)
    return used
  
def belman(s,g,n,es):
    d = [10**10] * n
    d[s] = 0
    fin = False
    cnt = 0
    while True:
      update = False
      for p,q,r in es:
        if d[p] != 10**10 and d[q] > d[p] + r:
          d[q] = d[p] + r
          update = True
          cnt += 1
      if not update:
        fin = True
        break
      if cnt > n+1:
          break      
    if fin:
        return max(0,-d[g])
    else:
        return -1   
  
n,m,p = map(int, input().split())

l = [[] for i in range(n)]
r = [[] for i in range(n)]
edges = []

for i in range(m):
    a,b,c = map(int, input().split())
    a -= 1
    b -= 1
    l[a].append(b)
    r[b].append(a)
    edges.append((a,b,-c+p))
    
use_nodes = use_path(n,0,l) & use_path(n,n-1,r)
es = [(a,b,c) for a,b,c in edges if a in use_nodes and b in use_nodes]
print(belman(0,n-1,n,es))

Submission status: Wrong Answer

Solution code:
from collections import deque
def use_path(n,start,es):
    q = deque()
    chk = [False] * n
    q.append(start)
    chk[start] = True
    used = {start}
    while len(q) > 0:
        node = q.popleft()
        for nex in es[node]:
            if not chk[nex]:
                chk[nex] = True
                q.append(nex)
                used.add(nex)
    return used
  
def belman(s,g,n,es):
    d = [10**10] * n
    d[s] = 0
    fin = False
    cnt = 0
    while True:
      update = False
      for p,q,r in es:
        if d[p] != 10**10 and d[q] > d[p] + r:
          d[q] = d[p] + r
          update = True
          cnt += 1
      if not update:
        fin = True
        break
      if cnt > n:
          break      
    if fin:
        return max(0,-d[g])
    else:
        return -1   
  
n,m,p = map(int, input().split())

l = [[] for i in range(n)]
r = [[] for i in range(n)]
edges = []

for i in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    l[a].append(b)
    r[b].append(a)
    edges.append((a, b, -(c-p)))
    
use_nodes = use_path(n,0,l) & use_path(n,n-1,r)
es = [(a,b,c) for a,b,c in edges if a in use_nodes and b in use_nodes]
print(belman(0,n-1,n,es))

Submission status: Wrong Answer

Solution code:
INF = 10 ** 15
MOD = 10 ** 9 + 7

def beruman(n,edges,start = 0,negative_loop = False):
    ans = False #startを含む負の閉路が存在するか
    dist = [INF] * n
    dist[start] = 0

    for i in range(n):
        update = False
        for a,b,c in edges:
            if dist[a] != INF and dist[b] > dist[a] + c:
                dist[b] = dist[a] + c
                update = True

        if not update:
            break
    
    if n != 1 and i == n - 1:
        ans = True
    
    if negative_loop:
        return dist,ans
    else:
        return dist

def dfs(G,can_reach,start):
    stack = [start]
    can_reach[start] = True
    while stack:
        v = stack.pop()
        for e in G[v]:
            if can_reach[e]:
                continue
            stack.append(e)
            can_reach[e] = True
    return can_reach

def main():
    N,M,P = map(int,input().split())
    edges = []
    G = [[] for _ in range(N)]
    G_inv = [[] for _ in range(N)]
    for _ in range(M):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        edges.append((a,b,P - c))
        G[a].append(b)
        G_inv[b].append(a)
    
    can_reach_from_start = [False] * N
    can_reach_from_goal = [False] * N
    dfs(G,can_reach_from_start,0)
    dfs(G_inv,can_reach_from_goal,N - 1)
    edge = []
    for a,b,c in edges:
        if (can_reach_from_goal[a] and can_reach_from_start[a]
            and can_reach_from_start[b] and can_reach_from_goal[b]):
            edge.append((a,b,c))
    
    dist,flag = beruman(N,edge,0,True)
    if flag:
        print(-1)
    else:
        print(max(-dist[-1],0))
if __name__ == '__main__':
    main()