結果

問題 No.92 逃走経路
ユーザー htensai
提出日時 2019-12-05 19:28:49
言語 Java
(openjdk 23)
結果
AC  
実行時間 687 ms / 5,000 ms
コード長 1,670 bytes
コンパイル時間 4,703 ms
コンパイル使用メモリ 81,508 KB
実行使用メモリ 55,328 KB
最終ジャッジ日時 2024-12-21 15:28:30
合計ジャッジ時間 10,120 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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