結果

問題 No.1565 Union
ユーザー tentententen
提出日時 2021-06-28 16:53:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,323 ms / 2,000 ms
コード長 2,123 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 79,580 KB
実行使用メモリ 107,216 KB
最終ジャッジ日時 2023-12-28 03:46:37
合計ジャッジ時間 21,740 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
54,140 KB
testcase_01 AC 54 ms
54,136 KB
testcase_02 AC 54 ms
54,108 KB
testcase_03 AC 54 ms
54,136 KB
testcase_04 AC 55 ms
54,128 KB
testcase_05 AC 54 ms
54,108 KB
testcase_06 AC 55 ms
54,136 KB
testcase_07 AC 57 ms
54,132 KB
testcase_08 AC 55 ms
54,136 KB
testcase_09 AC 56 ms
54,132 KB
testcase_10 AC 445 ms
69,100 KB
testcase_11 AC 742 ms
86,084 KB
testcase_12 AC 683 ms
74,564 KB
testcase_13 AC 345 ms
67,788 KB
testcase_14 AC 759 ms
82,160 KB
testcase_15 AC 1,323 ms
107,216 KB
testcase_16 AC 958 ms
102,628 KB
testcase_17 AC 974 ms
97,008 KB
testcase_18 AC 1,098 ms
98,284 KB
testcase_19 AC 1,012 ms
97,424 KB
testcase_20 AC 711 ms
96,172 KB
testcase_21 AC 659 ms
96,204 KB
testcase_22 AC 652 ms
96,240 KB
testcase_23 AC 731 ms
100,040 KB
testcase_24 AC 895 ms
106,008 KB
testcase_25 AC 673 ms
96,228 KB
testcase_26 AC 907 ms
105,856 KB
testcase_27 AC 687 ms
96,176 KB
testcase_28 AC 900 ms
105,852 KB
testcase_29 AC 679 ms
96,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0));
        int[] costs = new int[n];
        Arrays.fill(costs, Integer.MAX_VALUE);
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs[p.idx] <= p.value) {
                continue;
            }
            costs[p.idx] = p.value;
            for (int x : graph.get(p.idx)) {
                queue.add(new Path(x, p.value + 1));
            }
        }
        if (costs[n - 1] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(costs[n - 1]);
        }
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
}


class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0