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  #危険！基本オフにしろ！


def BELLMANFORD(point,n,d,p):
    cost=[1e18]*(n+1)
    cost[point]=0
    for i in range(n-1):
        for (nownode,nextnode,c) in d:
            if cost[nextnode]>cost[nownode]+c+p:  #infが定数なので論理が逆
                cost[nextnode]=cost[nownode]+c+p
    for (nownode,nextnode,c) in d:
        if cost[nextnode]>cost[nownode]+c+p:
            if nextnode==n:
                return -1
            cost[nextnode]=cost[nownode]+c+p
            
    return -cost[n]


def main():
    n,m,p=map(int,input().split())
    
    d=[]
    for i in range(m):
        a,b,c=map(int,input().split())
        d.append((a,b,-c))
    
    x=BELLMANFORD(1,n,d,p)
    if x==-1:
        print(-1)
    else:
        print(max(x,0))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline  #危険！基本オフにしろ！


def BELLMANFORD(point,n,d,p):
    cost=[1e18]*(n+1)
    cost[point]=0
    for i in range(n-1):
        for (nownode,nextnode,c) in d:
            if cost[nextnode]>cost[nownode]+c+p:  #infが定数なので論理が逆
                cost[nextnode]=cost[nownode]+c+p
        print(cost)
    for (nownode,nextnode,c) in d:
        if cost[nextnode]>cost[nownode]+c+p:
            if nextnode==n:
                return -1
            cost[nextnode]=cost[nownode]+c+p
            
    return -cost[n]


def main():
    n,m,p=map(int,input().split())
    
    d=[]
    for i in range(m):
        a,b,c=map(int,input().split())
        d.append((a,b,-c))
    
    x=BELLMANFORD(1,n,d,p)
    if x==-1:
        print(-1)
    else:
        print(max(x,0))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline  #危険！基本オフにしろ！


def BELLMANFORD(point,n,d,p):
    cost=[1e18]*(n+1)
    cost[point]=0
    for i in range(n-1):
        for (nownode,nextnode,c) in d:
            if cost[nextnode]>cost[nownode]+c+p:  #infが定数なので論理が逆
                cost[nextnode]=cost[nownode]+c+p
#        print(cost)
    for (nownode,nextnode,c) in d:
        if cost[nextnode]>cost[nownode]+c+p:
            return -1
            #cost[nextnode]=cost[nownode]+c
            
    return -cost[n]


def main():
    n,m,p=map(int,input().split())
    
    d=[]
    for i in range(m):
        a,b,c=map(int,input().split())
        d.append((a,b,-c))
    
    print(BELLMANFORD(1,n,d,p))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import* #defaultdict Counter deque appendleft
import sys
input=sys.stdin.readline  #危険！基本オフにしろ！
 
d=defaultdict(list)
n,m,p=map(int,input().split())
 
for s in range(m):
    a,b,c=map(int,input().split())
    d[a].append([b,c])
 
def BELLMANFORD(point,d,p):
    cost       = {node:-float("inf") for node in range(1,n+1)}
    cost[point]= 0
    for i in range(n-1):                                  #経路の長さは最大n-1であるため
        for now,lis in d.items():
            for nex,c in lis:
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p
 
    negative=[0]*n                                        #負閉路の長さは最大nであるため
    
    for i in range(n):                  #閉路判定 negativeの1は閉路に入っている部分を表す
        for now,lis in d.items():
            for nex,c in lis:
                if negative[now-1]==1:
                    negative[nex-1]=1
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p
                    negative[now-1]=1
 
    return cost,negative
 
def main():
    x,y=(BELLMANFORD(1,d,p))
    print(x,y)
    if y[n-1]==1:
        print(-1)
    else:
        print(max(x[n],0))
 
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import* #defaultdict Counter deque appendleft
import sys
input=sys.stdin.readline  #危険！基本オフにしろ！

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

for s in range(m):
    a,b,c=map(int,input().split())
    d[a].append([b,c])

def BELLMANFORD(point,d,p):
    cost       = {node:-100000000000000000 for node in range(1,n+1)}
    cost[point]= 0
    for i in range(n-1):                                  #経路の長さは最大n-1であるため
        for now,lis in d.items():
            for nex,c in lis:
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p

    negative=[0]*n                                        #負閉路の長さは最大nであるため
    
    for i in range(n):                  #閉路判定 negativeの1は閉路に入っている部分を表す
        for now,lis in d.items():
            for nex,c in lis:
                if negative[now-1]==1:
                    negative[nex-1]=1
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p
                    negative[now-1]=1
 
    return cost,negative

def main():
    x,y=(BELLMANFORD(1,d,p))
    if y[n-1]==1:
        print(-1)
    else:
        print(max(x[n],0))

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import* #defaultdict Counter deque appendleft
import sys
input=sys.stdin.readline  #危険！基本オフにしろ！

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

for s in range(m):
    a,b,c=map(int,input().split())
    d[a].append([b,c])

def BELLMANFORD(point,d,p):
    cost       = {node:-1e18 for node in range(1,n+1)}
    cost[point]= 0
    for i in range(n-1):                                  #経路の長さは最大n-1であるため
        for now,lis in d.items():
            for nex,c in lis:
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p

    negative=[0]*n                                        #負閉路の長さは最大nであるため
    
    for i in range(n):                  #閉路判定 negativeの1は閉路に入っている部分を表す
        for now,lis in d.items():
            for nex,c in lis:
                if negative[now-1]==1:
                    negative[nex-1]=1
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p
                    negative[now-1]=1
 
    return cost,negative

def main():
    x,y=(BELLMANFORD(1,d,p))
    if y[n-1]==1:
        print(-1)
    else:
        print(max(x[n],0))

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import* #defaultdict Counter deque appendleft
import sys
input=sys.stdin.readline  #危険！基本オフにしろ！

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

for s in range(m):
    a,b,c=map(int,input().split())
    d[a].append([b,c])

def BELLMANFORD(point,d,p):
    cost       = {node:-1e7 for node in range(1,n+1)}
    cost[point]= 0
    for i in range(n-1):                                  #経路の長さは最大n-1であるため
        for now,lis in d.items():
            for nex,c in lis:
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p

    negative=[0]*n                                        #負閉路の長さは最大nであるため
    
    for i in range(n):                  #閉路判定 negativeの1は閉路に入っている部分を表す
        for now,lis in d.items():
            for nex,c in lis:
                if negative[now-1]==1:
                    negative[nex-1]=1
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p
                    negative[now-1]=1
 
    return cost,negative

def main():
    x,y=(BELLMANFORD(1,d,p))
    if y[n-1]==1:
        print(-1)
    else:
        print(max(x[n],0))

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import* #defaultdict Counter deque appendleft
import sys
input=sys.stdin.readline  #危険！基本オフにしろ！

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

for s in range(m):
    a,b,c=map(int,input().split())
    d[a].append([b,c])

def BELLMANFORD(point,d,p):
    cost       = {node:-1e5-10 for node in range(1,n+1)}
    cost[point]= 0
    for i in range(n-1):                                  #経路の長さは最大n-1であるため
        for now,lis in d.items():
            for nex,c in lis:
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p

    negative=[0]*n                                        #負閉路の長さは最大nであるため
    
    for i in range(n):                  #閉路判定 negativeの1は閉路に入っている部分を表す
        for now,lis in d.items():
            for nex,c in lis:
                if negative[now-1]==1:
                    negative[nex-1]=1
                if cost[nex]<cost[now]+c-p:
                    cost[nex]=cost[now]+c-p
                    negative[now-1]=1
 
    return cost,negative

def main():
    x,y=(BELLMANFORD(1,d,p))
    if y[n-1]==1:
        print(-1)
    else:
        print(max(x[n],0))

    
if __name__ == '__main__':
    main()

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))

INF = 10 ** 9

coin_map = [-INF] * (N+1)
coin_map[1] = 0
way = [None] * (N+1)

for i in range(N-1):
    for a, b, c in edges:
        if coin_map[b] < coin_map[a] + c:
            coin_map[b] = coin_map[a] + c
            way[b] = a

ans = coin_map[N]

for i in range(3*N):
    for a, b, c in edges:
        if coin_map[b] < coin_map[a] + c:
            coin_map[b] = coin_map[a] + c
            way[b] = a

if ans != coin_map[N]:
    print(-1)
else:
    print(max(0, coin_map[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))

INF = 10 ** 9

coin_map = [-INF] * (N+1)
coin_map[1] = 0
way = [None] * (N+1)

for i in range(N-1):
    for a, b, c in edges:
        if coin_map[b] < coin_map[a] + c:
            coin_map[b] = coin_map[a] + c
            way[b] = a

ans = coin_map[N]

for i in range(N):
    for a, b, c in edges:
        if coin_map[b] < coin_map[a] + c:
            coin_map[b] = coin_map[a] + c
            way[b] = a

if ans != coin_map[N]:
    print(-1)
else:
    print(max(0, coin_map[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))

INF = 10 ** 9

coin_map = [-INF] * (N+1)
coin_map[1] = 0
way = [None] * (N+1)

for i in range(N-1):
    for a, b, c in edges:
        if coin_map[b] < coin_map[a] + c:
            coin_map[b] = coin_map[a] + c
            way[b] = a

path = [N]
now = N
while now == 1:
    now = way[now]
    path.append(now)

for i in range(N-1):
    for a, b, c in edges:
        if coin_map[b] < coin_map[a] + c:
            if a in path or b in path:
                print(-1)
                exit()

print(max(0, coin_map[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))

INF = 10 ** 9

coin_map = [-INF] * (N+1)
coin_map[1] = 0
way = [None] * (N+1)

for i in range(N-1):
    for a, b, c in edges:
        if coin_map[b] < coin_map[a] + c:
            coin_map[b] = coin_map[a] + c
            way[b] = a

path = [N]
now = N
while now == 1:
    now = way[now]
    path.append(now)

for a, b, c in edges:
    if coin_map[b] < coin_map[a] + c:
        if a in path or b in path:
            print(-1)
            exit()

print(max(0, coin_map[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))

INF = 10 ** 9

coin_map = [-INF] * (N+1)
coin_map[1] = 0

loop = False
for i in range(N):
    for a, b, c in edges:
        if coin_map[b] < coin_map[a] + c:
            coin_map[b] = coin_map[a] + c
            if i == N - 1 and b == N:
                loop = True
                break


if loop:
    print(-1)
else:
    print(max(0, coin_map[N]))

Submission status: Wrong Answer

Solution code:
def bellman_ford(num_of_node, edge, start, goal):
    distance = [-float('INF')] * num_of_node
    distance[start] = 0

    for i in range(num_of_node):
        for node, to, value in edge:
            if distance[to] < distance[node] + value:
                distance[to] = distance[node] + value
            else:
                continue

    answer = distance[goal]

    for i in range(num_of_node):
        for node, to, value in edge:
            if distance[to] < distance[node] + value:
                distance[to] = distance[node] + value
            else:
                continue

    if distance[goal] != answer or answer == -float('INF'):
        answer = -1
    else:
        answer = max(0, answer)
    return answer


n, m, p = map(int, input().split())
edge = []
for i in range(m):
    node, to, value = map(int, input().split())
    edge.append([node-1, to-1, value-p])

print(bellman_ford(n, edge, 0, n-1))

Submission status: Wrong Answer

Solution code:
def bellman_ford(num_of_node, edge, start, goal):
    distance = [-float('INF')] * num_of_node
    distance[start] = 0

    for i in range(num_of_node):
        for node, to, value in edge:
            if distance[to] < distance[node] + value:
                distance[to] = distance[node] + value
            else:
                continue

    answer = distance[goal]

    for node, to, cost in edge:
        if distance[to] < distance[node] + cost:
            distance[to] = distance[node] + cost
        else:
            continue

    if distance[goal] != answer or answer == -float('INF'):
        answer = -1
    else:
        answer = max(0, answer)
    return answer


n, m, p = map(int, input().split())
edge = []
for i in range(m):
    node, to, value = map(int, input().split())
    edge.append([node-1, to-1, value-p])

print(bellman_ford(n, edge, 0, n-1))

Submission status: Wrong Answer

Solution code:
def main():
    INF = float("inf")

    N, M, P, *ABC = map(int, open(0).read().split())

    F = [[] for _ in range(N)]
    B = [[] for _ in range(N)]
    E = [(a - 1, b - 1, P - c) for a, b, c in zip(*[iter(ABC)] * 3)]

    for a, b, c in E:
        F[a].append(b)
        B[b].append(a)

    def reachable(E, a):
        S = {a}
        stack = [a]
        while stack:
            s = stack.pop()
            for v in E[s]:
                if v not in S:
                    stack.append(v)
                    S.add(v)
        return S

    ok = reachable(F, 0) & reachable(B, N - 1)

    E = [(a, b, c) for a, b, c in E if a in ok and b in ok]

    D = [0] + [INF] * (N - 1)

    for a, b, c in E:
        D[b] = min(D[b], D[a] + c)

    ans = D[-1]

    for a, b, c in E:
        D[b] = min(D[b], D[a] + c)


    print(-1 if ans != D[-1] else max(-D[-1], 0))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    INF = float("inf")

    N, M, P, *ABC = map(int, open(0).read().split())

    F = [[] for _ in range(N)]
    B = [[] for _ in range(N)]
    E = [(a - 1, b - 1, P - c) for a, b, c in zip(*[iter(ABC)] * 3)]

    for a, b, c in E:
        F[a].append(b)
        B[b].append(a)

    def reachable(E, a):
        S = {a}
        stack = [a]
        while stack:
            s = stack.pop()
            for v in E[s]:
                if v not in S:
                    stack.append(v)
                    S.add(v)
        return S

    ok = reachable(F, 0) & reachable(B, N - 1)

    E = [(a, b, c) for a, b, c in E if a in ok and b in ok]

    D = [0] + [INF] * (N - 1)

    for a, b, c in E:
        D[b] = min(D[b], D[a] + c)

    ans = D[-1]

    for a, b, c in E:
        D[b] = min(D[b], D[a] + c)


    print(-1 if ans != D[-1] else max(-ans, 0))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(M)]

to_w = [set() for _ in range(N)]
for a, b, c in ABC:
    to_w[a - 1].add((b - 1, P - c))
reachable_from_start = {0}
Q = {0}
while Q:
    u = Q.pop()
    for v, _ in to_w[u]:
        if v not in reachable_from_start:
            reachable_from_start.add(v)
            Q.add(v)


from_w = [set() for _ in range(N)]
for a, b, c in ABC:
    from_w[b - 1].add((a - 1, P - c))
reachable_to_goal = {N - 1}
Q = {N - 1}
while Q:
    u = Q.pop()
    for v, _ in from_w[u]:
        if v not in reachable_to_goal:
            reachable_to_goal.add(v)
            Q.add(v)


reachable = reachable_from_start & reachable_to_goal


to_w = [set() for _ in range(N)]
for a, b, c in ABC:
    if a - 1 in reachable and b - 1 in reachable:
        to_w[a - 1].add((b - 1, P - c))
d = [float("inf")] * N
d[0] = 0
Q = {0}
while Q:
    u = Q.pop()
    refreshed = False
    for v, w in to_w[u]:
        tmp = d[u] + w
        if tmp < d[v]:
            d[v] = tmp
            refreshed = True
            if v not in Q:
                Q.add(v)
    if refreshed:
        d[u] = -float("inf")

if d[-1] == -float("inf"):
    print(-1)
else:
    print(max(0, -d[-1]))

Submission status: Wrong Answer

Solution code:
N, M, P = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(M)]

to_w = [set() for _ in range(N)]
for a, b, c in ABC:
    to_w[a - 1].add((b - 1, P - c))
reachable_from_start = {0}
Q = {0}
while Q:
    u = Q.pop()
    for v, _ in to_w[u]:
        if v not in reachable_from_start:
            reachable_from_start.add(v)
            Q.add(v)


from_w = [set() for _ in range(N)]
for a, b, c in ABC:
    from_w[b - 1].add((a - 1, P - c))
reachable_to_goal = {N - 1}
Q = {N - 1}
while Q:
    u = Q.pop()
    for v, _ in from_w[u]:
        if v not in reachable_to_goal:
            reachable_to_goal.add(v)
            Q.add(v)


reachable = reachable_from_start & reachable_to_goal


to_w = [set() for _ in range(N)]
for a, b, c in ABC:
    if a - 1 in reachable and b - 1 in reachable:
        to_w[a - 1].add((b - 1, P - c))
d = [float("inf")] * N
d[0] = 0
Q = {0}
while Q:
    u = Q.pop()
    for v, w in to_w[u]:
        tmp = d[u] + w
        if tmp < d[v]:
            d[v] = tmp
            d[u] = -float("inf")
            if v not in Q:
                Q.add(v)

if d[-1] == -float("inf"):
    print(-1)
else:
    print(max(0, -d[-1]))

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 5)

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

v = [0] * n
v[0] = 1
def bfs(x, depth):
    if depth >= n:
        return 1
    if x == n-1:
        return 0
    temp = 1
    for y in e[x]:
        if v[y] == 1 or x == y:
            continue
        ret = bfs(y, depth + 1)
        if ret == 0:
            v[y] = 1
            temp = 0
    return temp

bfs(0, 0)
x = 1
for i in range(n):
    if v[i] == 1:
        v[i] = x
        x += 1

x -= 1
#print(v)
inf = float('inf')
bf = [[inf for _ in range(x)] for _ in range(x)]
edge = []
for a, b, c in abc:
    va = v[a] - 1
    vb = v[b] - 1
    if va >= 0 and vb >= 0:
        edge.append([va, vb, c])

for i in range(x):
    bf[i][i] = 0

for i in range(x):
    for e_from, e_to, cost in edge:
        if bf[0][e_from] != inf:
            bf[0][e_to] = min(0, bf[0][e_to], bf[0][e_from] + cost)

#print(bf)

change = 0
for e_from, e_to, cost in edge:
    if bf[0][e_from] != inf:
        if bf[0][e_to] > bf[0][e_from] + cost:
            print(-1)
            exit()

print(- bf[0][-1])

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 5)

class BellmanFord():
    class Edge():
        def __init__(self, _from, _to, _cost):
            self.from_ = _from
            self.to = _to
            self.cost = _cost

    def __init__(self):
        self.edges = []  # 辺
        self.v_set = set()  # 頂点の集合

    @property
    def E(self):
        return len(self.edges)

    @property
    def V(self):
        return len(self.v_set)

    def add(self, _from, _to, _cost):
        self.edges.append(self.Edge(_from, _to, _cost))
        self.v_set.add(_from)
        self.v_set.add(_to)

    def shortest_path(self, s):
        d = [float("inf")] * self.V
        d[s] = 0

        while True:
            do_update = False
            for i in range(self.E):
                e = self.edges[i]
                if d[e.from_] != float("inf") and d[e.to] > d[e.from_] + e.cost:
                    d[e.to] = d[e.from_] + e.cost
                    do_update = True

            if not do_update: break

        return d

    def exist_negative_loop(self):
        d = [0] * self.V
        for i in range(self.V):
            for j in range(self.E):
                e = self.edges[j]
                if d[e.to] > d[e.from_] + e.cost:
                    d[e.to] = d[e.from_] + e.cost
                    if i == self.V - 1: return True
        return False

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

v = [0] * n
v[0] = 1
def bfs(x, depth):
    #print(x, depth, e[x])
    if depth >= n:
        return 1
    if x == n-1:
        return 0
    temp = 1
    for y in e[x]:
        #print(y)
        if v[y] == 1 or x == y:
            continue
        #print(x, y)
        ret = bfs(y, depth + 1)
        if ret == 0:
            v[y] = 1
            temp = 0
    return temp

bfs(0, 0)
x = 1
for i in range(n):
    if v[i] == 1:
        v[i] = x
        x += 1

bf = BellmanFord()
for a, b, c in abc:
    if v[a]!= 0 and v[b] != 0:
        bf.add(v[a]-1, v[b]-1, c)

if bf.exist_negative_loop():
    print(-1)
else:
    print(max(0, - bf.shortest_path(0)[-1]))

Submission status: Wrong Answer

Solution code:
INF = float("inf")

N, M, P, *ABC = map(int, open(0).read().split())

D = [INF] * (N + 1)
D[1] = 0

loop = False
for i in range(2 * N + 100):
    for a, b, c in zip(*[iter(ABC)] * 3):
        d = D[a] + P - c
        if D[b] > d:
            D[b] = d
            if b == N and i > N:
                loop = True

print(-1 if loop else max(0, -D[N]))

Submission status: Wrong Answer

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

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

inf = float('inf')
def search_by_bellman_ford(edges,V,start):
  dist = [inf]*V
  dist[start] = 0
  
  for i in range(V):
    for edge in edges:
      u,v,w = edge
      #if u == inf:
      if dist[u] == inf:
        continue
      if dist[v] > dist[u]+w:
        dist[v] = dist[u]+w
        if i == V-1:
          dist[v] = -inf # nagative cycle
  return dist

dist = search_by_bellman_ford(edges,N,0)
#print(dist)
ans = -dist[N-1]
print(max(0,ans) if ans != inf else -1)

Submission status: Wrong Answer

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

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

inf = float('inf')
def search_by_bellman_ford(edges,V,start):
  dist = [inf]*V
  dist[start] = 0
  
  for i in range(V):
    for edge in edges:
      u,v,w = edge
      #if u == inf:
      if dist[u] == inf:
        continue
      if dist[v] > dist[u]+w:
        dist[v] = dist[u]+w
        if i == V-1:
          dist[v] = -inf # nagative cycle
  return dist

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

Submission status: Wrong Answer

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

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

inf = float('inf')
def search_by_bellman_ford(edges,V,start):
  dist = [inf]*V
  dist[start] = 0
  
  for i in range(V):
    for edge in edges:
      u,v,w = edge
      if u == inf:
        continue
      if dist[v] > dist[u]+w:
        dist[v] = dist[u]+w
        if i == V-1:
          dist[v] = -inf # nagative cycle
  return dist

dist = search_by_bellman_ford(edges,N,0)
#print(dist)
ans = -dist[N-1]
print(max(0,ans) if ans != inf else -1)

Submission status: Wrong Answer

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

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

inf = float('inf')
def search_by_bellman_ford(edges,V,start):
  dist = [inf]*V
  dist[start] = 0
  
  for i in range(V):
    for edge in edges:
      u,v,w = edge
      if u == inf:
        continue
      if dist[v] > dist[u]+w:
        dist[v] = dist[u]+w
        if i == V-1:
          dist[v] = -inf # nagative cycle
  return dist

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

Submission status: Wrong Answer

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

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

inf = float('inf')
def search_by_bellman_ford(edges,V,start):
  dist = [inf]*V
  dist[start] = 0
  
  for i in range(V):
    for edge in edges:
      u,v,d = edge
      if u == inf:
        continue
      cost = dist[u]+d
      if dist[v] > cost:
        dist[v] = cost
        if i == V-1:
          dist[v] = -inf # nagative cycle
  return dist

dist = search_by_bellman_ford(edges,N,0)
ans = -min(0,dist[N-1])
#print(dist)
print(ans if ans != inf else -1)

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(m)]

for i in range(m):
    lst[i][2] = - (lst[i][2] - p)

d = [10**100] * (n+1)
d[1] = 0
rep = 0
loop = False

while True:
    update = False
    for edge in lst:
        a,b,c = edge
        if d[a] != 10**100 and d[b] > d[a]+ c:
            if rep >= n and b == n:
                loop = True
            d[b] = d[a] + c
            update = True
    if not update:
        break
    rep += 1
    if loop:
        break
    if rep > 3*n:
        break

if loop:
    print(-1)
elif -d[n] < 0:
    print(0)
else:
    print(-d[n])

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(m)]

for i in range(m):
    lst[i][2] = - (lst[i][2] - p)

d = [10**100] * (n+1)
d[1] = 0
rep = 0
loop = False

while True:
    update = False
    for edge in lst:
        a,b,c = edge
        if d[a] != 10**100 and d[b] > d[a]+ c:
            if rep >= n and b == n:
                loop = True
            d[b] = d[a] + c
            update = True
    if not update:
        break
    rep += 1
    if loop:
        break
    if rep > 2*n:
        break

if loop:
    print(-1)
elif -d[n] < 0:
    print(0)
else:
    print(-d[n])

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(m)]

for i in range(m):
    lst[i][2] = - (lst[i][2] - p)

d = [10**100] * (n+1)
d[1] = 0
rep = 0
loop = False

while True:
    update = False
    for edge in lst:
        a,b,c = edge
        if d[a] != 10**100 and d[b] > d[a]+ c:
            if rep >= n and b == n:
                loop = True
            d[b] = d[a] + c
            update = True
    if not update:
        break
    rep += 1
    if loop:
        break
    if rep > n:
        break

if loop:
    print(-1)
elif -d[n] < 0:
    print(0)
else:
    print(-d[n])

Submission status: Wrong Answer

Solution code:
n, m, p = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(m)]

for i in range(m):
    lst[i][2] = - (lst[i][2] - p)

d = [1000] * (n+1)
d[1] = 0
rep = 0
loop = False

while True:
    update = False
    for edge in lst:
        a,b,c = edge
        if d[a] != 1000 and d[b] > d[a]+ c:
            if rep >= n and b == n:
                loop = True
            d[b] = d[a] + c
            update = True
    if not update:
        break
    rep += 1
    if loop:
        break
    if rep > n:
        break

if loop:
    print(-1)
elif -d[n] < 0:
    print(0)
else:
    print(-d[n])