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:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, 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 S(): return input().rstrip()
def LS(): return S().split()
def IR(n):
    res = [None] * n
    for i in range(n):
        res[i] = II()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI()
    return res
def FR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR_(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI_()
    return res
def SR(n):
    res = [None] * n
    for i in range(n):
        res[i] = S()
    return res
def LSR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LS()
    return res
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n, m, p = LI()
    edg = [[] for i in range(n)]
    for _ in range(m):
        a, b, c = LI_()
        c += 1
        edg[a].append((b, c))
    dist = [inf] * n
    q = [(0, 0)]
    d = defaultdict(int)
    dist[0] = 0
    while q:
        score, i = heappop(q)
        for e, s in edg[i]:
            if d[(e,i)] > n:
                if -inf < dist[e]:
                    dist[e] = -inf
                    heappush(q, (-inf, e))
            else:
                if score - s + p < dist[e]:
                    dist[e] = score - s + p
                    heappush(q,(score - s + p, e))
                    d[(e,i)] += 1
    if abs(dist[-1]) == inf:
        print(-1)
    else:
        print(abs(min(dist[-1], 0)))
    return


#main
if __name__ == '__main__':
    solve()

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, 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 S(): return input().rstrip()
def LS(): return S().split()
def IR(n):
    res = [None] * n
    for i in range(n):
        res[i] = II()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI()
    return res
def FR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR_(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI_()
    return res
def SR(n):
    res = [None] * n
    for i in range(n):
        res[i] = S()
    return res
def LSR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LS()
    return res
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n, m, p = LI()
    edg = [[] for i in range(n)]
    for _ in range(m):
        a, b, c = LI_()
        c += 1
        edg[a].append((b, c))
    dist = [inf] * n
    q = [(0, 0)]
    d = defaultdict(int)
    dist[0] = 0
    while q:
        score, i = heappop(q)
        for e, s in edg[i]:
            if d[e] > n:
                if -inf < dist[e]:
                    dist[e] = -inf
                    heappush(q, (-inf, e))
                    d[e] += 1
            else:
                if score - s + p < dist[e]:
                    dist[e] = score - s + p
                    heappush(q,(score - s + p, e))
                    d[e] += 1
    if abs(dist[-1]) == inf:
        print(-1)
    else:
        print(abs(min(dist[-1], 0)))
    return


#main
if __name__ == '__main__':
    solve()

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, 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 S(): return input().rstrip()
def LS(): return S().split()
def IR(n):
    res = [None] * n
    for i in range(n):
        res[i] = II()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI()
    return res
def FR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR_(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI_()
    return res
def SR(n):
    res = [None] * n
    for i in range(n):
        res[i] = S()
    return res
def LSR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LS()
    return res
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n, m, p = LI()
    edg = [[] for i in range(n)]
    for _ in range(m):
        a, b, c = LI_()
        c += 1
        edg[a].append((b, c))
    ans = [0] * n
    dp = [-inf] * n
    dp[0] = 0
    for j in range(m):
        ndp = [-inf] * n
        for i in range(n):
            score = dp[i]
            for e, s in edg[i]:
                if ndp[e] < score + s - p:
                    ndp[e] = score + s - p
                    if ans[e] < ndp[e]:
                        ans[e] = ndp[e] 
        dp = ndp
    ans2 = ans[:]
    for j in range(m):
        ndp = [-inf] * n
        for i in range(n):
            score = dp[i]
            for e, s in edg[i]:
                if ndp[e] < score + s - p:
                    ndp[e] = score + s - p
                    if ans2[e] < ndp[e]:
                        ans2[e] = ndp[e]
        dp = ndp

    if ans[-1] != ans2[-1]:
        print(-1)
    else:
        print(ans[-1])
    return


#main
if __name__ == '__main__':
    solve()

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, 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 S(): return input().rstrip()
def LS(): return S().split()
def IR(n):
    res = [None] * n
    for i in range(n):
        res[i] = II()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI()
    return res
def FR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR_(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI_()
    return res
def SR(n):
    res = [None] * n
    for i in range(n):
        res[i] = S()
    return res
def LSR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LS()
    return res
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n, m, p = LI()
    edg = [[] for i in range(n)]
    for _ in range(m):
        a, b, c = LI_()
        c += 1
        edg[a].append((b, c))
    ans = [0] * n
    dp = [-inf] * n
    dp[0] = 0
    for j in range(m):
        ndp = [-inf] * n
        for i in range(n):
            score = dp[i]
            for e, s in edg[i]:
                if ndp[e] < score + s - p:
                    ndp[e] = score + s - p
                    if ans[e] < ndp[e]:
                        ans[e] = ndp[e] 
        dp = ndp
    ans2 = ans[:]
    for j in range(m):
        ndp = [-inf] * n
        for i in range(n):
            score = dp[i]
            for e, s in edg[i]:
                if ndp[e] < score + s - p:
                    ndp[e] = score + s - p
                    if ans2[e] < ndp[e]:
                        ans2[e] = ndp[e]

    if ans[-1] != ans2[-1]:
        print(-1)
    else:
        print(ans[-1])
    return


#main
if __name__ == '__main__':
    solve()

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, 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 S(): return input().rstrip()
def LS(): return S().split()
def IR(n):
    res = [None] * n
    for i in range(n):
        res[i] = II()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI()
    return res
def FR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR_(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI_()
    return res
def SR(n):
    res = [None] * n
    for i in range(n):
        res[i] = S()
    return res
def LSR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LS()
    return res
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n, m, p = LI()
    edg = [[] for i in range(n)]
    for _ in range(m):
        a, b, c = LI_()
        c += 1
        edg[a].append((b, c))
    ans = [0] * n
    dp = [-inf] * n
    dp[0] = 0
    for j in range(m):
        ndp = [-inf] * n
        for i in range(n):
            score = dp[i]
            for e, s in edg[i]:
                if ndp[e] < score + s - p:
                    ndp[e] = score + s - p
                    if ans[e] < ndp[e]:
                        ans[e] = ndp[e] 
        dp = ndp
    ans2 = ans[:]
    for j in range(m):
        ndp = [-inf] * n
        for i in range(n):
            score = dp[i]
            for e, s in edg[i]:
                if ndp[e] < score + s - p:
                    ndp[e] = score + s - p
                    if ans2[e] < ndp[e]:
                        ans2[e] = dp[e]
    if ans[-1] != ans2[-1]:
        print(-1)
    else:
        print(ans[-1])
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
S=sys.stdin.readlines()
N,M,P=map(int,S[0].split())
G=[]
a,b,c=0,0,0
for i in range(M):
  a,b,c=map(int,S[i+1].split())
  G.append((a-1,b-1,P-c))
INF=10**15
D=[INF]*N
D[0]=0
e=0
for i in range(2*max(N,M)):
  for j in range(M):
    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]
X=D[N-1]
for i in range(2*max(N,M)):
  for j in range(M):
    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 X<=D[N-1]:
  print(max(0,-X))
else:
  print(-1)

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, 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 S(): return input().rstrip()
def LS(): return S().split()
def IR(n):
    res = [None] * n
    for i in range(n):
        res[i] = II()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI()
    return res
def FR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR_(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI_()
    return res
def SR(n):
    res = [None] * n
    for i in range(n):
        res[i] = S()
    return res
def LSR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LS()
    return res
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n, m, p = LI()
    edg = [[] for i in range(n)]
    for _ in range(m):
        a, b, c = LI_()
        c += 1
        edg[a].append((b, c))
    dist = [inf] * n
    q = [(0, 0)]
    d = defaultdict(int)
    dist[0] = 0
    while q:
        score, i = heappop(q)
        for e, s in edg[i]:
            if d[e] >= n:
                if -inf < dist[e]:
                    dist[e] = -inf
                    heappush(q, (-inf, e))
                    d[e] += 1
            else:
                if score - s + p < dist[e]:
                    dist[e] = score - s + p
                    heappush(q,(score - s + p, e))
                    d[e] += 1
    if abs(dist[-1]) == inf:
        print(-1)
    else:
        print(abs(min(dist[-1], 0)))
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
S=sys.stdin.readlines()
N,M,P=map(int,S[0].split())
G=[]
a,b,c=0,0,0
for i in range(M):
  a,b,c=map(int,S[i+1].split())
  G.append((a-1,b-1,P-c))
INF=10**15
D=[INF]*N
D[0]=0
e=0
for i in range(2*max(N,M)):
  for j in range(M):
    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]
X=D[N-1]
for i in range(2*max(N,M)):
  for j in range(M):
    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 X==D[N-1]:
  print(max(0,-X))
else:
  print(-1)

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, 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 S(): return input().rstrip()
def LS(): return S().split()
def IR(n):
    res = [None] * n
    for i in range(n):
        res[i] = II()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI()
    return res
def FR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR_(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI_()
    return res
def SR(n):
    res = [None] * n
    for i in range(n):
        res[i] = S()
    return res
def LSR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LS()
    return res
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n, m, p = LI()
    edg = [[] for i in range(n)]
    for _ in range(m):
        a, b, c = LI_()
        c += 1
        edg[a].append((b, c))
    dist = [inf] * n
    q = [(0, 0)]
    d = defaultdict(int)
    dist[0] = 0
    while q:
        score, i = heappop(q)
        for e, s in edg[i]:
            if d[(e, i)]:
                if -inf < dist[e]:
                    dist[e] = -inf
                    heappush(q, (-inf, e))
                    d[(e, i)] = 1
            else:
                if score - s + p < dist[e]:
                    dist[e] = score - s + p
                    heappush(q,(score - s + p, e))
                    d[(e, i)] = 1
    if abs(dist[-1]) == inf:
        print(-1)
    else:
        print(abs(min(dist[-1], 0)))
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
S=sys.stdin.readlines()
N,M,P=map(int,S[0].split())
G=[]
a,b,c=0,0,0
for i in range(M):
  a,b,c=map(int,S[i+1].split())
  G.append((a-1,b-1,P-c))
INF=10**15
D=[INF]*N
D[0]=0
e=0
for i in range(M-1):
  for j in range(M):
    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]
X=D[N-1]
for j in range(M):
  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 X==D[N-1]:
  print(max(0,-X))
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
import sys
S=sys.stdin.readlines()
N,M,P=map(int,S[0].split())
G=[]
a,b,c=0,0,0
for i in range(M):
  a,b,c=map(int,S[i+1].split())
  G.append((a-1,b-1,P-c))
INF=10**15
D=[INF]*N
D[0]=0
e=0
for i in range(N-1):
  for j in range(M):
    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]
X=D[N-1]
for j in range(M):
  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 X==D[N-1]:
  print(max(0,-X))
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

N, M, P = map(int, input().split())
d = defaultdict(list)
e = defaultdict(list)
cd = defaultdict(list)
l = []

for _ in range(M):
    a, b, c = map(int, input().split())
    if a==b:
        cd[a].append(P-c)
    else:
        l.append((a, b, P-c))
        d[a].append(b)
        e[b].append(a)

def check_visited(d, start):
    visited = [False]*(N+1)
    visited[start] = True
    q = deque([start])
    while q:
        s = q.popleft()
        for t in d[s]:
            if visited[t]:continue
            visited[t] = True
            q.append(t)
    
    s = set()
    for i in range(1, N+1):
        if visited[i]:
            s.add(i)
    return s

def shortest_path(s):
    INF = 10**18
    dist = [INF]*(N+1)
    dist[s] = 0
    neg = False

    for i in range(N):
        for a, b, c in l:
            if dist[a]!=INF and dist[b]>dist[a]+c:
                dist[b] = dist[a]+c
                if i==N-1:
                    neg = True
    return dist, neg

add_l = check_visited(d, 1)&check_visited(e, N)
for i in add_l:
    for j in cd[i]:
        l.append((i, i, j))
d1, neg = shortest_path(1)
if neg:
    print(-1)
elif d1[N]<0:
    print(-d1[N])
else:
    print(0)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

N, M, P = map(int, input().split())
d = defaultdict(list)
e = defaultdict(list)
cd = defaultdict(list)
l = []

for _ in range(M):
    a, b, c = map(int, input().split())
    if a==b:
        cd[a].append(P-c)
    else:
        l.append((a, b, P-c))
        d[a].append(b)
        e[b].append(a)

def check_visited(d, start):
    visited = [False]*(N+1)
    visited[start] = True
    q = deque([start])
    while q:
        s = q.popleft()
        for t in d[s]:
            if visited[t]:continue
            visited[t] = True
            q.append(t)
    
    s = set()
    for i in range(1, N+1):
        if visited[i]:
            s.add(i)
    return s

def shortest_path(s):
    INF = 10**18
    dist = [INF]*(N+1)
    dist[s] = 0
    size = len(l)
    neg = False

    for i in range(size):
        for a, b, c in l:
            if dist[a]!=INF and dist[b]>dist[a]+c:
                dist[b] = dist[a]+c
                if i==size-1:
                    neg = True
    return dist, neg

add_l = check_visited(d, 1)&check_visited(e, N)
for i in add_l:
    for j in cd[i]:
        l.append((i, i, j))
d1, neg = shortest_path(1)
if neg:
    print(-1)
elif d1[N]<0:
    print(-d1[N])
else:
    print(0)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

N, M, P = map(int, input().split())
d = defaultdict(list)
candidate = []
l = []

for _ in range(M):
    a, b, c = map(int, input().split())
    
    if a==b:
        candidate.append((P-c, a))
    else:
        l.append((a, b, P-c))
        d[a].append((P-c, b))

if candidate:
    visited = [False]*(N+1)
    visited[1] = True
    q = deque([1])
    while q:
        s = q.popleft()
        for _, t in d[s]:
            if visited[t]:continue
            visited[t] = True
            q.append(t)
    
    candidate2 = [i for i in candidate if visited[i[1]]]
    for i in candidate2:
        visited = [False]*(N+1)
        visited[i[1]] = True
        q = deque([i[1]])
        while q:
            s = q.popleft()
            for _, t in d[s]:
                if visited[t]:continue
                visited[t] = True
                q.append(t)
        if visited[N]:
            d[i[1]].append(i)
            l.append((i[1], i[1], i[0]))


def shortest_path(s):
    INF = 10**18
    dist = [INF]*(N+1)
    dist[s] = 0
    size = len(l)
    neg = False

    for i in range(size):
        for a, b, c in l:
            if dist[a]!=INF and dist[b]>dist[a]+c:
                dist[b] = dist[a]+c
                if i==size-1:
                    neg = True
    return dist, neg

d1, neg = shortest_path(1)
if neg:
    print(-1)
elif d1[N]<0:
    print(-d1[N])
else:
    print(0)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

N, M, P = map(int, input().split())
d = defaultdict(list)
candidate = []
l = []

for _ in range(M):
    a, b, c = map(int, input().split())
    
    if a==b:
        candidate.append((P-c, a))
    else:
        l.append((a, b, P-c))
        d[a].append((P-c, b))

if candidate:
    visited = [False]*(N+1)
    visited[1] = True
    q = deque([1])
    while q:
        s = q.popleft()
        for _, t in d[s]:
            if visited[t]:continue
            visited[t] = True
            q.append(t)
    
    candidate2 = [i for i in candidate if visited[i[1]]]
    for i in candidate2:
        visited = [False]*(N+1)
        visited[i[1]] = True
        q = deque([i[1]])
        while q:
            s = q.popleft()
            for _, t in d[s]:
                if visited[t]:continue
                visited[t] = True
                q.append(t)
        if visited[N] or visited[1]:
            d[i[1]].append(i)
            l.append((i[1], i[1], i[0]))


def shortest_path(s):
    INF = 10**18
    dist = [INF]*(N+1)
    dist[s] = 0
    size = len(l)
    neg = False

    for i in range(size):
        for a, b, c in l:
            if dist[a]!=INF and dist[b]>dist[a]+c:
                dist[b] = dist[a]+c
                if i==size-1:
                    neg = True
    return dist, neg

d1, neg = shortest_path(1)
if neg:
    print(-1)
elif d1[N]<0:
    print(-d1[N])
else:
    print(0)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

N, M, P = map(int, input().split())
d = defaultdict(list)
candidate = []
l = []

for _ in range(M):
    a, b, c = map(int, input().split())
    
    if a==b:
        if P-c<0:
            candidate.append((P-c, a))
    else:
        l.append((a, b, P-c))
        d[a].append((P-c, b))

if candidate:
    visited = [False]*(N+1)
    visited[1] = True
    q = deque([1])
    while q:
        s = q.popleft()
        for _, t in d[s]:
            if visited[t]:continue
            visited[t] = True
            q.append(t)
    
    candidate2 = [i for i in candidate if visited[i[1]]]
    for i in candidate2:
        visited = [False]*(N+1)
        visited[i[1]] = True
        q = deque([i[1]])
        while q:
            s = q.popleft()
            for _, t in d[s]:
                if visited[t]:continue
                visited[t] = True
                q.append(t)
        if visited[N] or visited[1]:
            d[i[1]].append(i)
            l.append((i[1], i[1], i[0]))


def shortest_path(s):
    INF = 10**18
    dist = [INF]*(N+1)
    dist[s] = 0
    size = len(l)
    neg = False

    for i in range(size):
        for a, b, c in l:
            if dist[a]!=INF and dist[b]>dist[a]+c:
                dist[b] = dist[a]+c
                if i==size-1:
                    neg = True
    return dist, neg

d1, neg = shortest_path(1)
if neg:
    print(-1)
elif d1[N]<0:
    print(-d1[N])
else:
    print(0)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

N, M, P = map(int, input().split())
d = defaultdict(list)
candidate = []
l = []

for _ in range(M):
    a, b, c = map(int, input().split())
    
    if a==b:
        if P-c<0:
            candidate.append(a)
    else:
        l.append((a, b, P-c))
        d[a].append((P-c, b))

def check():
    if candidate:
        visited = [False]*(N+1)
        visited[1] = True
        q = deque([1])
        while q:
            s = q.popleft()
            for _, t in d[s]:
                if visited[t]:continue
                visited[t] = True
                q.append(t)
        
        candidate2 = [i for i in candidate if visited[i]]
        for i in candidate2:
            visited = [False]*(N+1)
            visited[i] = True
            q = deque([i])
            while q:
                s = q.popleft()
                for _, t in d[s]:
                    if visited[t]:continue
                    visited[t] = True
                    q.append(t)
            if visited[N] or visited[1]:
                return False
    return True

def shortest_path(s):
    INF = 10**18
    dist = [INF]*(N+1)
    dist[s] = 0

    while 1:
        update = False

        for a, b, c in l:
            if dist[a]!=INF and dist[b]>dist[a]+c:
                dist[b] = dist[a]+c
        if not update:
            break
    return dist

if check():
    d1 = shortest_path(1)
    dN = shortest_path(N)
    INF = 10**18

    if dN[1]!=INF and d1[N]+dN[1]<0:
        print(-1)
    elif d1[N]<0:
        print(-d1[N])
    else:
        print(0)


else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

N, M, P = map(int, input().split())
d = defaultdict(list)
candidate = []
l = []

for _ in range(M):
    a, b, c = map(int, input().split())
    
    if a==b:
        if P-c<0:
            candidate.append(a)
    else:
        l.append((a, b, P-c))
        d[a].append((P-c, b))

def check():
    if candidate:
        visited = [False]*(N+1)
        visited[1] = True
        q = deque([1])
        while q:
            s = q.popleft()
            for _, t in d[s]:
                if visited[t]:continue
                visited[t] = True
                q.append(t)
        
        candidate2 = [i for i in candidate if visited[i]]
        for i in candidate2:
            visited = [False]*(N+1)
            visited[i] = True
            q = deque([i])
            while q:
                s = q.popleft()
                for _, t in d[s]:
                    if visited[t]:continue
                    visited[t] = True
                    q.append(t)
            if visited[N]:
                return False
    return True

def shortest_path(s):
    INF = 10**18
    dist = [INF]*(N+1)
    dist[s] = 0

    while 1:
        update = False

        for a, b, c in l:
            if dist[a]!=INF and dist[b]>dist[a]+c:
                dist[b] = dist[a]+c
        if not update:
            break
    return dist

if check():
    d1 = shortest_path(1)
    dN = shortest_path(N)
    INF = 10**18

    if dN[1]!=INF and d1[N]+dN[1]<0:
        print(-1)
    elif d1[N]<0:
        print(-d1[N])
    else:
        print(0)
else:
    print(-1)