結果

問題 No.92 逃走経路
ユーザー tentententen
提出日時 2020-11-17 17:52:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 435 ms / 5,000 ms
コード長 1,895 bytes
コンパイル時間 2,933 ms
コンパイル使用メモリ 78,344 KB
実行使用メモリ 64,468 KB
最終ジャッジ日時 2023-09-30 14:20:13
合計ジャッジ時間 9,650 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 313 ms
62,768 KB
testcase_01 AC 117 ms
55,884 KB
testcase_02 AC 119 ms
55,932 KB
testcase_03 AC 121 ms
56,140 KB
testcase_04 AC 122 ms
55,724 KB
testcase_05 AC 333 ms
60,516 KB
testcase_06 AC 230 ms
59,572 KB
testcase_07 AC 233 ms
59,768 KB
testcase_08 AC 201 ms
59,028 KB
testcase_09 AC 270 ms
62,608 KB
testcase_10 AC 401 ms
61,096 KB
testcase_11 AC 421 ms
61,740 KB
testcase_12 AC 435 ms
64,468 KB
testcase_13 AC 250 ms
59,320 KB
testcase_14 AC 274 ms
59,056 KB
testcase_15 AC 305 ms
62,576 KB
testcase_16 AC 271 ms
60,332 KB
testcase_17 AC 294 ms
60,924 KB
testcase_18 AC 275 ms
60,940 KB
testcase_19 AC 256 ms
60,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        HashMap<Integer, HashMap<Integer, ArrayList<Integer>>> route = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            if (!route.containsKey(c)) {
                route.put(c, new HashMap<>());
            }
            if (!route.get(c).containsKey(a)) {
                route.get(c).put(a, new ArrayList<>());
            }
            route.get(c).get(a).add(b);
            if (!route.get(c).containsKey(b)) {
                route.get(c).put(b, new ArrayList<>());
            }
            route.get(c).get(b).add(a);
        }
        ArrayList<TreeSet<Integer>> currents = new ArrayList<>();
        for (int i = 0; i <= k; i++) {
            currents.add(new TreeSet<>());
        }
        for (int i = 1; i <= n; i++) {
            currents.get(0).add(i);
        }
        for (int i = 0; i < k; i++) {
            int x = sc.nextInt();
            HashMap<Integer, ArrayList<Integer>> tmp = route.get(x);
            if (tmp == null) {
                break;
            }
            for (int y : currents.get(i)) {
                if (tmp.containsKey(y)) {
                    currents.get(i + 1).addAll(tmp.get(y));
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        sb.append(currents.get(k).size()).append("\n");
        boolean isFirst = true;
        for (int x : currents.get(k)) {
            if (isFirst) {
                isFirst = false;
            } else {
                sb.append(" ");
            }
            sb.append(x);
        }
        System.out.println(sb);
    }
}
0