結果
問題 | No.20 砂漠のオアシス |
ユーザー | htensai |
提出日時 | 2020-05-14 17:41:21 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 530 ms / 5,000 ms |
コード長 | 2,778 bytes |
コンパイル時間 | 2,413 ms |
コンパイル使用メモリ | 79,520 KB |
実行使用メモリ | 49,236 KB |
最終ジャッジ日時 | 2024-09-15 12:16:48 |
合計ジャッジ時間 | 9,415 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 |
ソースコード
import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int v = sc.nextInt(); int ox = sc.nextInt() - 1; int oy = sc.nextInt() - 1; int[][] field = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { field[i][j] = sc.nextInt(); } } int[][] costs = new int[n][n]; for (int i = 0; i < n; i++) { Arrays.fill(costs[i], Integer.MAX_VALUE); } PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(0, 0, -field[0][0])); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.row][p.col] <= p.value + field[p.row][p.col]) { continue; } costs[p.row][p.col] = p.value + field[p.row][p.col]; if (p.row > 0) { queue.add(new Path(p.row - 1, p.col, p.value + field[p.row][p.col])); } if (p.row < n - 1) { queue.add(new Path(p.row + 1, p.col, p.value + field[p.row][p.col])); } if (p.col > 0) { queue.add(new Path(p.row, p.col - 1, p.value + field[p.row][p.col])); } if (p.col < n - 1) { queue.add(new Path(p.row, p.col + 1, p.value + field[p.row][p.col])); } } if (costs[n - 1][n - 1] < v) { System.out.println("YES"); return; } if (ox < 0 && oy < 0) { System.out.println("NO"); return; } if (costs[oy][ox] >= v) { System.out.println("NO"); return; } v -= costs[oy][ox]; v *= 2; for (int i = 0; i < n; i++) { Arrays.fill(costs[i], Integer.MAX_VALUE); } queue.add(new Path(oy, ox, -field[oy][ox])); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.row][p.col] <= p.value + field[p.row][p.col]) { continue; } costs[p.row][p.col] = p.value + field[p.row][p.col]; if (p.row > 0) { queue.add(new Path(p.row - 1, p.col, p.value + field[p.row][p.col])); } if (p.row < n - 1) { queue.add(new Path(p.row + 1, p.col, p.value + field[p.row][p.col])); } if (p.col > 0) { queue.add(new Path(p.row, p.col - 1, p.value + field[p.row][p.col])); } if (p.col < n - 1) { queue.add(new Path(p.row, p.col + 1, p.value + field[p.row][p.col])); } } if (costs[n - 1][n - 1] < v) { System.out.println("YES"); } else { System.out.println("NO"); } } static class Path implements Comparable<Path> { int row; int col; int value; public Path(int row, int col, int value) { this.row = row; this.col = col; this.value = value; } public int compareTo(Path another) { return value - another.value; } } }