結果

問題 No.20 砂漠のオアシス
ユーザー GrenacheGrenache
提出日時 2016-03-11 19:59:53
言語 Java21
(openjdk 21)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,856 bytes
コンパイル時間 4,180 ms
コンパイル使用メモリ 78,904 KB
実行使用メモリ 58,860 KB
最終ジャッジ日時 2024-04-21 06:50:36
合計ジャッジ時間 9,977 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
41,616 KB
testcase_01 AC 122 ms
40,824 KB
testcase_02 AC 125 ms
41,800 KB
testcase_03 AC 192 ms
44,544 KB
testcase_04 AC 197 ms
44,852 KB
testcase_05 AC 423 ms
52,000 KB
testcase_06 AC 474 ms
55,764 KB
testcase_07 AC 499 ms
55,680 KB
testcase_08 AC 491 ms
53,536 KB
testcase_09 AC 517 ms
55,688 KB
testcase_10 WA -
testcase_11 AC 119 ms
41,452 KB
testcase_12 AC 191 ms
43,600 KB
testcase_13 AC 198 ms
44,288 KB
testcase_14 AC 214 ms
46,248 KB
testcase_15 AC 211 ms
45,964 KB
testcase_16 AC 265 ms
47,728 KB
testcase_17 AC 236 ms
46,256 KB
testcase_18 AC 252 ms
48,508 KB
testcase_19 WA -
testcase_20 AC 178 ms
43,032 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 if (ox >= 0 && oy >= 0) {
			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");
			}
		} 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