結果

問題 No.92 逃走経路
ユーザー YamaKasaYamaKasa
提出日時 2018-07-07 13:36:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 279 ms / 5,000 ms
コード長 1,623 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 79,112 KB
実行使用メモリ 60,696 KB
最終ジャッジ日時 2023-09-17 04:00:19
合計ジャッジ時間 7,856 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 265 ms
60,316 KB
testcase_01 AC 122 ms
55,888 KB
testcase_02 AC 140 ms
55,852 KB
testcase_03 AC 139 ms
55,964 KB
testcase_04 AC 139 ms
55,704 KB
testcase_05 AC 263 ms
60,096 KB
testcase_06 AC 243 ms
59,824 KB
testcase_07 AC 244 ms
60,068 KB
testcase_08 AC 209 ms
60,100 KB
testcase_09 AC 218 ms
59,664 KB
testcase_10 AC 270 ms
59,968 KB
testcase_11 AC 268 ms
60,336 KB
testcase_12 AC 279 ms
60,696 KB
testcase_13 AC 224 ms
59,320 KB
testcase_14 AC 234 ms
59,068 KB
testcase_15 AC 231 ms
58,968 KB
testcase_16 AC 245 ms
59,072 KB
testcase_17 AC 266 ms
60,328 KB
testcase_18 AC 260 ms
60,272 KB
testcase_19 AC 250 ms
59,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main  {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		int M = scan.nextInt();
		int K = scan.nextInt();
		int []a = new int[M];
		int []b = new int[M];
		int []c = new int[M];
		int []d = new int[K];
		for(int i = 0; i < M; i++) {
			a[i] = scan.nextInt();
			b[i] = scan.nextInt();
			c[i] = scan.nextInt();
		}
		for(int i = 0; i < K; i++) {
			d[i] = scan.nextInt();
		}
		scan.close();

		int [][]dp = new int[N + 1][K + 1];
		for(int i = 0; i <= N; i++) {
			for(int j = 0; j <= K; j++) {
				dp[i][j] = 0;
			}
		}

		// 1回目に犯人いる可能性のある街をdp[街][1回目] = 1とする。
		for(int i = 0; i < M; i++) {
			if(c[i] == d[0]) {
				dp[a[i]][1] = 1;
				dp[b[i]][1] = 1;
			}
		}

		// 2回目以降を調べる
		// c[i] = d[i] のとき、i - 1回目に犯人がいた可能性があるとき、i回目に
		// 犯人がいた可能性が存在する。
		for(int i = 1; i < K; i++) {
			for(int j = 0; j < M; j++) {
				if(c[j] == d[i]) {
					if(dp[a[j]][i] == 1) {
						dp[b[j]][i + 1] = 1;
					}
					if(dp[b[j]][i] == 1) {
						dp[a[j]][i + 1] = 1;
					}
				}
			}
		}

		// dp[][K] = 1となっている街を調べる
		Set<Integer> set = new TreeSet<Integer>();
		for(int i = 1; i <= N; i++) {
			if(dp[i][K] == 1) {
				set.add(i);
			}
		}
		System.out.println(set.size());
		int cnt = 0;
		for(int i : set) {
			if(cnt == set.size() - 1) {
				System.out.println(i);
			}else {
				System.out.print(i + " ");
			}
		}
	}
}
0