Submission #6826245


Source Code Expand

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


class MaxHeap(object):
    def __init__(self, x):
        self.heap = [-e for e in x]
        heapq.heapify(self.heap)

    def push(self, value):
        a, b = value
        heapq.heappush(self.heap, (-a, b))

    def pop(self):
        a, b = heapq.heappop(self.heap)
        return -a, b

    def __len__(self):
        return len(self.heap)


# 有効グラフを仮定する。
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 solve(N: int, M: int, P: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    for i in range(M):
        C[i] -= P

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

    longest = [0]*N
    visited = [False]*N
    q = MaxHeap([])
    q.push([0, 0])
    while len(q) > 0:
        node, dist = q.pop()
        visited[node] = True
        for to_, coin in g.E[node]:
            if longest[node] == INF and longest[to_] != INF:
                visited[to_] = False
            if visited[to_] is False:
                longest[to_] = dist+coin
                q.push([to_, dist+coin])
            else:
                if longest[to_] < dist+coin:
                    longest[to_] = INF
                    q.push([to_, INF])

    if longest[N-1] == INF:
        print(-1)
    else:
        print(max(longest[N-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 2478 Byte
Status WA
Exec Time 2104 ms
Memory 5232 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 36
WA × 19
TLE × 1
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 19 ms 3192 KB
b05 AC 18 ms 3192 KB
b06 AC 18 ms 3320 KB
b07 AC 18 ms 3192 KB
b08 AC 19 ms 3192 KB
b09 AC 29 ms 4208 KB
b10 AC 29 ms 4208 KB
b11 AC 33 ms 4080 KB
b12 AC 32 ms 4080 KB
b13 AC 33 ms 4080 KB
b14 AC 29 ms 4080 KB
b15 WA 39 ms 4336 KB
b16 TLE 2104 ms 4080 KB
b17 WA 1256 ms 4080 KB
b18 AC 29 ms 4208 KB
b19 AC 29 ms 4208 KB
b20 AC 33 ms 4080 KB
b21 AC 33 ms 4080 KB
b22 AC 25 ms 4080 KB
b23 AC 33 ms 4208 KB
b24 AC 25 ms 3952 KB
b25 AC 19 ms 3440 KB
b26 WA 52 ms 4848 KB
b27 WA 53 ms 4848 KB
b28 WA 55 ms 4848 KB
b29 AC 52 ms 4848 KB
b30 AC 53 ms 4848 KB
b31 AC 53 ms 4848 KB
b32 WA 48 ms 4848 KB
b33 WA 45 ms 4720 KB
b34 WA 46 ms 4848 KB
b35 WA 46 ms 4720 KB
b36 WA 48 ms 4848 KB
b37 WA 49 ms 4848 KB
b38 WA 51 ms 4848 KB
b39 WA 51 ms 4848 KB
b40 WA 50 ms 4844 KB
b41 WA 51 ms 4848 KB
b42 WA 45 ms 4848 KB
b43 AC 47 ms 4848 KB
b44 AC 49 ms 4848 KB
b45 AC 47 ms 4848 KB
b46 AC 47 ms 4848 KB
b47 AC 31 ms 4720 KB
b48 AC 30 ms 4080 KB
b49 AC 29 ms 4080 KB
b50 AC 34 ms 4208 KB
b51 AC 29 ms 4208 KB
b52 AC 35 ms 4208 KB
b53 AC 33 ms 4080 KB
b54 WA 41 ms 5232 KB
b55 WA 54 ms 4592 KB
b56 WA 57 ms 4592 KB