結果

問題 No.92 逃走経路
ユーザー ぴろず
提出日時 2014-12-07 19:40:42
言語 Java
(openjdk 23)
結果
AC  
実行時間 317 ms / 5,000 ms
コード長 1,756 bytes
コンパイル時間 2,321 ms
コンパイル使用メモリ 79,632 KB
実行使用メモリ 59,068 KB
最終ジャッジ日時 2024-06-11 16:12:27
合計ジャッジ時間 7,680 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

package no092;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
		Graph g = new Graph(n);
		for(int i=0;i<m;i++) {
			g.addBidirectionalEdge(sc.nextInt()-1, sc.nextInt()-1, sc.nextInt());
		}
		int[] d = new int[k];
		for(int i=0;i<k;i++) {
			d[i] = sc.nextInt();
		}
		boolean[][] dp = new boolean[k+1][n];
		Arrays.fill(dp[0], true);
		for(int i=0;i<k;i++) {
			for(int v=0;v<n;v++) {
				if (!dp[i][v]) {
					continue;
				}
				for(Graph.Edge e:g.graph[v]) {
					if (e.cost == d[i]) {
						dp[i+1][e.to] = true;
					}
				}
			}
		}
		ArrayList<Integer> ans = new ArrayList<>();
		for(int i=0;i<n;i++) {
			if (dp[k][i] == true) {
				ans.add(i+1);
			}
		}
		System.out.println(ans.size());
		for(int i=0;i<ans.size();i++) {
			if (i > 0) {
				System.out.print(" ");
			}
			System.out.print(ans.get(i));
		}
		System.out.println();
	}

}
class Graph {
	public static final int INF = 1<<29;
	int n;
	ArrayList<Edge>[] graph;

	@SuppressWarnings("unchecked")
	public Graph(int n) {
		this.n = n;
		this.graph = new ArrayList[n];
		for(int i=0;i<n;i++) {
			graph[i] = new ArrayList<Edge>();
		}
	}

	public void addBidirectionalEdge(int from,int to,int cost) {
		addEdge(from,to,cost);
		addEdge(to,from,cost);
	}
	public void addEdge(int from,int to,int cost) {
		graph[from].add(new Edge(to, cost));
	}

	class Edge {
		int to;
		int cost;
		public Edge(int to,int cost) {
			this.to = to;
			this.cost = cost;
		}
	}

}
0