結果

問題 No.92 逃走経路
ユーザー htensaihtensai
提出日時 2019-12-05 19:28:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 652 ms / 5,000 ms
コード長 1,670 bytes
コンパイル時間 3,676 ms
コンパイル使用メモリ 76,492 KB
実行使用メモリ 67,472 KB
最終ジャッジ日時 2023-08-23 12:30:05
合計ジャッジ時間 11,019 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 311 ms
60,516 KB
testcase_01 AC 128 ms
57,872 KB
testcase_02 AC 128 ms
55,928 KB
testcase_03 AC 127 ms
55,776 KB
testcase_04 AC 128 ms
56,000 KB
testcase_05 AC 353 ms
60,508 KB
testcase_06 AC 316 ms
60,532 KB
testcase_07 AC 313 ms
60,624 KB
testcase_08 AC 192 ms
57,128 KB
testcase_09 AC 255 ms
64,540 KB
testcase_10 AC 446 ms
64,260 KB
testcase_11 AC 572 ms
65,408 KB
testcase_12 AC 652 ms
67,472 KB
testcase_13 AC 233 ms
59,448 KB
testcase_14 AC 253 ms
61,788 KB
testcase_15 AC 282 ms
64,540 KB
testcase_16 AC 281 ms
62,524 KB
testcase_17 AC 304 ms
60,540 KB
testcase_18 AC 303 ms
60,324 KB
testcase_19 AC 282 ms
60,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static ArrayList<Path>[] graph;
    static int k;
    static HashSet<Integer>[] visited;
    static TreeSet<Integer> ans = new TreeSet<>();
    static int[] costs;
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		k = sc.nextInt();
		graph = new ArrayList[n + 1];
		visited = new HashSet[n + 1];
		costs = new int[k];
		for (int i = 1; i <= n; i++) {
		    graph[i] = new ArrayList<Path>();
		    visited[i] = new HashSet<Integer>();
		}
		for (int i = 0; i < m; i++) {
		    int a = sc.nextInt();
		    int b = sc.nextInt();
		    int c = sc.nextInt();
		    graph[a].add(new Path(b, c));
		    graph[b].add(new Path(a, c));
		}
		for (int i = 0; i < k; i++) {
		    costs[i] = sc.nextInt();
		}
		for (int i = 1; i <= n; i++) {
		    search(0, i);
		}
		System.out.println(ans.size());
		boolean notFirst = false;
		StringBuilder sb = new StringBuilder();
		for (Integer x : ans) {
		    if (notFirst) {
		        sb.append(" ");
		    }
		    sb.append(x);
		    notFirst = true;
		}
		System.out.println(sb);
   }
   
   static void search(int idx, int to) {
       if (visited[to].contains(idx)) {
           return;
       }
       if (idx >= k) {
           ans.add(to);
           return;
       }
       visited[to].add(idx);
       for (Path p : graph[to]) {
           if (costs[idx] == p.cost) {
               search(idx + 1, p.to);
           }
       }
   }
   
   static class Path {
       int to;
       int cost;
       public Path (int to, int cost) {
           this.to = to;
           this.cost = cost;
       }
   }
}

0