結果

問題 No.20 砂漠のオアシス
ユーザー GrenacheGrenache
提出日時 2016-03-11 19:57:38
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 2,792 bytes
コンパイル時間 3,754 ms
コンパイル使用メモリ 79,664 KB
実行使用メモリ 65,760 KB
最終ジャッジ日時 2024-04-21 06:50:14
合計ジャッジ時間 9,915 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
53,928 KB
testcase_01 AC 132 ms
54,052 KB
testcase_02 AC 130 ms
53,928 KB
testcase_03 RE -
testcase_04 AC 210 ms
56,924 KB
testcase_05 AC 400 ms
63,728 KB
testcase_06 AC 475 ms
65,668 KB
testcase_07 AC 487 ms
65,760 KB
testcase_08 AC 463 ms
63,804 KB
testcase_09 AC 476 ms
65,604 KB
testcase_10 WA -
testcase_11 AC 121 ms
53,904 KB
testcase_12 AC 202 ms
57,036 KB
testcase_13 AC 204 ms
57,036 KB
testcase_14 AC 229 ms
57,712 KB
testcase_15 AC 228 ms
57,472 KB
testcase_16 AC 257 ms
58,632 KB
testcase_17 AC 249 ms
57,924 KB
testcase_18 AC 262 ms
58,136 KB
testcase_19 WA -
testcase_20 AC 185 ms
54,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;


public class Main_yukicoder20 {

    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[][] matrix = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				matrix[i][j] = sc.nextInt();
			}
		}

		int[] dx = {1, -1, 0, 0};
		int[] dy = {0, 0, 1, -1};

		Dijkstra di = new Dijkstra(n * n);
		for (int i = 0; i < n * n; i++) {
			int y = i / n;
			int x = i % n;

			for (int j = 0; j < dx.length; j++) {
				int ny = y + dy[j];
				int nx = x + dx[j];

				if (ny >= 0 && ny < n && nx >= 0 && nx < n) {
					int vv = ny * n + nx;

					di.addEdge(vv, i, matrix[y][x]);
				}
			}
		}

		if (di.getShortestPath(0, n * n - 1) <= v) {
			System.out.println("YES");
		} else {
			long tmp = di.getShortestPath(0, oy * n + ox);
			if (tmp <= v && di.getShortestPath(oy * n + ox, n * n - 1) <= (v - tmp) * 2) {
				System.out.println("YES");
			} else {
				System.out.println("NO");
			}
		}

        sc.close();
    }

	private static class Dijkstra {
		long[] d;
		List<Edge>[] edges;
		PriorityQueue<Vertex> pq;
		int n;
		int s;

		final long INF = Long.MAX_VALUE;

		@SuppressWarnings("unchecked")
		Dijkstra(int n) {
			this.n = n;

			edges = new List[n];
			for (int ii = 0; ii < n; ii++) {
				edges[ii] = new ArrayList<Edge>();
			}

			s = - 1;
		}

		// i, j:0-indexed
		public void addEdge(int i, int j, int c) {
			edges[i].add(new Edge(i, j, c));
		}

		public long getShortestPath(int i, int j) {
			if (s != i) {
				d = new long[n];
				Arrays.fill(d, INF);
				d[i] = 0;
				pq = new PriorityQueue<Vertex>();
				pq.add(new Vertex(i, d[i]));

				while (!pq.isEmpty()) {
					Vertex u = pq.poll();
					if (d[u.me] < u.d) {
						continue;  // skip old vertex
					}

					for (int ii = 0; ii < edges[u.me].size(); ii++) {
						Edge v = edges[u.me].get(ii);
						if (d[u.me] != INF && d[v.v] > d[u.me] + v.w) {
							d[v.v] = d[u.me] + v.w;
							pq.add(new Vertex(v.v, d[v.v]));
						}
					}
				}

				s = i;
			}

			return d[j];
		}

		private static class Edge {
//			int u; // from
			int v; // to
			int w; // cost

			Edge(int u, int v, int w) {
//				this.u = u;
				this.v = v;
				this.w = w;
			}
		}

		private static class Vertex implements Comparable<Vertex> {
			int me; // me
			long d; // cost

			Vertex(int u, long w) {
				this.me = u;
				this.d = w;
			}

			@Override
			public int compareTo(Vertex o) {
//				return Long.compare(this.d, o.d);
				return this.d > o.d ? 1 : this.d < o.d ? -1 : 0;
			}
		}
	}
}
0