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
import copy

N, M, P = [int(item) for item in input().split()]

from collections import defaultdict
next_b = defaultdict(lambda: [])

for i in range(M):
    a, b, c = [int(item) for item in input().split()]
    next_b[a].append([b, c - P])

max_sc = [0] + [int(-1e30)] * (N - 1)
prev_sc = [0] + [int(-1e30) - 1] * (N - 1)
route = [[0] * N] * N

pos_n = [0]
flag = 1

for loop in range(N):
    # print(max_sc)
    if not flag:
        print(max(max_sc[N - 1], 0))
        break
    flag = 0
    prev_sc = copy.deepcopy(max_sc)
    for i in pos_n:
        for d, scr in next_b[i + 1]:
            # print(route, d - 1, i)
            if route[d - 1][i] == 0:
                new_sc = prev_sc[i] + scr
                if new_sc > max_sc[d - 1]:
                    max_sc[d - 1] = new_sc
                    route[d - 1] = copy.deepcopy(route[i])
                    route[d - 1][i] = 1
                    pos_n.append(d - 1)
                    flag = 1
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import copy

N, M, P = [int(item) for item in input().split()]

from collections import defaultdict
next_b = defaultdict(lambda: [])

for i in range(M):
    a, b, c = [int(item) for item in input().split()]
    next_b[a].append([b, c - P])

max_sc = [0] + [int(-1e30)] * (N - 1)
prev_sc = [0] + [int(-1e30) - 1] * (N - 1)
route = [[0] * N] * N

pos_n = [0]

for loop in range(N):
    # print(max_sc)
    if max_sc == prev_sc:
        print(max(max_sc[N - 1], 0))
        break
    prev_sc = copy.deepcopy(max_sc)
    for i in pos_n:
        for d, scr in next_b[i + 1]:
            # print(route, d - 1, i)
            if route[d - 1][i] == 0:
                new_sc = prev_sc[i] + scr
                if new_sc > max_sc[d - 1]:
                    max_sc[d - 1] = new_sc
                    route[d - 1] = copy.deepcopy(route[i])
                    route[d - 1][i] = 1
                    pos_n.append(d - 1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import copy

N, M, P = [int(item) for item in input().split()]

from collections import defaultdict
next_b = defaultdict(lambda: [])

for i in range(M):
    a, b, c = [int(item) for item in input().split()]
    next_b[a].append([b, c - P])

max_sc = [0] + [int(-1e30)] * (N - 1)
prev_sc = [0] + [int(-1e30) - 1] * (N - 1)
route = [[0] * N] * N

for loop in range(N):
    # print(max_sc)
    if max_sc == prev_sc:
        print(max(max_sc[N - 1], 0))
        break
    prev_sc = copy.deepcopy(max_sc)
    for i in range(N):
        if prev_sc[i] > -1e30:
            for d, scr in next_b[i + 1]:
                # print(route, d - 1, i)
                if route[d - 1][i] == 0:
                    new_sc = prev_sc[i] + scr
                    if new_sc > max_sc[d - 1]:
                        max_sc[d - 1] = new_sc
                        route[d - 1] = copy.deepcopy(route[i])
                        route[d - 1][i] = 1
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**7)
    input=sys.stdin.readline
    def dfs(v):
        if v==n-1:return True
        r=False
        for w in edge[v]:
            if f[w]:
                r|=w in use
            else:
                f[w]=True
                r|=dfs(w)
        if r:use.add(v)
        return r
    n,m,p=map(int,input().split())
    edge=[[]for _ in range(n)]
    e=[]
    for _ in range(m):
        a,b,c=map(int,input().split())
        edge[a-1].append(b-1)
        e.append((a-1,b-1,p-c))
    f=[True]+[False]*(n-1)
    use={n-1}
    dfs(0)
    e=[(a,b,c)for a,b,c in e if a in use and b in use]
    INF=10**18
    d=[0]+[INF]*(n-1)
    for _ in range(2*n):
        f=False
        for a,b,c in e:
            cost=d[a]+c
            if cost<d[b]:
                d[b]=cost
                f=True
        if not f:break
    else:
        print(-1)
        exit()
    print(max(-d[-1],0))
main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**7)
    input=sys.stdin.readline
    def dfs(v):
        if v==n-1:return True
        r=False
        for w in edge[v]:
            if f[w]:
                r|=w in use
            else:
                f[w]=True
                r|=dfs(w)
        if r:use.add(v)
        return r
    n,m,p=map(int,input().split())
    edge=[[]for _ in range(n)]
    e=[]
    for _ in range(m):
        a,b,c=map(int,input().split())
        edge[a-1].append(b-1)
        e.append((a-1,b-1,p-c))
    f=[True]+[False]*(n-1)
    use={n-1}
    dfs(0)
    e=[(a,b,c)for a,b,c in e if a in use and b in use]
    INF=10**18
    d=[0]+[INF]*(n-1)
    for _ in range(n-1):
        f=False
        for a,b,c in e:
            cost=d[a]+c
            if cost<d[b]:
                d[b]=cost
                f=True
        if not f:break
    else:
        print(-1)
        exit()
    print(max(-d[-1],0))
main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**7)
    input=sys.stdin.readline
    def dfs(v):
        if v==n-1:return True
        r=False
        for w in edge[v]:
            if f[w]:
                r|=w in use
            else:
                f[w]=True
                r|=dfs(w)
        if r:use.add(v)
        return r
    n,m,p=map(int,input().split())
    edge=[[]for _ in range(n)]
    e=[]
    for _ in range(m):
        a,b,c=map(int,input().split())
        edge[a-1].append(b-1)
        e.append((a-1,b-1,p-c))
    f=[True]+[False]*(n-1)
    use={n-1}
    dfs(0)
    e=[(a,b,c)for a,b,c in e if a in use and b in use]
    INF=10**18
    d=[0]+[INF]*(n-1)
    for _ in range(n):
        f=False
        for p,q,r in e:
            dist=d[p]+r
            if INF>dist<d[q]:
                d[q]=dist
                f=True
        if not f:break
    else:
        print(-1)
        exit()
    print(max(-d[-1],0))
main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**7)
    input=sys.stdin.readline
    def dfs(v):
        if v==n-1:return True
        r=False
        for w in edge[v]:
            if f[w]:
                r|=w in use
            else:
                f[w]=True
                r|=dfs(w)
        if r:use.add(v)
        return r
    n,m,p=map(int,input().split())
    edge=[[]for _ in range(n)]
    e=[]
    for _ in range(m):
        a,b,c=map(int,input().split())
        edge[a-1].append(b-1)
        e.append((a-1,b-1,p-c))
    f=[True]+[False]*(n-1)
    use={n-1}
    dfs(0)
    e=[(a,b,c)for a,b,c in e if a in use and b in use]
    INF=10**18
    d=[0]+[INF]*(n-1)
    for i in range(n):
        f=False
        for p,q,r in e:
            dist=d[p]+r
            if dist<d[q]:
                d[q]=dist
                f=True
        if not f:break
    else:
        print(-1)
        exit()
    print(max(-d[-1],0))
main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**7)
    input=sys.stdin.readline
    def bfs(v):
        if v==n-1:return True
        r=False
        for w in edge[v]:
            if f[w]:continue
            f[w]=True
            r|=bfs(w)
        if r:use.add(v)
        return r
    n,m,p=map(int,input().split())
    edge=[[]for _ in range(n)]
    e=[]
    for _ in range(m):
        a,b,c=map(int,input().split())
        edge[a-1].append(b-1)
        e.append((a-1,b-1,p-c))
    f=[True]+[False]*(n-1)
    use={n-1}
    bfs(0)
    e=[(a,b,c)for a,b,c in e if a in use and b in use]
    INF=10**18
    d=[0]+[INF]*(n-1)
    for i in range(n):
        f=False
        for p,q,r in e:
            dist=d[p]+r
            if dist<d[q]:
                d[q]=dist
                f=True
        if not f:break
    else:
        print(-1)
        exit()
    print(max(-d[-1],0))
main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input=sys.stdin.readline
    n,m,p=map(int,input().split())
    e=[]
    for _ in range(m):
        a,b,c=map(int,input().split())
        e.append((a-1,b-1,p-c))
    INF=10**18
    d=[0]+[INF]*(n-1)
    for i in range(2*n):
        f=False
        for p,q,r in e:
            dist=d[p]+r
            if dist<d[q]:
                d[q]=dist
                f=True
        if not f:break
        if i==n-1:b=d[-1]
        if i==2*n-1:
            if b!=d[-1]:
                print(-1)
                exit()
    print(max(-d[-1],0))
main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input=sys.stdin.readline
    n,m,p=map(int,input().split())
    e=[]
    for _ in range(m):
        a,b,c=map(int,input().split())
        e.append((a-1,b-1,p-c))
    INF=10**18
    d=[0]+[INF]*(n-1)
    for i in range(2*n):
        f=False
        for p,q,r in e:
            dist=d[p]+r
            if dist<d[q]:
                d[q]=dist
                f=True
        if not f:break
        if i==n-2:b=d[-1]
        if i==2*n-1:
            if b!=d[-1]:
                print(-1)
                exit()
    print(max(-d[-1],0))
main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input=sys.stdin.readline
    n,m,p=map(int,input().split())
    e=[]
    for _ in range(m):
        a,b,c=map(int,input().split())
        e.append((a-1,b-1,p-c))
    INF=10**18
    d=[0]+[INF]*(n-1)
    for i in range(n):
        f=False
        for p,q,r in e:
            dist=d[p]+r
            if dist<d[q]:
                d[q]=dist
                f=True
        if not f:break
        if i==n-2:b=d[-1]
        if i==n-1:
            if b!=d[-1]<0:
                print(-1)
                exit()
    print(max(-d[-1],0))
main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input=sys.stdin.readline
    n,m,p=map(int,input().split())
    e=[]
    for _ in range(m):
        a,b,c=map(int,input().split())
        e.append((a-1,b-1,p-c))
    INF=10**18
    d=[0]+[INF]*(n-1)
    for i in range(2*n):
        f=False
        for p,q,r in e:
            dist=d[p]+r
            if dist<d[q]:
                d[q]=dist
                f=True
        if not f:break
        if i==2*n-2:b=d[-1]
        if i==2*n-1:
            if d[-1]!=b:
                print(-1)
                exit()
    print(max(-d[-1],0))
main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n,m,p=map(int,input().split())
e=[]
for _ in range(m):
    a,b,c=map(int,input().split())
    e.append((a-1,b-1,p-c))
INF=10**18
d=[0]+[INF]*(n-1)
c=0
while 1:
    f=0
    for p,q,r in e:
        dist=d[p]+r
        if dist<d[q]:
            d[q]=dist
            f=1
    if f<1:
        break
    c+=1
    if c==n-1:
        b=d[-1]
    if c==n:
        if d[-1]!=b:
            print(-1)
            exit()
        break
print(max(-d[-1],0))

Submission status: Wrong Answer

Solution code:
N,M,P = map(int,input().split())
make_Edge = lambda xs:[int(xs[0])-1,int(xs[1])-1,P-int(xs[2])] 
Edges =[make_Edge(input().split()) for i in range(M)]
 
def bellman_ford(N,edges):
    #各点の重み
    inf = float('inf')
    dist = [inf]*N
    dist[0] = 0
    for i in range(2*N):
        for e in edges:
            if dist[e[0]] != inf and dist[e[1]] > dist[e[0]]+e[2]:
                dist[e[1]] = dist[e[0]]+e[2]
                if i >= N-1 and e[1] == N-1:
                    return None
    return -dist[N-1] if dist[N-1] != inf else None
  
ans = bellman_ford(N,Edges)
if ans is None:
    print(-1)
elif ans < 0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N,M,P = map(int,input().split())
make_Edge = lambda xs:[int(xs[0])-1,int(xs[1])-1,P-int(xs[2])] 
Edges =[make_Edge(input().split()) for i in range(M)]
 
def bellman_ford(N,edges):
    #各点の重み
    inf = float('inf')
    dist = [inf]*N
    dist[0] = 0
    for i in range(2*N):
        for e in edges:
            if dist[e[0]] != inf and dist[e[1]] > dist[e[0]]+e[2]:
                dist[e[1]] = dist[e[0]]+e[2]
                if i >= N-1 and e[1] == N-1:
                    return None
    return -dist[N-1]
  
ans = bellman_ford(N,Edges)
if ans is None:
    print(-1)
elif ans < 0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
ABC = []
for _ in range(M):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
    
import sys
INT_MAX = sys.maxsize # 9223372036854775807        
    
def bellman_ford(edges,num_v,source):

    dist=[INT_MAX] * num_v
    dist[source]=0

    #辺の緩和
    has_negative = False
    for i in range(num_v):
        for edge in edges:
            if edge[0] != INT_MAX and dist[edge[1]] > dist[edge[0]] + edge[2]:
                if dist[edge[0]] != INT_MAX:
                    dist[edge[1]] = dist[edge[0]] + edge[2]
                if i==num_v-1:
                    has_negative = True
    
    if has_negative == False:
        return dist
    
    negative = [False] * num_v
    for i in range(num_v):
        for edge in edges:
            if dist[edge[1]]>dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                negative[edge[0]] = True
                
    for i, is_negative in enumerate(negative):
        if is_negative:
            dist[i] = -1 * INT_MAX
        
    return dist

edges = ABC[:]
edges = [(a, b, -c+P) for a, b, c in edges]

shortest_path = bellman_ford(edges, N+1, 1)
ans = -1 * shortest_path[N]
#print(shortest_path)
if ans == -INT_MAX or ans == INT_MAX:
    print(-1)
elif ans < 0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
"""
AtCoder
    :author: vierbein
"""
from __future__ import absolute_import, division, print_function

# import itertools as it
from collections import *
import sys
from heapq import *

INFTY = 10000000000


def bellman_ford(vertices, adj_list, edge_weight, source, sink):
    """
    Bellman-Ford algorithm for shortest path
    O(V E)
    """
    d = {vertex: (0 if vertex == source else INFTY) for vertex in vertices}
    prev = {v: None for v in vertices}

    num_vertices = len(vertices)
    for _ in range(num_vertices):
        for (u, v), weight in edge_weight.items():
            alt = d[u] + weight
            if d[v] > alt:
                d[v] = alt
                prev[v] = u

    path = []
    curr = sink
    while curr is not None:
        path.append(curr)
        curr = prev[curr]
        if curr in path:
            break
    # pp("path:", path)

    for (u, v), weight in edge_weight.items():
        if u in path and d[u] + weight < d[v]:
            raise ValueError("Graph contains a negative-weight cycle")
    
    return d, prev


def pp(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)


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

adj_list = defaultdict(list)
edge_weight = dict()
for _ in range(M):
    a, b, c = map(int, input().split())
    adj_list[a].append(b)
    edge_weight[(a, b)] = - (c - P)

# pp("adj_list:", adj_list)    
# pp("edge_weight:", edge_weight)

try:
    d, _ = bellman_ford(range(1, N+1), adj_list, edge_weight, 1, N)
    print(max(0, -d[N]))
except ValueError:
    print(-1)

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
ABC = []
for _ in range(M):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
    
import sys
INT_MAX = sys.maxsize # 9223372036854775807        
    
def bellman_ford(edges,num_v,source):

    dist=[INT_MAX] * num_v
    dist[source]=0

    #辺の緩和
    has_negative = False
    for i in range(num_v):
        for edge in edges:
            if edge[0] != INT_MAX and dist[edge[1]] > dist[edge[0]] + edge[2]:
                if dist[edge[0]] != INT_MAX:
                    dist[edge[1]] = dist[edge[0]] + edge[2]
                if i==num_v-1:
                    has_negative = True
    
    if has_negative == False:
        return dist
    
    negative = [False] * num_v
    for i in range(num_v):
        for edge in edges:
            if dist[edge[1]]>dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                negative[edge[1]] = True
                
    for i, is_negative in enumerate(negative):
        if is_negative:
            dist[i] = -1 * INT_MAX
        
    return dist

edges = ABC[:]
edges = [(a, b, -c+P) for a, b, c in edges]

shortest_path = bellman_ford(edges, N+1, 1)
ans = -1 * shortest_path[N]
#print(shortest_path)
if ans == -INT_MAX or ans == INT_MAX:
    print(-1)
elif ans < 0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:

# グラフ定義
class Edge:
    def __init__(self, f: int, t: int, cost: int):
        self._t = t
        self._cost = cost
        self._f = f

    @property
    def t(self):
        return self._t

    @property
    def f(self):
        return self._f

    @property
    def cost(self):
        return self._cost


INF = 1 << 28


# ここから本実装
def bellman_ford(n: int, edges, s: int):
    dist = [INF for _ in range(n)]
    dist[s] = 0

    for i in range(2 * n):
        for e in edges:

            if dist[e.t] > dist[e.f] + e.cost:
                dist[e.t] = dist[e.f] + e.cost
                if i >= n - 1:
                    return None

    return dist


def visitable_node(n, g, rg):

    vis = [False] * n

    def dfs(idx):
        vis[idx] = True
        f = True
        while f:
            f = False
            for e in g[idx]:
                if not vis[e]:
                    idx = e
                    f = True
                    vis[idx] = True
                    break

    dfs(0)

    rvis = [False] * n

    def rdfs(idx):
        rvis[idx] = True
        f = True
        while f:
            f = False
            for e in rg[idx]:
                if not rvis[e]:
                    idx = e
                    f = True
                    rvis[idx] = True
                    break

    rdfs(n - 1)

    return [vis[i] & rvis[i] for i in range(n)]


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

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

visitable = visitable_node(n, g, rg)
updated = [e for e in edges if visitable[e.f] and visitable[e.t]]

dist = bellman_ford(n, updated, 0)

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

Submission status: Wrong Answer

Solution code:

# グラフ定義
class Edge:
    def __init__(self, f: int, t: int, cost: int):
        self._t = t
        self._cost = cost
        self._f = f

    @property
    def t(self):
        return self._t

    @property
    def f(self):
        return self._f

    @property
    def cost(self):
        return self._cost


INF = 1 << 28


# ここから本実装
def bellman_ford(n: int, edges, s: int):
    dist = [INF for _ in range(n)]
    dist[s] = 0

    for i in range(n):
        for e in edges:

            if dist[e.t] > dist[e.f] + e.cost:
                dist[e.t] = dist[e.f] + e.cost
                if i == n - 1:
                    return None

    return dist


def visitable_node(n, g, rg):

    vis = [False] * n

    def dfs(idx):
        vis[idx] = True
        f = True
        while f:
            f = False
            for e in g[idx]:
                if not vis[e]:
                    idx = e
                    f = True
                    vis[idx] = True
                    break

    dfs(0)

    rvis = [False] * n

    def rdfs(idx):
        rvis[idx] = True
        f = True
        while f:
            f = False
            for e in rg[idx]:
                if not rvis[e]:
                    idx = e
                    f = True
                    rvis[idx] = True
                    break

    rdfs(n - 1)

    return [vis[i] & rvis[i] for i in range(n)]


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

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

visitable = visitable_node(n, g, rg)
updated = [e for e in edges if visitable[e.f] and visitable[e.t]]

dist = bellman_ford(n, updated, 0)

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

Submission status: Wrong Answer

Solution code:

# グラフ定義
class Edge:
    def __init__(self, f: int, t: int, cost: int):
        self._t = t
        self._cost = cost
        self._f = f

    @property
    def t(self):
        return self._t

    @property
    def f(self):
        return self._f

    @property
    def cost(self):
        return self._cost


INF = 1 << 28


# ここから本実装
def bellman_ford(n: int, edges, s: int):
    dist = [INF for _ in range(n)]
    dist[s] = 0

    for i in range(n):
        for e in edges:

            if dist[e.t] > dist[e.f] + e.cost:
                dist[e.t] = dist[e.f] + e.cost
                if i == n - 1:
                    return None

    return dist


def visitable_node(n, g, rg):

    vis = [False] * n

    def dfs(idx):
        vis[idx] = True
        f = True
        while f:
            f = False
            for e in g[idx]:
                if not vis[e]:
                    idx = e
                    f = True
                    vis[idx] = True
                    break

    dfs(0)

    rvis = [False] * n

    def rdfs(idx):
        rvis[idx] = True
        f = True
        while f:
            f = False
            for e in rg[idx]:
                if not rvis[e]:
                    idx = e
                    f = True
                    rvis[idx] = True
                    break

    rdfs(n - 1)

    return [vis[i] & rvis[i] for i in range(n)]


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

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

visitable = visitable_node(n, g, rg)
updated = [e for e in edges if visitable[e.f] and visitable[e.t]]

dist = bellman_ford(n, edges, 0)

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

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
ABC = []
for _ in range(M):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
    
import sys
INT_MAX = sys.maxsize # 9223372036854775807        
    
def bellman_ford(edges,num_v,source):

    dist=[INT_MAX] * num_v
    dist[source]=0

    #辺の緩和
    has_negative = False
    for i in range(num_v):
        for edge in edges:
            if edge[0] != INT_MAX and dist[edge[1]] > dist[edge[0]] + edge[2]:
                if dist[edge[0]] != INT_MAX:
                    dist[edge[1]] = dist[edge[0]] + edge[2]
                if i==num_v-1:
                    has_negative = True
    
    if has_negative == False:
        return dist
    
    negative = [False] * num_v
    for i in range(num_v):
        for edge in edges:
            if dist[edge[1]]>dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                negative[edge[1]] = True
                
    for i, is_negative in enumerate(negative):
        if is_negative:
            dist[i] = -1 * INT_MAX
        
    return dist

edges = ABC[:]
edges = [(a, b, -c+P) for a, b, c in edges]

shortest_path = bellman_ford(edges, N+1, 1)
rev_shortest_path = bellman_ford(edges, N+1, N)
ans = -1 * shortest_path[N]
rev = -1 * rev_shortest_path[1]
#print(shortest_path)
if ans == -INT_MAX:
    print(-1)
elif ans < 0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:

# グラフ定義
class Edge:
    def __init__(self, f: int, t: int, cost: int):
        self._t = t
        self._cost = cost
        self._f = f

    @property
    def t(self):
        return self._t

    @property
    def f(self):
        return self._f

    @property
    def cost(self):
        return self._cost


INF = 1 << 28


# ここから本実装
def bellman_ford(n: int, edges, s: int):
    dist = [INF for _ in range(n)]
    dist[s] = 0

    for i in range(n):
        for e in edges:

            if dist[e.t] > dist[e.f] + e.cost:
                dist[e.t] = dist[e.f] + e.cost
                if i == n - 1:
                    return None

    return dist


def visitable_node(n, g, rg):

    vis = [False] * n

    def dfs(idx):
        vis[idx] = True
        for e in g[idx]:
            if not vis[e]:
                dfs(e)

    dfs(0)

    rvis = [False] * n

    def rdfs(idx):
        rvis[idx] = True
        for e in rg[idx]:
            if not rvis[e]:
                rdfs(e)

    rdfs(n - 1)

    return [vis[i] & rvis[i] for i in range(n)]


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

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

# visitable = visitable_node(n, g, rg)
# print(g, rg)
# updated = [e for e in edges if visitable[e.f] and visitable[e.t]]
# 
# dist = bellman_ford(n, updated, 0)
dist = bellman_ford(n, edges, 0)
if dist:
    print(max(0, -dist[n - 1]))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N,M,P = map(int,input().split())
make_Edge = lambda xs:[int(xs[0])-1,int(xs[1])-1,P-int(xs[2])] 
Edges =[make_Edge(input().split()) for i in range(M)]
 
def bellman_ford(N,edges):
    #各点の重み
    inf = float('inf')
    dist = [inf]*N
    dist[0] = 0
    for i in range(N):
        for e in edges:
            if dist[e[0]] != inf and dist[e[1]] > dist[e[0]]+e[2]:
                dist[e[1]] = dist[e[0]]+e[2]
                if i==N-1 and e[1] == N-1:
                    return None
    return -dist[N-1]
  
ans = bellman_ford(N,Edges)
if ans is None:
    print(-1)
elif ans < 0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import namedtuple
#Edge v1,v2,cost
Edge = namedtuple('Edge','v1 v2 score') 
N,M,P = map(int,input().split())
make = lambda xs:Edge(int(xs[0])-1,int(xs[1])-1,P-int(xs[2]))
Edges =[make(input().split()) for i in range(M)]
def bellman_ford(N,edges):
    #各点の重み
    inf = float('inf')
    dist = [inf]*N
    dist[0] = 0
    for i in range(N):
        for e in edges:
            if dist[e.v1] != inf and dist[e.v2] > dist[e.v1]+e.score:
                dist[e.v2] = dist[e.v1] + e.score
                if i==N-1 and e.v2 == N-1:
                    return None
    return -dist[N-1]

ans = bellman_ford(N,Edges)
if ans is None:
	print("-1")
elif ans < 0:
	print("0")
else:
	print(ans)

Submission status: Wrong Answer

Solution code:
N,M,P = map(int,input().split())
make_Edge = lambda xs:[int(xs[0])-1,int(xs[1])-1,P-int(xs[2])] 
Edges =[make_Edge(input().split()) for i in range(M)]

def bellman_ford(N,edges):
    #各点の重み
    inf = float('inf')
    dist = [inf]*N
    dist[0] = 0
    for i in range(N):
        for e in edges:
            if dist[e[0]] != inf and dist[e[1]] > dist[e[0]]+e[2]:
                dist[e[1]] = dist[e[0]]+e[2]
                if i==N-1 and e[1] == N-1:
                    return None
    return -dist[N-1]
  
ans = bellman_ford(N,Edges)
if ans is None:
    print(-1)
elif ans < 0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N,M,P = map(int,input().split())
make_Edge = lambda xs:[int(xs[0])-1,int(xs[1])-1,P-int(xs[2])] 
Edges =[make_Edge(input().split()) for i in range(M)]

def bellman_ford(N,edges):
    #各点の重み
    inf = 1000000
    dist = [inf]*N
    dist[0] = 0
    for i in range(N):
        for e in edges:
            if dist[e[0]] != inf and dist[e[1]] > dist[e[0]]+e[2]:
                dist[e[1]] = dist[e[0]]+e[2]
                if i==N-1 and e[1] == N-1:
                    return None
    return -dist[N-1]
  
ans = bellman_ford(N,Edges)
if ans is None:
    print(-1)
elif ans < 0:
    print(0)
else:
    print(ans)