結果

問題 No.20 砂漠のオアシス
ユーザー matsu7874matsu7874
提出日時 2015-12-07 23:09:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 540 ms / 5,000 ms
コード長 3,550 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 35,584 KB
最終ジャッジ日時 2024-04-21 06:47:41
合計ジャッジ時間 4,272 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
11,136 KB
testcase_01 AC 30 ms
11,136 KB
testcase_02 AC 32 ms
11,264 KB
testcase_03 AC 53 ms
12,288 KB
testcase_04 AC 53 ms
12,288 KB
testcase_05 AC 448 ms
30,336 KB
testcase_06 AC 500 ms
35,456 KB
testcase_07 AC 511 ms
35,456 KB
testcase_08 AC 540 ms
35,584 KB
testcase_09 AC 507 ms
35,584 KB
testcase_10 AC 30 ms
11,008 KB
testcase_11 AC 30 ms
11,008 KB
testcase_12 AC 45 ms
11,904 KB
testcase_13 AC 50 ms
12,160 KB
testcase_14 AC 67 ms
13,184 KB
testcase_15 AC 59 ms
12,544 KB
testcase_16 AC 114 ms
15,744 KB
testcase_17 AC 88 ms
14,336 KB
testcase_18 AC 98 ms
14,976 KB
testcase_19 AC 114 ms
15,872 KB
testcase_20 AC 42 ms
11,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import collections


class Graph:

    def __init__(self, size):
        self.size = size
        self.graph = [[] for i in range(size)]

    def add_edge(self, source, target, cost):
        self.graph[source].append(self.Edge(target, cost))

    def add_bidirectional_edge(self, source, target, cost):
        self.add_edge(source, target, cost)
        self.add_edge(target, source, cost)

    def min_dist_dijkstra(self, s):
        dist = [float('inf')] * self.size
        dist[s] = 0
        q = [(0, s)]
        while q:
            node = self.Node(*heapq.heappop(q))
            v = node.id
            if dist[v] < node.dist:
                continue
            for e in self.graph[v]:
                if dist[e.target] > dist[v] + e.cost:
                    dist[e.target] = dist[v] + e.cost
                    heapq.heappush(q, (dist[e.target], e.target))
        return dist

    def min_path_dijkstra(self, s, t):
        dist = [float('inf')] * self.size
        prev = [-1] * self.size
        dist[s] = 0
        q = [(0, s)]
        while q:
            node = self.Node(*heapq.heappop(q))
            v = node.id
            if dist[v] < node.dist:
                continue
            for e in self.graph[v]:
                if dist[e.target] > dist[v] + e.cost:
                    dist[e.target] = dist[v] + e.cost
                    heapq.heappush(q, (dist[e.target], e.target))
                    prev[e.target] = v
            if v == t:
                break

        path = [t]
        while path[-1] > -1:
            path.append(prev[path[-1]])
        return path[1:]

    def min_dist_queue(self, s):
        dist = [float('inf')] * self.size
        dist[s] = 0
        q = collections.deque()
        q.append(s)
        while q:
            v = q.popleft()
            for e in self.graph[v]:
                if dist[e.target] > dist[v] + e.cost:
                    dist[e.target] = dist[v] + e.cost
                    if e.cost == 0:
                        q.appendleft(e.target)
                    else:
                        q.append(e.target)
        return dist

    def __str__(self):
        res = ''
        for i in range(self.size):
            res += str(i)
            for e in self.graph[i]:
                res += ' ' + str(e.target)
            res += '\n'
        return res

    class Edge:

        def __init__(self, target, cost):
            self.target = target
            self.cost = cost

    class Node:

        def __init__(self, dist, i):
            self.dist = dist
            self.id = i

        def __cmp__(self, other):
            if self.dist < other.dist:
                return -1
            elif self.dist == other.dist:
                return 0
            else:
                return 1

N, V, X, Y = map(int, input().split())
goal = N*N-1
L = [list(map(int, input().split())) for i in range(N)]

g = Graph(N * N)
d = [0, 1, 0, -1, 0]
for y in range(N):
    for x in range(N):
        for i in range(4):
            if 0 <= y + d[i] < N and 0 <= x + d[i + 1] < N:
                g.add_edge((y + d[i]) * N + x + d[i + 1], y * N + x, L[y][x])
if X==0 and Y==0:
    dist = g.min_dist_dijkstra(0)
    if dist[goal] < V:
        print('YES')
    else:
        print('NO')
    exit()
else:
    dist = g.min_dist_dijkstra(0)
    if dist[goal] < V:
        print('YES')
        exit()
    dist = g.min_dist_dijkstra((Y-1)*N+(X-1))
    if dist[0]< V and dist[goal] < 2*(V-(dist[0]+L[Y-1][X-1]-L[0][0])):
        print('YES')
    else:
        print('NO')
0