結果

問題 No.20 砂漠のオアシス
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-03-01 22:24:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 601 ms / 5,000 ms
コード長 2,126 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 21,920 KB
最終ジャッジ日時 2024-10-13 05:33:09
合計ジャッジ時間 4,694 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
11,008 KB
testcase_01 AC 34 ms
11,008 KB
testcase_02 AC 30 ms
11,008 KB
testcase_03 AC 46 ms
11,520 KB
testcase_04 AC 45 ms
11,648 KB
testcase_05 AC 542 ms
21,336 KB
testcase_06 AC 521 ms
21,920 KB
testcase_07 AC 517 ms
21,816 KB
testcase_08 AC 601 ms
21,888 KB
testcase_09 AC 516 ms
21,808 KB
testcase_10 AC 31 ms
11,008 KB
testcase_11 AC 30 ms
11,136 KB
testcase_12 AC 48 ms
11,520 KB
testcase_13 AC 53 ms
11,648 KB
testcase_14 AC 73 ms
12,160 KB
testcase_15 AC 60 ms
11,776 KB
testcase_16 AC 127 ms
13,768 KB
testcase_17 AC 92 ms
12,968 KB
testcase_18 AC 114 ms
13,892 KB
testcase_19 AC 130 ms
13,928 KB
testcase_20 AC 39 ms
11,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
import collections
import heapq
import itertools


DELTA = [(1, 0), (-1, 0), (0, 1), (0, -1)]
INF = 2 ** 31 - 1


class Dijkstra(object):

    def __init__(self, h, w, stage, start, goal):
        self.height = h
        self.width = w
        self.start = start
        self.goal = goal
        self.stage = stage
        self.dist = None
        self.init_dist()

    def init_dist(self):
        self.dist = collections.defaultdict(lambda: INF)
        self.dist[self.start] = 0

    def shortest_path_to_goal(self):
        self.init_dist()
        pq = [(self.dist[(r, c)], (r, c))
              for r, c in itertools.product(range(self.height), range(self.width))]
        heapq.heapify(pq)
        while pq:
            (_, (r0, c0)) = heapq.heappop(pq)
            if (r0, c0) == self.goal:
                break
            for dr, dc in DELTA:
                (r, c) = (r0 + dr, c0 + dc)
                if r < 0 or r >= self.height or c < 0 or c >= self.width:
                    continue
                new_length = self.dist[(r0, c0)] + self.stage[r][c]
                if new_length < self.dist[(r, c)]:
                    self.dist[r, c] = new_length
                    heapq.heappush(pq, (new_length, (r, c)))
        return self.dist[self.goal]


def judge(n, v, oasis, stage):
    start = (0, 0)
    goal = (n - 1, n - 1)
    dij = Dijkstra(n, n, stage, start, goal)
    cost_start_to_goal = dij.shortest_path_to_goal()
    if cost_start_to_goal < v:
        return True
    if oasis == (-1, -1):
        return False
    dij.goal = oasis
    cost_start_to_oasis = dij.shortest_path_to_goal()
    dij.start = oasis
    dij.goal = goal
    cost_oasis_to_goal = dij.shortest_path_to_goal()
    return 2 * (v - cost_start_to_oasis) > cost_oasis_to_goal


def main():
    n, v, o_x, o_y = map(int, input().split())
    oasis = (o_y - 1, o_x - 1)
    stage = [array.array("B", map(int, input().split())) for _ in range(n)]
    if judge(n, v, oasis, stage):
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    main()
0