Submission #6832377


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 shortestPath(g: Graph, s: int):
    """ グラフgにおいて、始点sから各頂点への最短路を求める
    引数
    g: グラフ, s: 始点
    返り値
    dist: 始点からの距離が格納されたリスト
    prev: 始点から最短経路で移動する場合、各頂点に至る前の頂点のリスト
    """
    dist = [INF]*g.N
    dist[s] = 0

    visited = [False]*g.N

    prev = [None]*g.N
    Q = []
    heapq.heappush(Q, (dist[s], s))

    while len(Q) > 0:
        _, u = heapq.heappop(Q)
        if visited[u] == False:
            visited[u] = True

        for v, w in g.E[u]:
            if dist[v] > dist[u] + w:
                if visited[v] == True:
                    dist[v] = -INF
                else:
                    dist[v] = dist[u] + w
                prev[v] = u
                heapq.heappush(Q, (dist[v], v))

    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 = shortestPath(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 2504 Byte
Status WA
Exec Time 41 ms
Memory 4852 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 41
WA × 15
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 27 ms 4212 KB
b10 AC 27 ms 4212 KB
b11 AC 30 ms 4084 KB
b12 AC 29 ms 4084 KB
b13 AC 29 ms 4084 KB
b14 AC 27 ms 4084 KB
b15 AC 29 ms 3828 KB
b16 AC 33 ms 3828 KB
b17 AC 32 ms 3828 KB
b18 AC 27 ms 4212 KB
b19 AC 27 ms 4212 KB
b20 AC 29 ms 4084 KB
b21 AC 29 ms 4084 KB
b22 AC 25 ms 4084 KB
b23 AC 30 ms 4212 KB
b24 AC 25 ms 4084 KB
b25 AC 19 ms 3444 KB
b26 WA 36 ms 4724 KB
b27 WA 37 ms 4724 KB
b28 WA 36 ms 4724 KB
b29 AC 36 ms 4724 KB
b30 AC 37 ms 4724 KB
b31 AC 37 ms 4724 KB
b32 WA 36 ms 4724 KB
b33 WA 38 ms 4852 KB
b34 WA 37 ms 4724 KB
b35 WA 37 ms 4724 KB
b36 WA 39 ms 4724 KB
b37 WA 39 ms 4724 KB
b38 WA 36 ms 4724 KB
b39 WA 36 ms 4724 KB
b40 WA 36 ms 4724 KB
b41 WA 37 ms 4724 KB
b42 WA 37 ms 4720 KB
b43 AC 34 ms 4724 KB
b44 AC 35 ms 4724 KB
b45 AC 35 ms 4724 KB
b46 AC 35 ms 4724 KB
b47 AC 31 ms 4724 KB
b48 AC 27 ms 4084 KB
b49 AC 27 ms 4084 KB
b50 AC 28 ms 4212 KB
b51 AC 27 ms 4212 KB
b52 AC 27 ms 4212 KB
b53 AC 29 ms 4084 KB
b54 AC 30 ms 4468 KB
b55 AC 34 ms 4724 KB
b56 WA 41 ms 4724 KB