結果

問題 No.2565 はじめてのおつかい
ユーザー viral8viral8
提出日時 2023-12-02 15:38:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 700 ms / 2,000 ms
コード長 1,335 bytes
コンパイル時間 2,406 ms
コンパイル使用メモリ 81,356 KB
実行使用メモリ 77,732 KB
最終ジャッジ日時 2023-12-02 15:38:51
合計ジャッジ時間 22,608 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
54,504 KB
testcase_01 AC 61 ms
54,632 KB
testcase_02 AC 63 ms
54,516 KB
testcase_03 AC 700 ms
77,732 KB
testcase_04 AC 461 ms
76,524 KB
testcase_05 AC 64 ms
54,496 KB
testcase_06 AC 453 ms
64,564 KB
testcase_07 AC 333 ms
62,208 KB
testcase_08 AC 327 ms
63,640 KB
testcase_09 AC 419 ms
64,320 KB
testcase_10 AC 349 ms
63,604 KB
testcase_11 AC 541 ms
68,680 KB
testcase_12 AC 244 ms
61,972 KB
testcase_13 AC 348 ms
64,228 KB
testcase_14 AC 442 ms
66,364 KB
testcase_15 AC 494 ms
65,940 KB
testcase_16 AC 322 ms
68,356 KB
testcase_17 AC 172 ms
59,500 KB
testcase_18 AC 199 ms
61,740 KB
testcase_19 AC 479 ms
68,452 KB
testcase_20 AC 407 ms
66,280 KB
testcase_21 AC 362 ms
66,148 KB
testcase_22 AC 252 ms
62,452 KB
testcase_23 AC 324 ms
63,888 KB
testcase_24 AC 150 ms
59,704 KB
testcase_25 AC 408 ms
66,340 KB
testcase_26 AC 339 ms
63,920 KB
testcase_27 AC 308 ms
66,216 KB
testcase_28 AC 394 ms
68,508 KB
testcase_29 AC 493 ms
68,688 KB
testcase_30 AC 317 ms
66,124 KB
testcase_31 AC 429 ms
66,424 KB
testcase_32 AC 254 ms
63,048 KB
testcase_33 AC 321 ms
66,252 KB
testcase_34 AC 428 ms
66,176 KB
testcase_35 AC 336 ms
68,336 KB
testcase_36 AC 439 ms
68,376 KB
testcase_37 AC 325 ms
63,848 KB
testcase_38 AC 315 ms
65,868 KB
testcase_39 AC 440 ms
66,312 KB
testcase_40 AC 369 ms
67,904 KB
testcase_41 AC 458 ms
68,800 KB
testcase_42 AC 347 ms
63,888 KB
testcase_43 AC 296 ms
65,844 KB
testcase_44 AC 292 ms
63,520 KB
testcase_45 AC 189 ms
59,796 KB
testcase_46 AC 496 ms
66,468 KB
testcase_47 AC 352 ms
64,292 KB
testcase_48 AC 356 ms
63,808 KB
testcase_49 AC 245 ms
62,160 KB
testcase_50 AC 367 ms
64,448 KB
testcase_51 AC 64 ms
54,508 KB
testcase_52 AC 331 ms
62,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
class Main{
	private static final BufferedReader br =
		new BufferedReader(new InputStreamReader(System.in));
	private static final PrintWriter out =
		new PrintWriter(System.out);
	public static void main(String[] args)throws IOException{

		String[] str = br.readLine().split(" ");
		int N = Integer.parseInt(str[0]);
		int M = Integer.parseInt(str[1]);
		ArrayList<ArrayList<Integer>> list = new ArrayList<>();
		for(int i=0;i<=N;i++)
			list.add(new ArrayList<>());
		while(M-->0){
			str = br.readLine().split(" ");
			int u = Integer.parseInt(str[0]);
			int v = Integer.parseInt(str[1]);
			list.get(u).add(v);
		}
		PriorityQueue<int[]> queue = new PriorityQueue<>((a,b)->Integer.compare(a[1],b[1]));
		int[] dist = new int[4*N+1];
		Arrays.fill(dist,Integer.MAX_VALUE);
		dist[1] = 0;
		queue.add(new int[]{1,0,0});
		while(queue.size()>0){
			int[] now = queue.poll();
			if(dist[now[0]+now[2]*N]<now[1])
				continue;
			for(int next:list.get(now[0])){
				int nextP = next==N-1?now[2]|1:next==N?now[2]|2:now[2];
				if(dist[now[0]+now[2]*N]+1<dist[next+nextP*N]){
					dist[next+nextP*N] = dist[now[0]+now[2]*N]+1;
					queue.add(new int[]{next,dist[next+nextP*N],nextP});
				}
			}
		}
		out.println(dist[3*N+1]==Integer.MAX_VALUE?-1:dist[3*N+1]);

		br.close();
		out.close();
	}
}
0