Submission #6839724


Source Code Expand

Copy
#!/usr/bin/env python3
import sys
import heapq
INF = float("inf")


# 有効グラフを仮定する。
class Graph(object):
    def __init__(self, N):
        self.N = N
        self.V = list(range(N))
        self.E = [[] for _ in range(N)]

    def add_edge(self, edge):
        """辺を加える。edgeは(始点, 終点、重み)からなるリスト
        重みがなければ、重み1とする。
        """
        if len(edge) == 2:
            edge.append(1)
        elif len(edge) != 3:
            print("error in add_edge")
            pass

        s, t, w = edge
        self.E[s].append([t, w])

        pass


def BellmanFord(g: Graph, s):
    """ベルマンフォード法による最短経路
    """
    N = g.N
    dist = [INF]*N
    dist[s] = 0
    prev = [None]*N

    # 辺の緩和
    for _ in range(N):
        for from_ in range(N):      # ここの2重ループはO(M)
            for to_, weight in g.E[from_]:
                if dist[to_] > dist[from_] + weight:
                    dist[to_] = dist[from_] + weight
                    prev[to_] = from_
    # 負の重みの閉路がないかチェック
    for from_ in range(N):
        for to_, weight in g.E[from_]:
            if dist[from_]+weight < dist[to_]:
                dist[from_] = -INF
                dist[to_] = -INF
    return dist, prev


def solve(N: int, M: int, P: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    for i in range(M):
        C[i] = P - C[i]

    g = Graph(N)
    for a, b, c in zip(A, B, C):
        g.add_edge([a-1, b-1, c])

    dist, prev = BellmanFord(g, 0)
    if -dist[-1] == INF:
        print(-1)
    else:
        print(max(-dist[-1], 0))

    # print(longest)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    P = int(next(tokens))  # type: int
    A = [int()] * (M)  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    C = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    solve(N, M, P, A, B, C)


if __name__ == '__main__':
    main()

Submission Info

Submission Time
Task E - Coins Respawn
User toot_tatsuhiro
Language Python (3.4.3)
Score 0
Code Size 2324 Byte
Status WA
Exec Time 2104 ms
Memory 4832 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 25
WA × 4
TLE × 27
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49, b50, b51, b52, b53, b54, b55, b56
Case Name Status Exec Time Memory
a01 AC 18 ms 3192 KB
a02 AC 18 ms 3192 KB
a03 AC 18 ms 3192 KB
b04 AC 18 ms 3192 KB
b05 AC 18 ms 3192 KB
b06 AC 18 ms 3192 KB
b07 AC 18 ms 3192 KB
b08 AC 18 ms 3192 KB
b09 AC 1658 ms 4212 KB
b10 AC 1672 ms 4212 KB
b11 WA 1732 ms 4188 KB
b12 TLE 2100 ms 4212 KB
b13 AC 1990 ms 4208 KB
b14 AC 1581 ms 4084 KB
b15 AC 30 ms 3828 KB
b16 AC 30 ms 3828 KB
b17 AC 30 ms 3828 KB
b18 AC 1700 ms 4212 KB
b19 AC 1670 ms 4272 KB
b20 WA 1607 ms 4192 KB
b21 WA 1627 ms 4188 KB
b22 AC 1880 ms 4032 KB
b23 AC 1772 ms 4280 KB
b24 AC 1800 ms 3940 KB
b25 AC 630 ms 3444 KB
b26 TLE 2104 ms 4820 KB
b27 TLE 2104 ms 4828 KB
b28 TLE 2104 ms 4816 KB
b29 TLE 2104 ms 4812 KB
b30 TLE 2104 ms 4696 KB
b31 TLE 2104 ms 4780 KB
b32 TLE 2104 ms 4816 KB
b33 TLE 2104 ms 4760 KB
b34 TLE 2104 ms 4804 KB
b35 TLE 2104 ms 4832 KB
b36 TLE 2104 ms 4832 KB
b37 TLE 2104 ms 4828 KB
b38 TLE 2104 ms 4812 KB
b39 TLE 2104 ms 4716 KB
b40 TLE 2104 ms 4816 KB
b41 TLE 2104 ms 4816 KB
b42 TLE 2104 ms 4812 KB
b43 TLE 2104 ms 4724 KB
b44 TLE 2104 ms 4684 KB
b45 TLE 2104 ms 4692 KB
b46 TLE 2104 ms 4712 KB
b47 TLE 2104 ms 4680 KB
b48 AC 1109 ms 3828 KB
b49 AC 1631 ms 4180 KB
b50 TLE 2104 ms 4212 KB
b51 AC 1645 ms 4212 KB
b52 TLE 2104 ms 4212 KB
b53 WA 1538 ms 4120 KB
b54 AC 1792 ms 4468 KB
b55 TLE 2104 ms 4684 KB
b56 TLE 2104 ms 4704 KB