結果
問題 | No.92 逃走経路 |
ユーザー |
|
提出日時 | 2018-02-19 23:58:41 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 181 ms / 5,000 ms |
コード長 | 2,280 bytes |
コンパイル時間 | 3,856 ms |
コンパイル使用メモリ | 85,440 KB |
実行使用メモリ | 56,852 KB |
最終ジャッジ日時 | 2024-07-16 03:54:51 |
合計ジャッジ時間 | 7,344 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
package yukicoder;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.*;public class P92 {public static void main(String[] args) throws IOException {MyScanner sc = new MyScanner(System.in);PrintWriter pw=new PrintWriter(System.out);final int n = sc.nextInt(); final int m = sc.nextInt(); final int k = sc.nextInt();HashMap<Integer, ArrayList<Edge>> adj = new HashMap<>(n+1);for(int i=1; i<=n; i++) {adj.put(i, new ArrayList<Edge>());}for(int i=0; i<m; i++) {int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt();adj.get(a).add(new Edge(b, c));adj.get(b).add(new Edge(a, c));}int[] ds = new int[k+1];for(int i=1; i<=k; i++) {ds[i] = sc.nextInt();}boolean[][] exists = new boolean[k+1][n+1];Arrays.fill(exists[0], true);for(int t=1; t<=k; t++) {for(int i=1; i<=n; i++) {if(exists[t-1][i]) {for(Edge e : adj.get(i)) {if(ds[t] == e.w) {exists[t][e.to] = true;}}}}}int cnt = 0;ArrayList<Integer> ans = new ArrayList<>();for(int i=1; i<=n; i++) {if(exists[k][i]) {cnt++;ans.add(i);}}pw.println(cnt);for(int i=0; i<cnt; i++) {if(i==0) {pw.print(ans.get(i));} else {pw.print(" " + ans.get(i));}}pw.println();pw.close();}static class Edge {int to;int w;Edge(int _to, int _w) {to = _to; w = _w;}}static class MyScanner{BufferedReader br;StringTokenizer st;public MyScanner(InputStream s){br=new BufferedReader(new InputStreamReader(s));}public String nextLine() throws IOException{return br.readLine();}public String next() throws IOException{while(st==null || !st.hasMoreTokens())st=new StringTokenizer(br.readLine());return st.nextToken();}public int nextInt() throws IOException{return Integer.parseInt(next());}public double nextDouble() throws IOException{return Double.parseDouble(next());}public boolean ready() throws IOException{return br.ready();}public long nextLong() throws IOException{return Long.parseLong(next());}}}