結果

問題 No.20 砂漠のオアシス
ユーザー nagitaosunagitaosu
提出日時 2020-03-13 14:43:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 400 ms / 5,000 ms
コード長 1,706 bytes
コンパイル時間 240 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 32,768 KB
最終ジャッジ日時 2024-05-01 20:31:29
合計ジャッジ時間 3,773 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
11,008 KB
testcase_01 AC 31 ms
11,008 KB
testcase_02 AC 31 ms
11,008 KB
testcase_03 AC 46 ms
12,032 KB
testcase_04 AC 53 ms
12,032 KB
testcase_05 AC 345 ms
28,032 KB
testcase_06 AC 400 ms
32,640 KB
testcase_07 AC 397 ms
32,640 KB
testcase_08 AC 398 ms
32,768 KB
testcase_09 AC 397 ms
32,640 KB
testcase_10 AC 30 ms
11,008 KB
testcase_11 AC 30 ms
11,008 KB
testcase_12 AC 43 ms
11,648 KB
testcase_13 AC 47 ms
11,904 KB
testcase_14 AC 60 ms
12,800 KB
testcase_15 AC 52 ms
12,160 KB
testcase_16 AC 99 ms
15,104 KB
testcase_17 AC 79 ms
13,824 KB
testcase_18 AC 89 ms
14,592 KB
testcase_19 AC 103 ms
15,104 KB
testcase_20 AC 39 ms
11,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys
input = sys.stdin.readline
import heapq
INF = 10**9

class Dijkstra:
    def __init__(self, adj):
        self.adj = adj
        self.dist = [INF] * len(adj)
        self.q = []

    def reset(self):
        self.dist = [INF] * len(self.adj)
        self.q = []

    def calc(self, start):
        self.dist[start] = 0
        heapq.heappush(self.q, (0, start))
        while len(self.q) != 0:
            prov_cost, src = heapq.heappop(self.q)
            if self.dist[src] < prov_cost:
                continue
            for dest, cost in self.adj[src]:
                if self.dist[dest] > self.dist[src] + cost:
                    self.dist[dest] = self.dist[src] + cost
                    heapq.heappush(self.q, (self.dist[dest], dest))
        return self.dist

n, v, oy, ox = map(int, input().split())
ox -= 1; oy -= 1
edge = [[] for _ in range(n * n)]

field = []
for _ in range(n):
    field.append([int(item) for item in input().split()])

delta = [(0, 1), (1, 0), (0, -1), (-1, 0)]
for i in range(n):
    for j in range(n):
        for dx, dy in delta:
            if i + dx < 0 or i + dx >= n:
                continue
            if j + dy < 0 or j + dy >= n:
                continue
            frm = i * n + j 
            too = (i + dx) * n + (j + dy)
            edge[frm].append((too, field[i + dx][j + dy]))

DIJK = Dijkstra(edge)
dist_from_start = DIJK.calc(0)
ans = v - dist_from_start[n*n-1]
if ox != -1 and oy != -1:
    DIJK.reset()
    dist_from_oasis = DIJK.calc(ox*n + oy)
    ret = v - dist_from_start[ox * n + oy]
    ret *= 2
    ret -= dist_from_oasis[n*n - 1]
    ans = max(ans, ret)

if ans > 0:
    print("YES")
else:
    print("NO")
0