結果

問題 No.20 砂漠のオアシス
ユーザー リチウムリチウム
提出日時 2014-11-16 18:21:03
言語 Java21
(openjdk 21)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,142 bytes
コンパイル時間 3,382 ms
コンパイル使用メモリ 84,340 KB
実行使用メモリ 67,528 KB
最終ジャッジ日時 2024-04-21 06:19:39
合計ジャッジ時間 9,091 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
53,788 KB
testcase_01 AC 138 ms
53,840 KB
testcase_02 AC 139 ms
54,144 KB
testcase_03 AC 215 ms
57,100 KB
testcase_04 AC 223 ms
56,648 KB
testcase_05 AC 489 ms
65,748 KB
testcase_06 AC 519 ms
67,200 KB
testcase_07 AC 509 ms
67,116 KB
testcase_08 AC 551 ms
66,972 KB
testcase_09 AC 521 ms
67,528 KB
testcase_10 WA -
testcase_11 AC 141 ms
54,032 KB
testcase_12 WA -
testcase_13 AC 221 ms
56,916 KB
testcase_14 AC 237 ms
58,044 KB
testcase_15 AC 236 ms
57,508 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 293 ms
58,764 KB
testcase_20 AC 200 ms
56,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static int[] dijstra(ArrayList<Integer> e[], ArrayList<Integer> c[],
			int start) {
		PriorityQueue<Edge> dij = new PriorityQueue<>();
		int ans[] = new int[e.length];
		boolean ok[] = new boolean[e.length];
		Arrays.fill(ans, Integer.MAX_VALUE);
		ans[start] = 0;
		ok[start] = true;
		dij.add(new Edge(start, 0));
		while (!dij.isEmpty()) {
			Edge ed = dij.poll();
			if (ans[ed.p] < ed.c)
				continue;
			int a = ed.p;
			ok[a] = true;
			for (int i = 0; i < e[a].size(); i++) {
				int x = e[a].get(i);
				int y = c[a].get(i);
				if (!ok[x] && ans[x] > ans[a] + y) {
					ans[x] = ans[a] + y;
					dij.add(new Edge(x, ans[x]));
				}
			}
		}
		return ans;
	}

	
				
	public static void main(String[] args) {
		Scanner sc=new Scanner (System.in);
		int n=sc.nextInt();
		int hp=sc.nextInt();
		int map[][]=new int[n][n];
		int ox=sc.nextInt()-1;
		int oy=sc.nextInt()-1;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				map[i][j]=sc.nextInt();
			}
		}
		ArrayList <Integer>e[]=new ArrayList[n*n];
		ArrayList <Integer>c[]=new ArrayList[n*n];
		for(int i=0;i<n*n;i++){
			e[i]=new ArrayList<>();
			c[i]=new ArrayList<>();
		}
		
		for(int i=0;i<n*n;i++){
			int x=i/n;
			int y=i-x*n;
			int dx[]={1,-1,0,0};
			int dy[]={0,0,1,-1};
			for(int j=0;j<4;j++){
				int nx=x+dx[j];
				int ny=y+dy[j];
				if(nx<0 || nx>=n || ny<0 || ny>=n)continue;
					e[i].add(nx*n+ny);
					c[i].add(map[nx][ny]);
				
			}
		}
		
		int dij[]=dijstra(e,c,0);
		int g=n*n-1;
		if(dij[g]<hp){
			System.out.println("YES");
			return;
		}
		if(ox!=-1){
			int oa=ox*n+oy;
			dij=dijstra(e,c,0);
			hp-=dij[oa];
			hp*=2;
			dij=dijstra(e,c,oa);
			if(hp>dij[g]){
				System.out.println("YES");
				return;
			}
		}
		System.out.println("NO");
	}
	
	
	}

class Edge implements Comparable<Edge> {//scacheさんから拝借
	int p;
	int c;

	public Edge(int p, int c) {
		this.p = p;
		this.c = c;
	}

	public Edge() {
	}

	@Override
	public int compareTo(Edge o) {
		return this.c - o.c;
	}

}
0