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 math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,m,p = LI()
    abc = [LI() for _ in range(m)]
    er = collections.defaultdict(set)
    err = collections.defaultdict(set)
    for a,b,c in abc:
        er[b].add(a)
        err[a].add(b)

    v = set([n])
    q = [n]
    qi = 0
    while len(q) > qi:
        t = q[qi]
        qi += 1
        for b in er[t]:
            if b not in v:
                v.add(b)
                q.append(b)

    u = set([1])
    q = [1]
    qi = 0
    while len(q) > qi:
        t = q[qi]
        qi += 1
        for b in err[t]:
            if b not in u:
                u.add(b)
                q.append(b)

    v &= u

    e = collections.defaultdict(list)
    for a,b,c in abc:
        if a not in v or b not in v:
            continue
        e[a].append((b,c-p))

    def search(s):
        d = collections.defaultdict(lambda: -inf)
        q = []
        d[s] = 0
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            mk, u = heapq.heappop(q)
            k = -mk
            if v[u]:
                continue
            v[u] = True

            for uv, ud in e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv] < vd:
                    d[uv] = vd
                    heapq.heappush(q, (-vd, uv))

        return d

    for k in list(e.keys()):
        t = [0] * (n+1)
        def f(i, c):
            for ni,nc in e[i]:
                if ni < i:
                    continue
                nd = c + nc
                if ni == k:
                    if nd > 0:
                        return True
                    continue
                if t[ni] == 1:
                    continue
                t[ni] = 1
                r = f(ni, nd)
                if r:
                    return True
                t[ni] = 0
            return False

        if f(k, 0):
            return -1

    d = search(1)
    return max(0, d[n])

print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,m,p = LI()
    abc = [LI() for _ in range(m)]
    er = collections.defaultdict(set)
    err = collections.defaultdict(set)
    for a,b,c in abc:
        er[b].add(a)
        err[a].add(b)

    v = set([n])
    q = [n]
    qi = 0
    while len(q) > qi:
        t = q[qi]
        qi += 1
        for b in er[t]:
            if b not in v:
                v.add(b)
                q.append(b)

    u = set([1])
    q = [1]
    qi = 0
    while len(q) > qi:
        t = q[qi]
        qi += 1
        for b in err[t]:
            if b not in u:
                u.add(b)
                q.append(b)

    v &= u

    e = collections.defaultdict(list)
    for a,b,c in abc:
        if a not in v or b not in v:
            continue
        e[a].append((b,c-p))

    def search(s, lf):
        d = collections.defaultdict(lambda: -inf)
        vv = collections.defaultdict(lambda: -inf)
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            mk, u = heapq.heappop(q)
            k = -mk
            if v[u]:
                if lf and vv[u] < k:
                    return True
                continue
            v[u] = True
            vv[u] = k

            for uv, ud in e[u]:
                if lf:
                    if uv < s:
                        continue
                elif v[uv]:
                    continue
                vd = k + ud
                if d[uv] < vd:
                    d[uv] = vd
                    heapq.heappush(q, (-vd, uv))

        if lf:
            return False
        return d

    for k in list(e.keys()):
        d = search(k, True)
        if d:
            return -1

    d = search(1, False)
    return max(0, d[n])

print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,m,p = LI()
    abc = [LI() for _ in range(m)]
    er = collections.defaultdict(set)
    err = collections.defaultdict(set)
    for a,b,c in abc:
        er[b].add(a)
        err[a].add(b)

    v = set([n])
    q = [n]
    qi = 0
    while len(q) > qi:
        t = q[qi]
        qi += 1
        for b in er[t]:
            if b not in v:
                v.add(b)
                q.append(b)

    u = set([1])
    q = [1]
    qi = 0
    while len(q) > qi:
        t = q[qi]
        qi += 1
        for b in err[t]:
            if b not in u:
                u.add(b)
                q.append(b)

    v &= u

    e = collections.defaultdict(list)
    for a,b,c in abc:
        if a not in v or b not in v:
            continue
        e[a].append((b,c-p))

    def search(s, lf):
        d = collections.defaultdict(lambda: -inf)
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        sf = True
        while len(q):
            mk, u = heapq.heappop(q)
            k = -mk
            if v[u]:
                continue
            if not sf:
                v[u] = True
            sf = False

            for uv, ud in e[u]:
                if v[uv] or (lf and uv < s):
                    continue
                vd = k + ud
                if d[uv] < vd:
                    d[uv] = vd
                    heapq.heappush(q, (-vd, uv))

        return d

    for k in list(e.keys()):
        d = search(k, True)
        if d[k] > 0:
            return -1

    d = search(1, False)
    return max(0, d[n])

print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,m,p = LI()
    abc = [LI() for _ in range(m)]
    er = collections.defaultdict(set)
    err = collections.defaultdict(set)
    for a,b,c in abc:
        er[b].add(a)
        err[a].add(b)

    v = set([n])
    q = [n]
    qi = 0
    while len(q) > qi:
        t = q[qi]
        qi += 1
        for b in er[t]:
            if b not in v:
                v.add(b)
                q.append(b)

    u = set([1])
    q = [1]
    qi = 0
    while len(q) > qi:
        t = q[qi]
        qi += 1
        for b in err[t]:
            if b not in u:
                u.add(b)
                q.append(b)

    v &= u

    e = collections.defaultdict(list)
    for a,b,c in abc:
        if a not in v or b not in v:
            continue
        e[a].append((b,c-p))

    def search(s):
        d = collections.defaultdict(lambda: -inf)
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        sf = True
        while len(q):
            mk, u = heapq.heappop(q)
            k = -mk
            if v[u]:
                continue
            if not sf:
                v[u] = True
            sf = False

            for uv, ud in e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv] < vd:
                    d[uv] = vd
                    heapq.heappush(q, (-vd, uv))

        return d

    dd = {}
    for k in list(e.keys()):
        d = search(k)
        if d[k] > 0:
            return -1
        dd[k] = d

    return max(0, dd[1][n])

print(main())

Submission status: Wrong Answer

Solution code:
import heapq
def dijkstra(G, s, st):
    dist = [float("inf")]*len(G)
    q = []
    dist[s] = 0
    heapq.heapify(q)
    heapq.heappush(q, [0, s])
    cnt = 0
    while 0 < len(q):
        pre_cost, p = heapq.heappop(q)
        if dist[p] < pre_cost:
            continue
        for v, d in G[p]:
            if dist[v] > dist[p] + d and v in st:
                dist[v] = dist[p] + d
                heapq.heappush(q, [dist[v], v])
                cnt += 1
        if 10000 < cnt:
            return None
    return dist

def main():
    n, m, p = map(int, input().split())
    G = [[] for _ in range(n)]
    U = [[] for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        G[a-1].append([b-1, -c + p])
        U[b-1].append(a-1)
    st = set()
    st.add(n-1)
    q = [n-1]
    while len(q) > 0:
        t = q.pop(-1)
        for v in U[t]:
            if not v in st:
                q.append(v)
                st.add(v)
    dist = dijkstra(G, 0, st)
    if dist is None:
        print(-1)
    else:
        print(max(0, -dist[n-1]))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    import sys
    input = sys.stdin.readline

    inf = 1 << 60

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

    to = tuple(set() for _ in range(N))
    ot = tuple(set() for _ in range(N))
    for _ in range(M):
        a, b, c = map(int, input().split())
        a -= 1
        b -= 1
        to[a].add((b, -(c - P)))  # to,coin(負辺)
        ot[b].add((a, -(c - P)))  # to,coin(負辺)

    def reach(s, g, result):
        dq = deque()
        dq.append(s)
        while dq:
            v = dq.popleft()
            for u, _ in g[v]:
                if result[u]: continue
                result[u] = 1
                dq.append(u)

    reachable_from_N = [0] * N
    reachable_from_N[N - 1] = 1
    reach(N - 1, ot, reachable_from_N)

    reachable_from_1 = [0] * N
    reachable_from_1[0] = 1
    reach(0, to, reachable_from_1)

    unreachable = tuple(1 ^ (rn & r1) for rn, r1 in zip(reachable_from_N, reachable_from_1))

    def bellman_ford(dist):
        for _ in range(N):
            not_updated = True
            for v in range(N):
                for u, c in to[v]:
                    if unreachable[u]: continue
                    if (dist[v] == inf) or (dist[v] + c >= dist[u]): continue
                    dist[u] = dist[v] + c
                    not_updated = False
            if not_updated: return

    dist = [inf] * N
    dist[0] = 0
    bellman_ford(dist=dist)

    # 負閉路の検知
    ret = dist[N - 1]
    bellman_ford(dist=dist)

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq
def dijkstra(G, s, st):
    dist = [float("inf")]*len(G)
    q = []
    dist[s] = 0
    heapq.heapify(q)
    heapq.heappush(q, [0, s])
    cnt = 0
    while 0 < len(q):
        pre_cost, p = heapq.heappop(q)
        if dist[p] < pre_cost:
            continue
        for v, d in G[p]:
            if dist[v] > dist[p] + d and v in st:
                dist[v] = dist[p] + d
                heapq.heappush(q, [dist[v], v])
                cnt += 1
        if len(G)*5 < cnt:
            return None
    return dist

def main():
    n, m, p = map(int, input().split())
    G = [[] for _ in range(n)]
    U = [[] for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        G[a-1].append([b-1, -c + p])
        U[b-1].append(a-1)
    st = set()
    st.add(n-1)
    q = [n-1]
    while len(q) > 0:
        t = q.pop(-1)
        for v in U[t]:
            if not v in st:
                q.append(v)
                st.add(v)
    dist = dijkstra(G, 0, st)
    if dist is None:
        print(-1)
    else:
        print(max(0, -dist[n-1]))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import heapq
def dijkstra(G, s, st):
    dist = [float("inf")]*len(G)
    q = []
    dist[s] = 0
    heapq.heapify(q)
    heapq.heappush(q, [0, s])
    cnt = 0
    while 0 < len(q):
        pre_cost, p = heapq.heappop(q)
        if dist[p] < pre_cost:
            continue
        for v, d in G[p]:
            if dist[v] > dist[p] + d and v in st:
                dist[v] = dist[p] + d
                heapq.heappush(q, [dist[v], v])
                cnt += 1
        if len(G) < cnt:
            return None
    return dist

def main():
    n, m, p = map(int, input().split())
    G = [[] for _ in range(n)]
    U = [[] for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        G[a-1].append([b-1, -c + p])
        U[b-1].append(a-1)
    st = set()
    st.add(n-1)
    q = [n-1]
    while len(q) > 0:
        t = q.pop(-1)
        for v in U[t]:
            if not v in st:
                q.append(v)
                st.add(v)
    dist = dijkstra(G, 0, st)
    if dist == None:
        print(-1)
    else:
        print(max(0, -dist[n-1]))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def Bellman_Ford(s, g, inf=1 << 60):
    # https://tjkendev.github.io/procon-library/python/graph/bellman-ford.html
    N = len(g)
    dist = [inf] * N
    dist[s] = 0
    for _ in range(N):
        not_updated = True
        for v in range(N):
            for u, c in g[v]:
                if (dist[v] == inf) or (dist[v] + c >= dist[u]): continue
                dist[u] = dist[v] + c
                not_updated = False

        if not_updated:
            return max(0, -dist[N - 1])

    # 負閉路が存在する
    ret = dist[N - 1]
    for _ in range(N):
        not_updated = True
        for v in range(N):
            for u, c in g[v]:
                if (dist[v] == inf) or (dist[v] + c >= dist[u]): continue
                dist[u] = dist[v] + c
                not_updated = False
        if not_updated: break

    if ret == dist[N - 1]:
        return max(0, -dist[N - 1])
    else:
        return -1


def main():
    import sys
    input = sys.stdin.readline

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

    g = tuple(set() for _ in range(N))
    for _ in range(M):
        a, b, c = map(int, input().split())
        a -= 1
        b -= 1
        g[a].add((b, -(c - P)))  # to,coin(負辺)

    ret = Bellman_Ford(0, g)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def Bellman_Ford(s, g, inf=1 << 60):
    # https://tjkendev.github.io/procon-library/python/graph/bellman-ford.html
    N = len(g)
    dist = [inf] * N
    dist[s] = 0
    for _ in range(N):
        not_updated = True
        for v in range(N):
            for u, c in g[v]:
                if (dist[v] == inf) or (dist[v] + c >= dist[u]): continue
                dist[u] = dist[v] + c
                not_updated = False

        if not_updated:
            return 0 if dist[N - 1] == inf else -dist[N - 1]

    # 負閉路が存在する
    ret = dist[N - 1]
    for _ in range(N):
        not_updated = True
        for v in range(N):
            for u, c in g[v]:
                if (dist[v] == inf) or (dist[v] + c >= dist[u]): continue
                dist[u] = dist[v] + c
                not_updated = False
        if not_updated: break

    if ret == dist[N - 1]:
        ret = 0 if dist[N - 1] == inf else -dist[N - 1]
        return max(0, ret)
    else:
        return -1


def main():
    import sys
    input = sys.stdin.readline

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

    g = tuple(set() for _ in range(N))
    for _ in range(M):
        a, b, c = map(int, input().split())
        a -= 1
        b -= 1
        g[a].add((b, -(c - P)))  # to,coin,負辺

    ret = Bellman_Ford(0, g)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def Bellman_Ford(s, g, inf=1 << 60):
    # https://tjkendev.github.io/procon-library/python/graph/bellman-ford.html
    N = len(g)
    dist = [inf] * N
    dist[s] = 0
    for _ in range(N):
        not_updated = True
        for v in range(N):
            for u, c in g[v]:
                if (dist[v] == inf) or (dist[v] + c >= dist[u]): continue
                dist[u] = dist[v] + c
                not_updated = False

        if not_updated:
            return -dist[N - 1]

    # 負閉路が存在する
    ret = dist[N - 1]
    for _ in range(N):
        not_updated = True
        for v in range(N):
            for u, c in g[v]:
                if (dist[v] == inf) or (dist[v] + c >= dist[u]): continue
                dist[u] = dist[v] + c
                not_updated = False
        if not_updated: break

    if ret == dist[N - 1]:
        return max(0, -ret)
    else:
        return -1


def main():
    import sys
    input = sys.stdin.readline

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

    g = tuple(set() for _ in range(N))
    for _ in range(M):
        a, b, c = map(int, input().split())
        a -= 1
        b -= 1
        g[a].add((b, -(c - P)))  # to,coin,負辺

    ret = Bellman_Ford(0, g)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,m,p=map(int,input().split())
E=[list(map(int,input().split())) for i in range(m)]
INF=float("inf")
dist=[INF]*n
dist[0]=0
for i in range(n):
  for fr,to,cost in E:
    fr,to=fr-1,to-1
    if fr==to:
      if fr!=n-1:
        if cost>p:
          print(0)
          exit(0)
      else:
        print(-1)
        exit(0)
    cost*=-1
    cost+=p
    if dist[fr]!=INF and dist[to]>dist[fr]+cost:
      dist[to]=dist[fr]+cost
print(0) if -dist[n-1]<0 else print(-dist[n-1])

Submission status: Wrong Answer

Solution code:
n,m,p=map(int,input().split())
E=[list(map(int,input().split())) for i in range(m)]
INF=float("inf")
dist=[INF]*n
dist[0]=0
for i in range(n):
  for fr,to,cost in E:
    fr,to=fr-1,to-1
    cost*=-1
    cost+=p
    if dist[fr]!=INF and dist[to]>dist[fr]+cost:
      dist[to]=dist[fr]+cost
      if i==n-1 and to==n-1:
        print(-1)
        exit(0)
print(0) if -dist[n-1]<0 else print(-dist[n-1])

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 BellmanFord(s):
    inf = 10**18
    dist=[inf for i in range(N)]
    dist[s]=0
    
    for i in range(N*3):
        for edge in Edges:
            if edge[0] != inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                if i>=N-1 and edge[1]==N-1:
                    return []
    
    return dist
d = BellmanFord(0)
if len(d)==0:
    print(-1)
else:
    print(max(0, -d[-1]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
#%%code

def resolve():
    N,M,P=pin()
    edge=[]#0-index
    for i in range(M):
        a,b,c=pin()
        edge.append((a-1,b-1,P-c))
    #print(edge)
    
    #Bellman–Ford algorithm?
    inf = float("inf")
    ans=[inf]*N#0-index
    ans[0]=0

    for i in range(N*2):
        for j in range(M):
            t,f,p=edge[j]
            ans[f]=min(ans[f],ans[t]+p)
            if f==N-1 and ans[f]>ans[t]+p:
                ans[f]=0-inf
    #    print(ans)
        if i==N*2-2:
            temp=ans[-1]
        if i==N*2-1:
            if ans[-1]<temp:
                ans[-1]=0-inf
    t=(0-ans[-1])
    



    if t==inf:s=-1
    elif t<0:s=0
    else:s=t
    print(s)
#%%submit!
resolve()

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 BellmanFord(s):
    inf = 10**18
    dist=[inf for i in range(N)]
    dist[s]=0
    
    for i in range(N*2):
        for edge in Edges:
            if edge[0] != inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                if i>=N-1 and edge[1]==N-1:
                    return []
    
    return dist
d = BellmanFord(0)
if len(d)==0:
    print(-1)
else:
    print(max(0, -d[-1]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
#%%code

def resolve():
    N,M,P=pin()
    edge=[]#0-index
    for i in range(M):
        a,b,c=pin()
        edge.append((a-1,b-1,P-c))
    #print(edge)
    
    #Bellman–Ford algorithm?
    inf = float("inf")
    ans=[inf]*N#0-index
    ans[0]=0

    for i in range(N*2):
        for j in range(M):
            t,f,p=edge[j]
            ans[f]=min(ans[f],ans[t]+p)
            if ans[f]>ans[t]+p:
                ans[f]=0-inf
    #    print(ans)
        if i==N*2-2:
            temp=ans[-1]
        if i==N*2-1:
            if ans[-1]<temp:
                ans[-1]=0-inf
    t=(0-ans[-1])
    



    if t==inf:s=-1
    elif t<0:s=0
    else:s=t
    print(s)
#%%submit!
resolve()

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 BellmanFord(s):
    inf = 10**18
    dist=[inf for i in range(N)]
    dist[s]=0
    
    for i in range(N*2):
        for edge in Edges:
            if edge[0] != inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                if i==N-1 and edge[1]==N-1:
                    return []
    
    return dist
d = BellmanFord(0)
if len(d)==0:
    print(-1)
else:
    print(max(0, -d[-1]))

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 BellmanFord(s):
    inf = 10**18
    dist=[inf for i in range(N)]
    dist[s]=0
    
    for i in range(N):
        for edge in Edges:
            if edge[0] != inf and dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                if i==N-1 and edge[1]==N-1:
                    return []
    
    return dist
d = BellmanFord(0)
if len(d)==0:
    print(-1)
else:
    print(max(0, -d[-1]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
#%%code

def resolve():
    N,M,P=pin()
    edge=[]#0-index
    for i in range(M):
        a,b,c=pin()
        edge.append((a-1,b-1,P-c))
    #print(edge)
    
    #Bellman–Ford algorithm?
    inf = float("inf")
    ans=[inf]*N#0-index
    ans[0]=0

    for i in range(N*2):
        for j in range(M):
            t,f,p=edge[j]
            ans[f]=min(ans[f],ans[t]+p)
            if ans[f]>ans[t]+p:
                ans[f]=0-inf
    t=(0-ans[-1])
    if t==inf:s=-1
    elif t<0:s=0
    else:s=t
    print(s)
#%%submit!
resolve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
#%%code

def resolve():
    N,M,P=pin()
    edge=[]#0-index
    for i in range(M):
        a,b,c=pin()
        edge.append((a-1,b-1,P-c))
    #print(edge)
    
    #Bellman–Ford algorithm?
    inf = float("inf")
    ans=[inf]*N#0-index
    ans[0]=0

    for i in range(N):
        for j in range(M):
            t,f,p=edge[j]
            ans[f]=min(ans[f],ans[t]+p)
            if ans[f]>ans[t]+p:
                ans[f]=0-inf
    t=(0-ans[-1])
    if t==inf:s=-1
    elif t<0:s=0
    else:s=t
    print(s)
#%%submit!
resolve()

Submission status: Wrong Answer

Solution code:
def solve():
    ans = 0
    N, M, P = map(int, input().split())
    edge = []
    for i in range(M):
        a,b,c = map(int, input().split())
        edge.append([P-c,a-1,b-1])
    d = [10**10]*N
    d[0]=0
    n = 0
    while True:
        n += 1
        update = False
        if n==N:
            updateN = False
        for i in range(M):
            w,u,v = edge[i]
            if d[u]<10**10 and d[v]>d[u]+w:
                d[v] = d[u]+w
                update = True
                if n>=N and v==N-1:
                    updateN = True
        if not update:
            break
        if n==N+M:
            if updateN:
                return -1
            break
    return max(0,-d[-1])
print(solve())

Submission status: Wrong Answer

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

def BellmanFord(E, N, source):
    inf = 10**12
    d = [inf for _ in range(N)]
    d[source] = 0
    
    for i in range(N+1):
        for j in range(N):
            for edge in E[j]:
                if d[edge[0]] > d[j] + edge[1]:
                    d[edge[0]] = d[j] + edge[1]
                    if i == N and edge[0] == N-1:
                        return -1
    
    return d

d = BellmanFord(E, N, 0)
if d == -1:
    print(-1)
else:
    print(max(0, -d[-1]))