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
from collections import deque
def input():
    return sys.stdin.readline()[:-1]

n, m, p = map(int,input().split())
d = {}
dr = {}
for i in range(m):
    ai,bi,ci = map(int,input().split())
    if not ai-1 in d:
        d[ai-1] = []
    d[ai-1] += [[bi-1,ci-p]]
    if not bi-1 in dr:
        dr[bi-1] = []
    dr[bi-1] += [ai-1]
#n-1に到達可能な点
an = [False for i in range(n)]
q1 = deque([n-1])

while len(q1) > 0:
    x1 = q1.popleft()
    an[x1] = True
    if x1 in dr:
        for dd in dr[x1]:
            if not an[dd]:
                q1.append(dd)
 
v = [False for i in range(n)]
sp = [0 for i in range(n)]
q = deque([0])
while len(q) > 0:
    start = q.popleft()
    v[start] = True
    if start in d:
        for nx in d[start]:
            if an[nx[0]]:            
                if not v[nx[0]]:
                    sp[nx[0]] = max(sp[nx[0]],sp[start] + nx[1])
                    q.append(nx[0])
                else:
                    if sp[nx[0]] < sp[start] + nx[1]:
                            print(-1)
                            exit()
print(max(0,sp[n-1]))

Submission status: Wrong Answer

Solution code:
from collections import deque
n, m, p = map(int,input().split())
d = {}
dr = {}
for i in range(m):
    ai,bi,ci = map(int,input().split())
    if not ai-1 in d:
        d[ai-1] = []
    d[ai-1] += [[bi-1,ci-p]]
    if not bi-1 in dr:
        dr[bi-1] = []
    dr[bi-1] += [ai-1]
#n-1に到達可能な点
an = [False for i in range(n)]
q1 = deque([n-1])

while len(q1) > 0:
    x1 = q1.popleft()
    an[x1] = True
    if x1 in dr:
        for dd in dr[x1]:
            if not an[dd]:
                q1.append(dd)
 
v = [False for i in range(n)]
sp = [0 for i in range(n)]
q = deque([0])
while len(q) > 0:
    start = q.popleft()
    v[start] = True
    if start in d:
        for nx in d[start]:
            if an[nx[0]]:            
                if not v[nx[0]]:
                    sp[nx[0]] = max(sp[nx[0]],sp[start] + nx[1])
                    q.append(nx[0])
                else:
                    if sp[nx[0]] < sp[start] + nx[1]:
                            print(-1)
                            exit()
print(sp[n-1])

Submission status: Wrong Answer

Solution code:
import collections

n, m, p = map(int, input().split())
edges1 = collections.defaultdict(dict)
edges2 = collections.defaultdict(dict)
for _ in range(m):
    a, b, c = map(int, input().split())
    edges1[a - 1][b - 1] = (c - p) * -1
    edges2[b - 1][a - 1] = (c - p) * -1


def dfs(edges, start):
    stack = [start]
    use = {start, }
    while stack:
        x = stack.pop()
        for y in edges[x]:
            if y in use:
                continue
            stack.append(y)
            use.add(y)
    return use


def bellman_ford(edges):
    v = [float('inf')] * n
    v[0] = 0
    for _ in range(n):
        updated = False
        for x in edges:
            for y in edges[x]:
                if v[x] + edges[x][y] < v[y]:
                    updated = True
                    v[y] = v[x] + edges[x][y]
        if not updated:
            return max(0, v[n - 1] * -1)
    return -1
    



use = dfs(edges1, 0) & dfs(edges2, n - 1)
edges = collections.defaultdict(dict)
for x in edges1:
    for y in edges1[x]:
        if x in use and y in use:
            edges[x][y] = edges1[x][y]

answer = bellman_ford(edges)
print(answer)

Submission status: Wrong Answer

Solution code:
import collections

n, m, p = map(int, input().split())
edges1 = collections.defaultdict(dict)
edges2 = collections.defaultdict(dict)
for _ in range(m):
    a, b, c = map(int, input().split())
    edges1[a - 1][b - 1] = (c - p) * -1
    edges2[b - 1][a - 1] = (c - p) * -1


def dfs(edges, start):
    stack = [start]
    use = {start, }
    while stack:
        x = stack.pop()
        for y in edges[x]:
            if y in use:
                continue
            stack.append(y)
            use.add(y)
    return use


def bellman_ford(edges):
    v = [float('inf')] * n
    v[0] = 0
    for _ in range(n - 1):
        for x in edges:
            for y in edges[x]:
                if v[x] + edges[x][y] < v[y]:
                    v[y] = v[x] + edges[x][y]
    
    for x in edges:
        for y in edges[x]:
            if v[x] + edges[x][y] < v[y]:
                return -1
    return max(0, v[n - 1] * -1)



use = dfs(edges1, 0) & dfs(edges2, n - 1)
edges = collections.defaultdict(dict)
for x in edges1:
    for y in edges1[x]:
        if x in use and y in use:
            edges[x][y] = edges1[x][y]

answer = bellman_ford(edges)
print(answer)

Submission status: Wrong Answer

Solution code:
import collections

n, m, p = map(int, input().split())
scores = collections.defaultdict(dict)
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    scores[a][b] = (c - p) * -1

# 閉路検出
stack = [n - 1]
visited = set()
while stack:
    node = stack.pop()
    visited.add(node)
    for nxt in scores[node]:
        if nxt == n - 1:
            print(-1)
            exit()
        if not nxt in visited:
            stack.append(nxt)


v = [float('inf')] * n
v[0] = 0
for _ in range(n - 1):
    for x in scores:
        for y in scores[x]:
            if v[x] + scores[x][y] < v[y]:
                v[y] = v[x] + scores[x][y]

ans = v[n - 1] * -1
if ans > 0:
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import collections

n, m, p = map(int, input().split())
scores = collections.defaultdict(dict)
for _ in range(m):
    a, b, c = map(int, input().split())
    scores[a][b] = c - p

stack = [n]
visited = set()
while stack:
    node = stack.pop()
    visited.add(node)
    for nxt in scores[node]:
        if nxt == n:
            print(-1)
            exit()
        if not nxt in visited:
            stack.append(nxt)

stack = [(1, 0, set())]
answer = -float('inf')
while stack:
    node, score, history = stack.pop()
    if node == n:
        answer = max(answer, score)
    else:
        for nxt in scores[node]:
            if nxt in history:
                continue
            stack.append((nxt, score + scores[node][nxt], history | {node, }))

if answer > 0:
    print(answer)
else:
    print(0)

Submission status: Wrong Answer

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

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


def dfs(e, s):
    v_set = {s}
    stack = [s]
    while stack:
        v = stack.pop()
        for next_v in e[v]:
            if next_v in v_set:
                continue
            v_set.add(next_v)
            stack.append(next_v)
    return v_set


def bellman_ford(sub_abc, n):
    inf = float("inf")
    dist = [inf] * n
    dist[0] = 0

    for _ in range(n):
        updated = False
        target_updated = False
        for u, v, d in sub_abc:
            if dist[u] + d < dist[v]:
                dist[v] = dist[u] + d
                updated = True
                if v == n - 1:
                    target_updated = True
        if not updated:
            break
    else:
        if target_updated:
            return -1

    return max(-dist[n - 1], 0)


use = dfs(edge, 0) & dfs(rev_edge, n - 1)
print(bellman_ford([(a, b, c)
                    for (a, b, c) in abc if a in use and b in use], n))

Submission status: Wrong Answer

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

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


def dfs(e, n):
    v_set = {n - 1}
    stack = [n - 1]
    while stack:
        v = stack.pop()
        for next_v in e[v]:
            if next_v in v_set:
                continue
            v_set.add(next_v)
            stack.append(next_v)
    return v_set


def bellman_ford(sub_abc, n):
    inf = float("inf")
    dist = [inf] * n
    dist[0] = 0

    for _ in range(n):
        updated = False
        target_updated = False
        for u, v, d in sub_abc:
            if dist[u] + d < dist[v]:
                dist[v] = dist[u] + d
                updated = True
                if v == n - 1:
                    target_updated = True
        if not updated:
            break
    else:
        if target_updated:
            return -1

    return max(-dist[n - 1], 0)


use = dfs(rev_edge, n)
print(bellman_ford([(a, b, c)
                    for (a, b, c) in abc if a in use and b in use], n))

Submission status: Wrong Answer

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

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


def bellman_ford(e, n):
    inf = float("inf")
    dist = [10**18] * n
    dist[0] = 0

    for _ in range(n):
        updated = False
        target_updated = False
        for u, v, d in edges:
            if dist[u] + d < dist[v]:
                dist[v] = dist[u] + d
                updated = True
                if v == n - 1:
                    target_updated = True
        if not updated:
            break
    else:
        if target_updated:
            return -1

    return max(-dist[n - 1], 0)


print(bellman_ford(edges, n))

Submission status: Wrong Answer

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

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


def bellman_ford(e, n):
    inf = float("inf")
    dist = [inf] * n
    dist[0] = 0

    for _ in range(n):
        updated = False
        target_updated = False
        for u, v, d in edges:
            if dist[u] + d < dist[v]:
                dist[v] = dist[u] + d
                updated = True
                if v == n - 1:
                    target_updated = True
        if not updated:
            break
    else:
        if target_updated:
            return -1

    return max(-dist[n - 1], 0)


print(bellman_ford(edges, n))

Submission status: Wrong Answer

Solution code:
INFINITY = float("inf")

N, M, P = [int(e) for e in input().split()]
Es = [None] * M

for i in range(M):
    Es[i] = ([int(e) for e in input().split()])
    Es[i][2] = P - Es[i][2]

d = [INFINITY] * (N + 1)
d[1] = 0

for _ in range(M):
    for e in Es:
        u, v, w = e
        dd = d[u] + w
        if dd < d[v]:
            d[v] = dd

circle_exists = False

for e in Es:
    u, v, w = e

    if v != N:
        continue

    if d[u] + w < d[v]:
        circle_exists = True
        break


if circle_exists:
    print(-1)
else:
    print(max(0, -d[N]))

Submission status: Wrong Answer

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

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


def shortest_path(e, n):
    dist = [float("inf")] * (n + 1)
    dist[1] = 0

    for _ in range(n):
        for u, v, d in edges:
            if dist[u] + d < dist[v]:
                dist[v] = dist[u] + d

    result_1 = dist[n]
    for u, v, d in edges:
        if dist[u] + d < dist[v]:
            dist[v] = dist[u] + d
    result_2 = dist[n]

    if result_1 != result_2:
        return 1

    return min(result_1, 0)


print(-shortest_path(edges, n))

Submission status: Wrong Answer

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

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


def shortest_path(e, n):
    dist = [float("inf")] * (n + 1)
    dist[1] = 0

    for _ in range(n - 1):
        for u, v, d in edges:
            if dist[u] + d < dist[v]:
                dist[v] = dist[u] + d

    result_1 = dist[n]
    for u, v, d in edges:
        if dist[u] + d < dist[v]:
            dist[v] = dist[u] + d
    result_2 = dist[n]

    if result_1 != result_2:
        return 1

    return min(result_1, 0)


print(-shortest_path(edges, n))

Submission status: Wrong Answer

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

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


def shortest_path(e, n):
    dist = [0] * (n + 1)
    dist[1] = 0

    for _ in range(n - 1):
        for u, v, d in edges:
            if dist[u] + d > dist[v]:
                dist[v] = dist[u] + d

    result_1 = dist[n]
    for u, v, d in edges:
        if dist[u] + d > dist[v]:
            dist[v] = dist[u] + d
    result_2 = dist[n]

    if result_1 > 0 and result_1 != result_2:
        return -1

    return max(result_1, 0)


print(shortest_path(edges, n))

Submission status: Wrong Answer

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

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


def shortest_path(e, n):
    dist = [0] * (n + 1)
    dist[1] = 0

    for _ in range(n - 1):
        for u, v, d in edges:
            if dist[u] + d > dist[v]:
                dist[v] = dist[u] + d

    result_1 = dist[n]
    for u, v, d in edges:
        if dist[u] + d > dist[v]:
            dist[v] = dist[u] + d
    result_2 = dist[n]

    if result_1 and result_1 != result_2:
        return -1

    return max(result_1, 0)


print(shortest_path(edges, n))

Submission status: Wrong Answer

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

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


def shortest_path(edges, n):
    dist = [float("inf")] * (n + 1)
    dist[1] = 0

    for _ in range(n - 1):
        for u, v, d in edges:
            if dist[u] + d < dist[v]:
                dist[v] = dist[u] + d

    result_1 = dist[n]
    for u, v, d in edges:
        if dist[u] + d < dist[v]:
            dist[v] = dist[u] + d
    result_2 = dist[n]

    if result_1 != result_2:
        return 1

    return min(result_1, 0)


print(-shortest_path(edges, n))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

graph = {}
for _ in range(m):
    a, b, c = map(int, input().split())
    if a not in graph:
        graph[a] = {}
    graph[a][b] = p-c


def shortest_path(g, n):
    dist = [float("inf")] * (n + 1)
    dist[1] = 0

    for _ in range(n - 1):
        for u in range(1, n + 1):
            for v in g.get(u, {}):
                if dist[u] + g[u][v] < dist[v]:
                    dist[v] = dist[u] + g[u][v]

    for u in range(1, n + 1):
        if n in g.get(u, {}):
            if dist[u] < float("inf") and g[u][n] < float("inf") and dist[u] + g[u][n] < dist[n]:
                return 1

    return min(dist[n], 0)


print(-shortest_path(graph, n))

Submission status: Wrong Answer

Solution code:

N, M, P = map(int, input().split())
edges = [list(map(int, input().split())) for i in range(M)]
INF = float("inf")

cost_edges = [[edge[0], edge[1], -1*(edge[2]-P)] for edge in edges]
v = [INF for i in range(N)]
v[0] = 0
finish = True
for i in range(2*N):
    for edge in cost_edges:
        if v[edge[1]-1] > v[edge[0]-1] + edge[2]:
            v[edge[1]-1] = v[edge[0]-1] + edge[2]
            if i >= N:
                v[edge[1]-1] = INF
        if i == N-1:
            goal = v[N-1]
if goal != v[N-1]:
    print(-1)
else:
    print(max( -1*v[N-1], 0))

Submission status: Wrong Answer

Solution code:

N, M, P = map(int, input().split())
edges = [list(map(int, input().split())) for i in range(M)]
inf = 10**9

cost_edges = [[edge[0], edge[1], -1*(edge[2]-P)] for edge in edges]
v = [inf for i in range(N)]
v[0] = 0
finish = True
for i in range(2*N):
    for edge in cost_edges:
        if v[edge[1]-1] > v[edge[0]-1] + edge[2]:
            v[edge[1]-1] = v[edge[0]-1] + edge[2]
            if i >= N:
                v[edge[1]-1] = inf
        if i == N-1:
            goal = v[N-1]
if goal != v[N-1]:
    print(-1)
else:
    print(max( -1*v[N-1], 0))

Submission status: Wrong Answer

Solution code:

N, M, P = map(int, input().split())
edges = [list(map(int, input().split())) for i in range(M)]
inf = 10**9
reachable_from = [False for i in range(N)]
reachable_to = [False for i in range(N)]
reachable_from[0] = True
reachable_to[N-1] = True
edges.sort(key=lambda x: x[0])
for edge in edges:
    if reachable_from[edge[0]-1]:
        reachable_from[edge[1]-1] = True

edges.sort(key=lambda x: x[1], reverse=True)
for edge in edges:
    if reachable_to[edge[1]-1]:
        reachable_to[edge[0]-1] = True

reachable_edges = [edge for edge in edges if reachable_from[edge[0]-1] and reachable_to[edge[1]-1]]
cost_edge = [[edge[0], edge[1], -1*(edge[2]-P)] for edge in reachable_edges]
v = [inf for i in range(N)]
v[0] = 0
finish = True
for i in range(2*N):
    for edge in cost_edge:
        if v[edge[1]-1] > v[edge[0]-1] + edge[2]:
            v[edge[1]-1] = v[edge[0]-1] + edge[2]
            if i >= N:
                finish = False
                break
if finish:
    print(max( -1*v[N-1], 0))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:

N, M, P = map(int, input().split())
edges = [list(map(int, input().split())) for i in range(M)]
inf = 10**9
reachable_from = [False for i in range(N)]
reachable_to = [False for i in range(N)]
reachable_from[0] = True
reachable_to[N-1] = True
edges.sort(key=lambda x: x[0])
for edge in edges:
    if reachable_from[edge[0]-1]:
        reachable_from[edge[1]-1] = True

edges.sort(key=lambda x: x[1], reverse=True)
for edge in edges:
    if reachable_to[edge[1]-1]:
        reachable_to[edge[0]-1] = True

reachable_edges = [edge for edge in edges if reachable_from[edge[0]-1] and reachable_to[edge[1]-1]]
cost_edge = [[edge[0], edge[1], -1*(edge[2]-P)] for edge in reachable_edges]
v = [inf for i in range(N)]
v[0] = 0
finish = True
for i in range(N+1):
    for edge in cost_edge:
        if v[edge[1]-1] > v[edge[0]-1] + edge[2]:
            v[edge[1]-1] = v[edge[0]-1] + edge[2]
            if i == N:
                finish = False
                break
if finish:
    print(max( -1*v[N-1], 0))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N,M,P = map(int,input().split())
abc = []
edge = [[] for _ in range(N)]
for _ in range(M):
    a,b,c = map(int,input().split())
    #向かってくる矢印
    edge[b-1].append(a-1)
    abc.append((a-1,b-1,P-c))

def dfs(edge,goal):
    #goal = n-1
    used = {goal}
    q = [goal]
    while q:
        v = q.pop()
        for w in edge[v]:
            if w in used:continue
            used.add(w)
            q.append(w)
    
    return used

def bellman_ford(v,s,g,e):

    '''
    v: vertex(頂点)
    s: start.  in this case,0
    g: goal. in this case,n-1
    e: (edge_start,egde_goal,score)
    今回はp-cの最小値を求めていく.
    '''
    inf = 10*10
    # cost : d[V] 
    d = [inf] * v
    #開始点のコストを0とする
    d[s] = 0
    for _ in range(v):
        flag = False
        for a,b,c in e:
            # a -> b の向きを考える
            if d[a] == inf:continue
            cost = d[a] + c
            if cost < d[b]:
                d[b] = cost
                flag = True
        if not flag: break
    if flag:
        return -1
    return max(-d[g],0)

vertex = dfs(edge,N-1)
abc_ = [(a,b,c) for a,b,c in abc if a in vertex and b in vertex]
print(bellman_ford(N,0,N-1,abc_))

Submission status: Wrong Answer

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

d = [float("inf") for _ in range(N)]
d[0] = 0
for i in range(N):
    for f, t, w in E:
        if d[f] != float("inf") and d[t] > d[f] + w:
            d[t] = d[f] + w
            if i == N - 1 and t == N - 1:
                print(-1)
                exit()
if d[-1] > 0:
    print(0)
else:
    print(-d[-1])

Submission status: Wrong Answer

Solution code:
def BellmanFord(Edges,num_v,start,goal):
    inf=float('inf')
    Lu =[inf]*num_v
    Lu[start] = 0
    # Calculate cost
    for i in range(num_v-1):
        for (s,v,c) in Edges:
            Lu[v] = min(Lu[v], Lu[s]+c)
    
    # Detect cycles
    for (s,v,c) in Edges:
        if Lu[v] > Lu[s] + c:
            Lu[v] = -inf

    return Lu[goal]

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)))
d=BellmanFord(Edges,n,0,n-1)
print(-1 if d==-float('inf') else max(0,-d))

Submission status: Wrong Answer

Solution code:
import collections
import sys
import time
sys.setrecursionlimit(10 ** 5)
N, M, P = map(int, input().split())
e = collections.defaultdict(list)
st = time.time()
for _ in range(M):
  u, v, c = map(int, input().split())
  e[u].append((v, c))
def isConnected(n):
  if n == N:
    return True
  vis = [0] * (N + 1)
  que = collections.deque([])
  que.append(n)
  while que:
    nc = que.popleft()
    vis[nc] = 1
    for o in e[nc]:
      if o[0] == N:
        return True
      if vis[o[0]] == 0:
        que.append(o[0])
  return False
inf = 10 ** 10
visited = [0] * (N + 1)
earn = [-inf] * (N + 1)
earn[1] = 0
def dfs(now):
  global visited
  global earn
  nt = time.time() - st
  if nt > 1.8:
    print(max(earn[N], 0))
    exit(0)
  visited[now] = 1
  for n in e[now]:
    if visited[n[0]] == 0:
      if n[1] + earn[now] - P > earn[n[0]]:
        earn[n[0]] = n[1] + earn[now] - P
        dfs(n[0])
        visited[n[0]] = 0
    else:
      if earn[n[0]] < n[1] + earn[now] - P:
        if isConnected(n[0]) == True:
          print(-1)
          exit(0)
dfs(1)
print(max(earn[N], 0))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N,M,P = inpl()
lines = defaultdict(set)

for _ in range(M):
    a,b,c = inpl()
    a,b = a-1,b-1
    lines[a].add((b,-c+P))

#ベルマンフォード(始点と終点が決まってる時)
def BellmanFord(Start,Goal):
    Costs=[INF]*N
    Costs[Start] = 0
    upd8s = [True]*N
    for i in range(2*N): #2N回ループ(負回路の検出までみる)
        will_upd8s = [False]*N
        upd8 = False
        for s in range(N):
            if not upd8s[s]: continue    #前回更新してないので見ない
            for t,c in lines[s]:
                if c + Costs[s] < Costs[t]:
                    Costs[t] = Costs[s]+c
                    upd8 = True
                    will_upd8s[t] = True #更新した点だけ次に見る

        if not upd8: #なにも更新しなかったら終わり
            return Costs[Goal]

        if i == N-1: #Nループ目のGoalのCostを記録
            tmp = Costs[Goal]

        upd8s = will_upd8s[:]

    if tmp != Costs[Goal]:
        return -INF
    else:
        return Costs[Goal]

ans = -BellmanFord(0,N-1)
if ans == INF:
    print(-1)
else:
    print(max(0,ans))

Submission status: Wrong Answer

Solution code:
import collections
N, M, P = map(int, input().split())
e = collections.defaultdict(list)
for _ in range(M):
  u, v, c = map(int, input().split())
  e[u].append((v, c))
inf = 10 ** 10
visited = [0] * (N + 1)
earn = [-inf] * (N + 1)
earn[1] = 0
Q = collections.deque([])
Q.append(1)
while Q:
  n = Q.popleft()
  visited[n] = 1
  for o in e[n]:
    if visited[o[0]] == 0:
      Q.append(o[0])
      earn[o[0]] = max(o[1] + earn[n] - P, earn[o[0]])
    else:
      if earn[o[0]] < o[1] + earn[n] - P:
        print(-1)
        exit(0)
  visited[n] = 0
print(max(earn[N], 0))

Submission status: Wrong Answer

Solution code:
import collections
N, M, P = map(int, input().split())
e = collections.defaultdict(list)
for _ in range(M):
  u, v, c = map(int, input().split())
  e[u].append((v, c))
def isConnected(n):
  if n == N:
    return True
  vis = [0] * (N + 1)
  que = collections.deque([])
  que.append(n)
  while que:
    nc = que.popleft()
    vis[nc] = 1
    for o in e[nc]:
      if o[0] == N:
        return True
      if visited[o[0]] == 0:
        que.append(o[0])
  return False

visited = [0] * (N + 1)
earn = [0] * (N + 1)
Q = collections.deque([])
Q.append(1)
while Q:
  n = Q.popleft()
  visited[n] = 1
  for o in e[n]:
    if visited[o[0]] == 0:
      Q.append(o[0])
      earn[o[0]] = max(o[1] + earn[n] - P, earn[o[0]])
    else:
      if earn[o[0]] < o[1] + earn[n] - P:
        if isConnected(o[0]) == True:
          print(-1)
          exit(0)
print(max(earn[N], 0))

Submission status: Wrong Answer

Solution code:
import collections
N, M, P = map(int, input().split())
e = collections.defaultdict(list)
for _ in range(M):
  u, v, c = map(int, input().split())
  e[u].append((v, c))
def isConnected(n):
  vis = [0] * (N + 1)
  que = collections.deque([])
  que.append(n)
  while que:
    nc = que.popleft()
    vis[nc] = 1
    for o in e[nc]:
      if o[0] == N:
        return True
      if visited[o[0]] == 0:
        que.append(o[0])
  return False


visited = [0] * (N + 1)
earn = [0] * (N + 1)
Q = collections.deque([])
Q.append(1)
while Q:
  n = Q.popleft()
  visited[n] = 1
  for o in e[n]:
    if visited[o[0]] == 0:
      Q.append(o[0])
      earn[o[0]] = max(o[1] + earn[n] - P, earn[o[0]])
    else:
      if earn[o[0]] < o[1] + earn[n] - P:
        if isConnected(o[0]) == True:
          print(-1)
          exit(0)
print(max(earn[N], 0))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF = float('inf')

def main():
    n, m, p = map(int,input().split())
    es = []
    for _ in range(m):
        e = list(map(int,input().split()))
        es.append([e[0]-1,e[1]-1,- e[2] + p])

    v_cost = [INF]*n
    v_cost[0] = 0
    for v in range(n*2):
        for e in es:
            frm = e[0]
            to = e[1]
            if v_cost[to] > v_cost[frm]+e[2]:
                v_cost[to] = v_cost[frm]+e[2]
                #if v >= n:
                #    v_cost[to] = -INF
        if v == n-1:
            prev = v_cost[-1]

    if prev != v_cost[-1]:
        return -1

    return max(0,-v_cost[-1])
   

if __name__ =="__main__":
    print (main())

Submission status: Wrong Answer

Solution code:

INF = float('inf')

def main():
    n, m, p = map(int,input().split())
    es = []
    for _ in range(m):
        e = list(map(int,input().split()))
        es.append([e[0]-1,e[1]-1,- e[2] + p])

    v_cost = [INF]*n
    v_cost[0] = 0
    for v in range(n):
        for e in es:
            frm = e[0]
            to = e[1]
            v_cost[to] = min(v_cost[to],v_cost[frm]+e[2])
    
    tmp = v_cost[-1]

    for v in range(n):
        for e in es:
            frm = e[0]
            to = e[1]
            if v_cost[to] > v_cost[frm]+e[2]:
                v_cost[to] = v_cost[frm]+e[2]
                if v >= n:
                    v_cost[to] = -INF
    if tmp != v_cost[-1]:
        return -1

    return max(0,-v_cost[-1])
   

if __name__ =="__main__":
    print (main())

Submission status: Wrong Answer

Solution code:

INF = (10**5)*(10**4) 

def main():
    n, m, p = map(int,input().split())
    es = []
    for _ in range(m):
        e = list(map(int,input().split()))
        es.append([e[0]-1,e[1]-1,- e[2] + p])

    v_cost = [INF]*n
    v_cost[0] = 0
    for v in range(n):
        for e in es:
            frm = e[0]
            to = e[1]
            v_cost[to] = min(v_cost[to],v_cost[frm]+e[2])
    
    tmp = v_cost[-1]

    for v in range(n):
        for e in es:
            frm = e[0]
            to = e[1]
            if v_cost[to] > v_cost[frm]+e[2]:
                v_cost[to] = v_cost[frm]+e[2]
                if v >= n:
                    v_cost[to] = -INF
    if tmp != v_cost[-1]:
        return -1

    return max(0,-v_cost[-1])
   

if __name__ =="__main__":
    print (main())

Submission status: Wrong Answer

Solution code:

INF = (10**5)*(10*4) 

def main():
    n, m, p = map(int,input().split())
    es = []
    for _ in range(m):
        e = list(map(int,input().split()))
        es.append([e[0]-1,e[1]-1,- e[2] + p])

    v_cost = [INF]*n
    v_cost[0] = 0
    for v in range(n):
        for e in es:
            frm = e[0]
            to = e[1]
            v_cost[to] = min(v_cost[to],v_cost[frm]+e[2])
    
    tmp = v_cost[-1]

    for v in range(n):
        for e in es:
            frm = e[0]
            to = e[1]
            if v_cost[to] > v_cost[frm]+e[2]:
                v_cost[to] = v_cost[frm]+e[2]
                if v >= n:
                    v_cost[to] = -INF
    if tmp != v_cost[-1]:
        return -1

    return max(0,-v_cost[-1])
   

if __name__ =="__main__":
    print (main())