結果

問題 No.20 砂漠のオアシス
ユーザー 37zigen37zigen
提出日時 2016-03-27 03:03:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 529 ms / 5,000 ms
コード長 2,521 bytes
コンパイル時間 2,303 ms
コンパイル使用メモリ 79,648 KB
実行使用メモリ 65,844 KB
最終ジャッジ日時 2024-04-21 06:52:15
合計ジャッジ時間 8,983 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
54,216 KB
testcase_01 AC 143 ms
54,300 KB
testcase_02 AC 141 ms
54,168 KB
testcase_03 AC 220 ms
57,340 KB
testcase_04 AC 212 ms
57,228 KB
testcase_05 AC 506 ms
65,844 KB
testcase_06 AC 503 ms
65,724 KB
testcase_07 AC 516 ms
65,628 KB
testcase_08 AC 463 ms
63,416 KB
testcase_09 AC 529 ms
65,520 KB
testcase_10 AC 132 ms
54,192 KB
testcase_11 AC 133 ms
54,200 KB
testcase_12 AC 213 ms
56,800 KB
testcase_13 AC 221 ms
56,996 KB
testcase_14 AC 240 ms
57,932 KB
testcase_15 AC 227 ms
57,352 KB
testcase_16 AC 287 ms
58,792 KB
testcase_17 AC 261 ms
58,156 KB
testcase_18 AC 280 ms
58,812 KB
testcase_19 AC 285 ms
58,828 KB
testcase_20 AC 205 ms
54,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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[][] map=new int[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				map[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<4;j++){
				int ny=y+dy[j];
				int nx=x+dx[j];
				if(0<=nx&&nx<n&&0<=ny&&ny<n){
					int vv=ny*n+nx;
					di.addEdge(vv,i,map[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, ox+oy*n);
			if(tmp<v&&di.getShortestPath(ox+oy*n, n*n-1)<2*(v-tmp)){
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}
		}else{
			System.out.println("NO");
		}

	}

	private static class Dijkstra{
		long[] d;
		List<Edge>[] edges;
		PriorityQueue<Vertex> pq;
		int n;
		int s;
		final long INF=Long.MAX_VALUE;

		Dijkstra(int n){
			this.n=n;
			edges=new List[n];
			for(int ii=0;ii<n;ii++){
				edges[ii]=new ArrayList<Edge>();
			}
			s=-1;
		}
		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;//the min total cost to go to i
				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 the old vertx
					}
					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){
				//			u=this.u;
				this.v=v;
				this.w=w;
			}
		}
		public static class Vertex implements Comparable<Vertex>{
			int me;//me
			long d;//cost(the min total cost to arrive here)

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

			@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