Submission #6825068


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

    prev = [None]*g.N
    Q = MaxHeap([])
    Q.push((dist[s], s))
    visited = [False]*g.N
    visited[s] = True

    while len(Q) > 0:
        print(Q.heap)
        _, u = Q.pop()
        visited[u] = True
        for v, w in g.E[u]:
            if dist[v] == INF and dist[u] == INF:
                continue
            if visited[v]:
                dist[v] = INF
                prev[v] = u
                Q.push((dist[v], v))

            if dist[v] < dist[u] + w:
                dist[v] = dist[u] + w
                prev[v] = u
                Q.push((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

    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 visited[to_] is False:
                longest[to_] = dist+coin
                q.push([to_, dist+coin])
            else:
                if longest[to_] < dist+coin:
                    longest[to_] = INF

    if longest[N-1] == INF:
        print(-1)
    else:
        print(max(longest[N-1], 0))
    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 3284 Byte
Status WA
Exec Time 698 ms
Memory 5236 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 25
WA × 31
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 19 ms 3316 KB
a02 AC 18 ms 3316 KB
a03 AC 18 ms 3320 KB
b04 AC 19 ms 3320 KB
b05 WA 18 ms 3320 KB
b06 AC 19 ms 3320 KB
b07 AC 18 ms 3320 KB
b08 WA 18 ms 3320 KB
b09 AC 29 ms 4212 KB
b10 AC 29 ms 4212 KB
b11 WA 28 ms 4084 KB
b12 WA 28 ms 3956 KB
b13 WA 28 ms 4084 KB
b14 AC 28 ms 4084 KB
b15 WA 38 ms 4340 KB
b16 WA 691 ms 4084 KB
b17 WA 698 ms 4084 KB
b18 AC 28 ms 4212 KB
b19 AC 29 ms 4212 KB
b20 AC 28 ms 4084 KB
b21 WA 28 ms 4084 KB
b22 AC 25 ms 4084 KB
b23 AC 28 ms 4212 KB
b24 AC 25 ms 3956 KB
b25 AC 19 ms 3444 KB
b26 WA 38 ms 4724 KB
b27 WA 38 ms 4724 KB
b28 WA 39 ms 4852 KB
b29 WA 39 ms 4840 KB
b30 WA 38 ms 4724 KB
b31 AC 38 ms 4724 KB
b32 AC 38 ms 4724 KB
b33 WA 36 ms 4724 KB
b34 WA 37 ms 4720 KB
b35 WA 37 ms 4724 KB
b36 WA 37 ms 4724 KB
b37 WA 37 ms 4724 KB
b38 AC 38 ms 4852 KB
b39 AC 38 ms 4852 KB
b40 WA 38 ms 4852 KB
b41 WA 38 ms 4848 KB
b42 WA 37 ms 4724 KB
b43 AC 36 ms 4852 KB
b44 WA 37 ms 4724 KB
b45 WA 36 ms 4724 KB
b46 WA 36 ms 4852 KB
b47 AC 30 ms 4724 KB
b48 AC 29 ms 4084 KB
b49 AC 28 ms 4084 KB
b50 WA 29 ms 4212 KB
b51 AC 29 ms 4212 KB
b52 WA 29 ms 4212 KB
b53 WA 28 ms 4084 KB
b54 WA 41 ms 5236 KB
b55 WA 40 ms 4596 KB
b56 WA 40 ms 4596 KB