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:
import sys
input = sys.stdin.readline

sys.setrecursionlimit(1000000)

N,M,P = map(int,input().split())
to = [[] for _ in range(N+1)]
for i in range(M):
    a,b,c = map(int,input().split())
    if a==b==N:
        print(-1)
        sys.exit()
    if a==b: continue
    to[a].append([b,c])
ans = 0
done = [0]*(N+1)
def dfs(v, time, score):
    global ans
    if done[v] == 1:
        if done[N] == 1:
            if score-P*time > 0:
                print(-1)
                sys.exit()
            else:
                return
        else:
            return
    if v==N:
        ans = max(ans, score-P*time)
    done[v] = 1
    for u, c in to[v]:
        dfs(u,time+1,score+c)
        done[u] = 0

dfs(1,0,0)
print(ans)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = P-c
INF = float('inf')

dist = {i: INF for i in range(1, N+1)}
dist[1] = 0
ans = 0
for i in range(N*2):
    for uv, c in E.items():
        u, v = uv
        if dist[v] > dist[u] + c:
            dist[v] = dist[u] + c
            if i >= N:
                dist[v] = -INF
    if i == N-1:
        ans = dist[N]

if ans != dist[N]:
    print(-1)
elif ans > 0:
    print(0)
else:
    print(-ans)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = P-c
INF = float('inf')

dist = {i: INF for i in range(1, N+1)}
dist[1] = 0
ans = 0
for i in range(N*2):
    for uv, c in E.items():
        u, v = uv
        if dist[v] > dist[u] + c:
            dist[v] = dist[u] + c
            if i >= N:
                dist[v] = -INF
    if i == N-1:
        ans = dist[N]

print(max(0, -dist[N]) if ans == dist[N] else -1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = c-P
INF = float('inf')

dist = {i: -INF for i in range(1, N+1)}
dist[1] = 0
ans = 0
for i in range(N*2):
    for uv, c in E.items():
        u, v = uv
        if dist[v] < dist[u] + c:
            dist[v] = dist[u] + c
            if i >= N:
                dist[v] = INF
    if i == N-1:
        ans = dist[N]

print(max(0, dist[N]) if ans == dist[N] else -1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = c-P
INF = float('inf')

dist = {i: -INF for i in range(1, N+1)}
dist[1] = 0
ans = 0
for i in range(N*2):
    for uv, c in E.items():
        u, v = uv
        if dist[v] < dist[u] + c:
            dist[v] = dist[u] + c
            if i >= N:
                dist[v] = -INF
    if i == N-1:
        ans = dist[N]

print(max(0, dist[N]) if ans == dist[N] else -1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = c-P
INF = float('inf')

dist = {i: -INF for i in range(1, N+1)}
prev = {i: 0 for i in range(1, N+1)}
dist[1] = 0
for _ in range(N):
    for uv, c in E.items():
        u, v = uv
        if dist[v] < dist[u] + c:
            dist[v] = dist[u] + c
            prev[v] = u

v = N
infloop = False
if prev[1] != 0:
    infloop = True
while v != 1 and not infloop:
    u = prev[v]
    if dist[v] == -INF or dist[u] + E[(u, v)] > dist[v]:
        infloop = True
    v = u

print(max(0, dist[N]) if not infloop else -1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = c-P
INF = float('inf')

dist = {i: -INF for i in range(1, N+1)}
prev = {i: 0 for i in range(1, N+1)}
dist[1] = 0
for _ in range(N):
    for uv, c in E.items():
        u, v = uv
        if dist[u] > -INF and dist[v] < dist[u] + c:
            dist[v] = dist[u] + c
            prev[v] = u

v = N
infloop = False
#while v != 1:
#    u = prev[v]
#    if dist[v] == -INF or dist[u] + E[(u, v)] > dist[v]:
#        infloop = True
#        break
#    v = u

print(max(0, dist[N]) if not infloop else -1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = c-P
INF = float('inf')

dist = {i: -INF for i in range(1, N+1)}
prev = {i: 0 for i in range(1, N+1)}
dist[1] = 0
for _ in range(N):
    for uv, c in E.items():
        u, v = uv
        if dist[v] < dist[u] + c:
            dist[v] = dist[u] + c
            prev[v] = u

v = N
infloop = False
#while v != 1:
#    u = prev[v]
#    if dist[v] == -INF or dist[u] + E[(u, v)] > dist[v]:
#        infloop = True
#        break
#    v = u

print(max(0, dist[N]) if not infloop else -1)

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

Solution code:
print(-1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = P-c
INF = float('inf')

dist = {i: INF for i in range(1, N+1)}
prev = {i: 0 for i in range(1, N+1)}
dist[1] = 0
for _ in range(N):
    for uv, c in E.items():
        u, v = uv
        if dist[v] > dist[u] + c:
            dist[v] = dist[u] + c
            prev[v] = u

v = N
ans = dist[N]
for _ in range(N):
    for uv, c in E.items():
        u, v = uv
        if dist[v] > dist[u] + c:
            dist[v] = dist[u] + c
            prev[v] = u

print(max(0, -dist[N]) if ans == dist[N] else -1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = P-c
INF = float('inf')

dist = {i: INF for i in range(1, N+1)}
prev = {i: 0 for i in range(1, N+1)}
dist[1] = 0
for _ in range(N):
    for uv, c in E.items():
        u, v = uv
        if dist[v] > dist[u] + c:
            dist[v] = dist[u] + c
            prev[v] = u

v = N
rec = 0  # reconstructed path
cnt = 0
while v != 1 and cnt < N:
    u = prev[v]
    rec += E[(u, v)]
    cnt += 1
    v = u

print(max(0, -dist[N]) if rec == dist[N] else -1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = P-c
INF = float('inf')

dist = {i: INF for i in range(1, N+1)}
prev = {i: 0 for i in range(1, N+1)}
dist[1] = 0
for _ in range(N):
    for uv, c in E.items():
        u, v = uv
        if dist[v] > dist[u] + c:
            dist[v] = dist[u] + c
            prev[v] = u

v = N
infloop = False
while v != 1:
    u = prev[v]
    if dist[v] == INF or dist[u] + E[(u, v)] < dist[v]:
        infloop = True
        break
    v = u

print(max(0, -dist[N]) if not infloop else -1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = c-P
INF = float('inf')

dist = {i: -INF for i in range(1, N+1)}
prev = {i: 0 for i in range(1, N+1)}
dist[1] = 0
for _ in range(N):
    for uv, c in E.items():
        u, v = uv
        if dist[v] < dist[u] + c:
            dist[v] = dist[u] + c
            prev[v] = u

v = N
infloop = False
while v != 1:
    u = prev[v]
    if dist[v] == -INF or dist[u] + E[(u, v)] > dist[v]:
        infloop = True
        break
    v = u

print(max(0, dist[N]) if not infloop else -1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = c-P
INF = float("inf")

dist = {i: -INF for i in range(1, N+1)}
prev = {i: 0 for i in range(1, N+1)}
dist[1] = 0
for _ in range(N):
    for uv, c in E.items():
        u, v = uv
        if dist[u] != -INF and dist[v] < dist[u] + c:
            dist[v] = dist[u] + c
            prev[v] = u

v = N
infloop = False
while v != 1:
    u = prev[v]
    if dist[u] + E[(u, v)] > dist[v]:
        infloop = True
        break
    v = u

print(max(0, dist[N]) if not infloop else -1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = c-P
INF = 10 ** 9

dist = {i: -INF for i in range(1, N+1)}
prev = {i: 0 for i in range(1, N+1)}
dist[1] = 0
for _ in range(N):
    for uv, c in E.items():
        u, v = uv
        if dist[u] != -INF and dist[v] < dist[u] + c:
            dist[v] = dist[u] + c
            prev[v] = u

v = N
infloop = False
while v != 1:
    u = prev[v]
    if dist[u] + E[(u, v)] > dist[v]:
        infloop = True
        break
    v = u

print(max(0, dist[N]) if not infloop else -1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
E = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    E[(a, b)] = c-P
INF = 10 ** 9

dist = {i: -INF for i in range(1, N+1)}
prev = {i: 0 for i in range(1, N+1)}
dist[1] = 0
for _ in range(N):
    for uv, c in E.items():
        u, v = uv
        if dist[v] < dist[u] + c:
            dist[v] = dist[u] + c
            prev[v] = u

v = N
infloop = False
while v != 1:
    u = prev[v]
    if dist[u] + E[(u, v)] > dist[v]:
        infloop = True
        break
    v = u

print(max(0, dist[N]) if not infloop else -1)

Submission status: Wrong Answer

Solution code:
def dfs(edges, start, end):
    g = {}
    for f,t,w in edges:
        if f in g: g[f].append(t)
        else: g[f] = [t]
    visiting = [start]
    visited = {}
    while visiting:
        v = visiting.pop()
        visited[v] = 1
        if v == end: return True
        for n in g.get(v, ()):
            if n in visited: continue
            if n in visiting: continue
            visiting.append(n)
    return False
M,N,P = map(int, input().split())

edges = []
for _ in range(N):
    a,b,c = map(int, input().split())
    edges.append((a,b,c))

neg_inf = float("-Inf")
no_answer = False

start = 1
end = M

distance = {}
distance[start] = 0
previous = {}

for i in range(M-1):
    for f,t,w in edges:
        if distance.get(f,neg_inf) + w - P > distance.get(t,neg_inf):
            distance[t] = distance[f] + w - P
            previous[t] = f

if dfs(edges,start,end) == False:
    print(-1)
    exit(0)

path = []
node = end
if node in previous:
    while node != start:
        if  node in path:
            no_answer = True
            break
        path.append(node)
        node = previous[node]
    path.append(start)

for f,t,w in edges:
    if distance.get(f,neg_inf) + w - P > distance.get(t,neg_inf) and t in path:
        no_answer = True
        break

if no_answer:
    print(-1)
else:
    print(max(0,distance[end]))

Submission status: Wrong Answer

Solution code:
def dfs(edges, start, end):
    g = {}
    for f,t,w in edges:
        if f in g: g[f].append(t)
        else: g[f] = [t]
    visiting = [start]
    visited = {}
    while visiting:
        v = visiting.pop()
        visited[v] = 1
        if v == end: return True
        for n in g.get(v, ()):
            if n in visited: continue
            if n in visiting: continue
            visiting.append(n)
    return False
M,N,P = map(int, input().split())

edges = []
for _ in range(N):
    a,b,c = map(int, input().split())
    edges.append((a,b,c))

neg_inf = float("-Inf")
no_answer = False

start = 1
end = M

distance = {}
distance[start] = 0
previous = {}

for i in range(M-1):
    for f,t,w in edges:
        if distance.get(f,neg_inf) + w - P > distance.get(t,neg_inf):
            distance[t] = distance[f] + w - P
            previous[t] = f

if dfs(edges,start,end) == False:
    print(-1)
    exit(0)

path = []
node = end
if node in previous:
    while node != start:
        path.append(node)
        node = previous[node]
        if  node in path:
            no_answer = True
            break
    path.append(start)

for f,t,w in edges:
    if distance.get(f,neg_inf) + w - P > distance.get(t,neg_inf) and t in path:
        no_answer = True
        break

if no_answer:
    print(-1)
else:
    print(max(0,distance[end]))

Submission status: Wrong Answer

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

edges = []
for _ in range(N):
    a,b,c = map(int, input().split())
    edges.append((a,b,c))

neg_inf = float("-Inf")
no_answer = False

start = 1
end = M

distance = {}
distance[start] = 0
previous = {}

for i in range(M-1):
    for f,t,w in edges:
        if distance.get(f,neg_inf) + w - P > distance.get(t,neg_inf):
            distance[t] = distance[f] + w - P
            previous[t] = f

path = []
node = end
if node in previous:
    while node != start:
        path.append(node)
        node = previous[node]
        if len(path) > M:
            no_answer = True
            break
    path.append(start)

for f,t,w in edges:
    if distance.get(f,neg_inf) + w - P > distance.get(t,neg_inf) and t in path:
        no_answer = True
        break

if no_answer:
    print(-1)
else:
    print(max(0,distance[end]))

Submission status: Wrong Answer

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

edges = []
for _ in range(N):
    a,b,c = map(int, input().split())
    edges.append((a,b,c))

neg_inf = float("-Inf")
no_answer = False

start = 1
end = M

distance = {}
distance[start] = 0
previous = {}

for i in range(M-1):
    for f,t,w in edges:
        if distance.get(f,neg_inf) + w - P > distance.get(t,neg_inf):
            distance[t] = distance[f] + w - P
            previous[t] = f

path = []
node = end
if node in previous:
    while node != start:
        path.append(node)
        node = previous[node]
        if node in path:
            no_answer = True
            break
    path.append(start)

for f,t,w in edges:
    if distance.get(f,neg_inf) + w - P > distance.get(t,neg_inf) and t in path:
        no_answer = True
        break

if no_answer:
    print(-1)
else:
    print(max(0,distance[end]))

Submission status: Wrong Answer

Solution code:
def BellmanFord(s,n,w,es):
  # s: start
  # n: number of nodes
  # w: number of edges
  # es[i]: from, to, cost
  
  # d: shortest dist of s -> i
  d = [float('inf')]*n
  d[s] = 0
  c = 0
  while True:
    c += 1
    update = False
    for p, q, r in es:
      if d[p] != float('inf') and (d[q] > d[p] + r):
        if c >= n:
          if q == n-1:
            return 'NEGATIVE CYCLE'
          else:
            break
        d[q] = d[p] + r
        update = True
    if not update:
      break
  return d

n, w, p = map(int, input().split())
es = []
for i in range(w):
  x, y, z = map(int, input().split())
  x, y = x-1, y-1
  z = (z-p)*(-1)
  es.append([x, y, z])
  
d = BellmanFord(0, n, w ,es)
if d != 'NEGATIVE CYCLE':
  print(min(0, d[-1])*(-1))
else:
  print('-1')

Submission status: Wrong Answer

Solution code:
def BellmanFord(s,n,w,es):
  # s: start
  # n: number of nodes
  # w: number of edges
  # es[i]: from, to, cost
  
  # d: shortest dist of s -> i
  d = [float('inf')]*n
  d[s] = 0
  c = 0
  while True:
    c += 1
    update = False
    for p, q, r in es:
      if d[p] != float('inf') and (d[q] > d[p] + r):
        if c >= w:
          if q == n-1:
            return 'NEGATIVE CYCLE'
          else:
            break
        d[q] = d[p] + r
        update = True
    if not update:
      break
  return d

n, w, p = map(int, input().split())
es = []
for i in range(w):
  x, y, z = map(int, input().split())
  x, y = x-1, y-1
  z = (z-p)*(-1)
  es.append([x, y, z])
  
d = BellmanFord(0, n, w,es)
if d != 'NEGATIVE CYCLE':
  print(min(0, d[-1])*(-1))
else:
  print('-1')

Submission status: Wrong Answer

Solution code:
def BellmanFord(s,n,w,es):
  # s: start
  # n: number of nodes
  # w: number of edges
  # es[i]: from, to, cost
  
  # d: shortest dist of s -> i
  d = [float('inf')]*n
  d[s] = 0
  c = 0
  while True:
    c += 1
    update = False
    for p, q, r in es:
      if d[p] != float('inf') and (d[q] > d[p] + r):
        d[q] = d[p] + r
        update = True
    if not update:
      break
    if c == w:
      return 'NEGATIVE CYCLE'
  return d

n, w, p = map(int, input().split())
es = []
for i in range(w):
  x, y, z = map(int, input().split())
  x, y = x-1, y-1
  z = (z-p)*(-1)
  es.append([x, y, z])
  
d = BellmanFord(0, n, w,es)
if d != 'NEGATIVE CYCLE':
  print(d[-1]*(-1))
else:
  print('-1')

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
edge = []
for i in range(m):
    ai, bi, ci = map(int, input().split())
    edge.append((bi-1,ai-1,-ci+p))
dist = [float("inf") for i in range(n)]
dist[n-1] = 0
h = False
for i in range(n):
    updated = False
    for e in edge:
        if dist[e[1]] > dist[e[0]] + e[2]:
            dist[e[1]] = dist[e[0]] + e[2]
            updated = True
    if not updated:
        break
else:
    h = True

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**4)

inf = float('inf')

# 計算量O(VE) V:頂点数, E:辺の数
# True - >正常に更新, False -> startからendまでの経路において負閉路有り
def bellmanford(start, end):
    global dist
    dist = [inf] * N
    dist[start] = 0
    # 頂点数Nなら更新は高々N-1回で済む
    for _ in range(N-1):
        for v ,nv, w in edges:
            if dist[nv] > dist[v] + w:
                dist[nv] = dist[v] + w
    # 負閉路の検出部分
    # N回目で更新があった場合、負閉路を含む
    for v, nv, w in edges:
        if dist[nv] > dist[v] + w:
            return 0
    return 1

# True -> 頂点vが1とNのパスに含まれる
def dfs(v):
    visited[v] = 1
    if bit[v] != -1:
        return bit[v]
    res = 0
    for nv, w in ad[v]:
        if not visited[nv]:
            res |= dfs(nv)
    bit[v] = res
    return res

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

visited = [0] * N
bit = [-1] * N
bit[N-1] = 1
dfs(0)
edges = [(v, nv, -w+P) for v, nv, w in edges if bit[v] == 1 and bit[nv] == 1]
flag = bellmanford(0, N-1)
ans = max(-dist[N-1], 0) if flag else -1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**4)

inf = float('inf')

# 計算量O(VE) V:頂点数, E:辺の数
# True - >正常に更新, False -> startからendまでの経路において負閉路有り
def bellmanford(start, end):
    global dist
    dist = [inf] * N
    dist[start] = 0
    # 頂点数Nなら更新は高々N-1回で済む
    for _ in range(N-1):
        for v ,nv, w in edges:
            if dist[nv] > dist[v] + w:
                dist[nv] = dist[v] + w
    # 負閉路の検出部分
    # N回目で更新があった場合、負閉路を含む
    for v, nv, w in edges:
        if dist[nv] > dist[v] + w:
            return False
    return True

# True -> 頂点vが1とNのパスに含まれる
def dfs(v):
    visited[v] = True
    res = False
    if v == N-1:
        res = True
        bit[v] = res
        return res
    for nv, w in ad[v]:
        if not visited[nv]:
            res |= dfs(nv)
    bit[v] = res
    return res

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

visited = [False] * N
bit = [False] * N
dfs(0)
edges = [(v, nv, -w+P) for v, nv, w in edges if bit[v] and bit[nv]]
flag = bellmanford(0, N-1)
ans = max(-dist[N-1], 0) if flag else -1
print(ans)

Submission status: Wrong Answer

Solution code:
inf = float('inf')

# 計算量O(VE) V:頂点数, E:辺の数
# True - >正常に更新, False -> startからendまでの経路において負閉路有り
def bellmanford(start, end):
    global dist
    dist = [inf] * N
    dist[start] = 0
    # 頂点数Nなら更新は高々N-1回で済む
    for _ in range(N-1):
        for v ,nv, w in edges:
            if dist[nv] > dist[v] + w:
                dist[nv] = dist[v] + w
    # 負閉路の検出部分
    # N回目にdist[end]の更新を行う <=> startからendのパスの中に負閉路が含まれる
    for v, nv, w in edges:
        if dist[nv] > dist[v] + w:
            dist[nv] = dist[v] + w
            if nv == end:
                return False
    return True

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

flag = bellmanford(0,N-1)
ans = max(-dist[N-1],0) if flag else -1
print(ans)

Submission status: Wrong Answer

Solution code:
inf = float('inf')

# 計算量O(VE) V:頂点数, E:辺の数
# True - >正常に更新, False -> startからendまでの経路において負閉路有り
def bellmanford(start, end):
    global dist
    dist = [inf] * N
    dist[start] = 0
    # 頂点数Nなら更新は高々N-1回で済む
    for _ in range(N-1):
        for v ,nv, w in edges:
            if dist[nv] > dist[v] + w:
                dist[nv] = dist[v] + w
    # 負閉路の検出部分
    # N回目に更新を行うならstartからendのパスの中に負閉路が含まれるということ
    for v, nv, w in edges:
        if nv == end and dist[nv] > dist[v] + w:
            return False
    return True

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

flag = bellmanford(0,N-1)
ans = max(-dist[N-1],0) if flag else -1
print(ans)

Submission status: Wrong Answer

Solution code:
inf = float('inf')

# 計算量O(VE) V:頂点数, E:辺の数
# True - >正常に更新, False -> startからendまでの経路において負閉路有り
def bellmanford(start, end):
    global dist
    dist = [inf for _ in range(N)]
    dist[start] = 0
    for i in range(N):
        for v ,nv, w in edges:
            w += P
            if dist[nv] > dist[v] + w:
                dist[nv] = dist[v] + w
                # 頂点数Nなら更新は高々N-1回で済む
                # N回目にendの更新を行うなら経路の中に負閉路があるということ
                if i == N-1 and nv == end:
                    return False
    return True

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

flag = bellmanford(0,N-1)
ans = max(-dist[N-1],0) if flag else -1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from queue import Queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000000)

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

A = [-1]*M
B = [-1]*M
C = [-1]*M
nodes_edges = [[] for _ in range(N)]
rev_nodes_edges = [[] for _ in range(N)]
coin = [-float('inf')]*N
coin[0] = 0
availables = set()

for edge in range(M):
    a, b, c = map(int, input().split())
    A[edge] = a-1
    B[edge] = b-1
    C[edge] = c-P
    nodes_edges[a-1].append(edge)
    rev_nodes_edges[b-1].append(edge)

def cut_graph(node):
    edges = rev_nodes_edges[node]
    availables.add(node)
    for edge in edges:
        prev = A[edge]
        if prev in availables:
            continue
        cut_graph(prev)

cut_graph(N-1)
q = Queue()
q.put(0)
count = 0
limit = M+1+len(availables)
while not q.empty():
    if count == limit:
        print(-1)
        break
    node = q.get()
    edges = nodes_edges[node]
    for edge in edges:
        nxt = B[edge]
        if nxt not in availables:
            continue
        new_coin = C[edge]+coin[node]
        if new_coin > coin[nxt]:
            coin[nxt] = new_coin
            q.put(nxt)
    count += 1
else:
    ans = coin[N-1]
    ans = 0 if ans < 0 else ans
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from queue import Queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000000)

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

A = [-1]*M
B = [-1]*M
C = [-1]*M
nodes_edges = [[] for _ in range(N)]
rev_nodes_edges = [[] for _ in range(N)]
coin = [-float('inf')]*N
coin[0] = 0
availables = set()

for edge in range(M):
    a, b, c = map(int, input().split())
    A[edge] = a-1
    B[edge] = b-1
    C[edge] = c-P
    nodes_edges[a-1].append(edge)
    rev_nodes_edges[b-1].append(edge)

def cut_graph(node):
    edges = rev_nodes_edges[node]
    availables.add(node)
    for edge in edges:
        prev = A[edge]
        if prev in availables:
            continue
        cut_graph(prev)

cut_graph(N-1)
q = Queue()
q.put(0)
count = 0
limit = M+1
while not q.empty():
    if count == limit:
        print(-1)
        break
    node = q.get()
    edges = nodes_edges[node]
    for edge in edges:
        nxt = B[edge]
        if nxt not in availables:
            continue
        new_coin = C[edge]+coin[node]
        if new_coin > coin[nxt]:
            coin[nxt] = new_coin
            q.put(nxt)
    count += 1
else:
    ans = coin[N-1]
    ans = 0 if ans < 0 else ans
    print(ans)

Submission status: Wrong Answer

Solution code:
# E - Coins Respawn
from collections import defaultdict

_input = list(map(int, input().split()))
n = _input[0]
m = _input[1]
p = _input[2]

# aから伸びるルート＋点数をマッピング
routes = defaultdict(list)
for i in range(m):
    __input = list(map(int, input().split()))
    routes[__input[0] - 1].append([__input[1] - 1, __input[2] - p])
    
# 重み付け経路探索(ベルマンフォード)
def berman(points, before):
    for i in range(n):
        for route in routes[i]:
            b = route[0]
            c = route[1]
            if points[i] + c >= points[b]:
                points[b] = points[i] + c
    return points[n - 1] == before[n - 1]

pre = [0] * n
points = [0] * n
cnt = -1
for j in range(n):
    ret = berman(points, pre)
    if ret:
        cnt = points[n - 1] if points[n - 1] > 0 else 0
        break
    pre = points[:]
    
print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
s_l = len(s)

wi = {}  
for n, _ in enumerate(s):
  wi[n] = {}
  for k, ws in enumerate(s):
    if not wi[n].get(ws):
      wi[n][ws] = [k-n if k-n > 0 else k-n+s_l]
    else:
      wi[n][ws].append(k-n if k-n > 0 else k-n+s_l)

ans = 0
x = 0
for b, wt in enumerate(t):
  if b == 0:
    x = s.find(wt)
    if x == -1:
      print(x)
      exit()
    else:
      ans = x + 1
  else:
    if not wi[x].get(wt):
      print(-1)
      exit()
    else:
      ans += wi[x][wt][0]
      x = (x + wi[x][wt][0]) % s_l

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from queue import Queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000000)

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

A = [-1]*M
B = [-1]*M
C = [-1]*M
nodes_edges = [[] for _ in range(N)]
rev_nodes_edges = [[] for _ in range(N)]
coin = [-float('inf')]*N
coin[0] = 0
availables = set()

for edge in range(M):
    a, b, c = map(int, input().split())
    A[edge] = a-1
    B[edge] = b-1
    C[edge] = c-P
    nodes_edges[a-1].append(edge)
    rev_nodes_edges[b-1].append(edge)

def cut_graph(node):
    edges = rev_nodes_edges[node]
    availables.add(node)
    for edge in edges:
        prev = A[edge]
        if prev in availables:
            continue
        cut_graph(prev)

cut_graph(N-1)
q = Queue()
q.put(0)
count = 0
while not q.empty():
    if count == len(availables)+1:
        print(-1)
        break
    node = q.get()
    edges = nodes_edges[node]
    for edge in edges:
        nxt = B[edge]
        if nxt not in availables:
            continue
        if C[edge]+coin[node] > coin[nxt]:
            coin[nxt] = C[edge]+coin[node]
            q.put(nxt)
    count += 1
else:
    ans = coin[N-1]
    ans = 0 if ans < 0 else ans
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from queue import Queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000000)

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

A = [-1]*M
B = [-1]*M
C = [-1]*M
nodes_edges = [[] for _ in range(N)]
rev_nodes_edges = [[] for _ in range(N)]
coin = [-float('inf')]*N
coin[0] = 0
availables = set()

for edge in range(M):
    a, b, c = map(int, input().split())
    A[edge] = a-1
    B[edge] = b-1
    C[edge] = c-P
    nodes_edges[a-1].append(edge)
    rev_nodes_edges[b-1].append(edge)

def cut_graph(node):
    edges = rev_nodes_edges[node]
    availables.add(node)
    for edge in edges:
        prev = A[edge]
        if prev in availables:
            continue
        cut_graph(prev)

cut_graph(N-1)
q = Queue()
q.put(0)
count = 0
while not q.empty():
    if count == len(availables)+1:
        print(-1)
        break
    node = q.get()
    edges = nodes_edges[node]
    for edge in edges:
        nxt = B[edge]
        if nxt not in availables:
            continue
        coin[nxt] = max(C[edge]+coin[node], coin[nxt])
        q.put(nxt)
    count += 1
else:
    ans = coin[N-1]
    ans = 0 if ans < 0 else ans
    print(ans)