結果

問題 No.20 砂漠のオアシス
ユーザー nagitaosunagitaosu
提出日時 2020-03-13 14:42:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,717 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 32,896 KB
最終ジャッジ日時 2024-05-01 20:30:49
合計ジャッジ時間 3,795 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

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)

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