結果

問題 No.92 逃走経路
ユーザー kitakitalilykitakitalily
提出日時 2019-05-26 15:37:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 274 ms / 5,000 ms
コード長 1,623 bytes
コンパイル時間 3,322 ms
コンパイル使用メモリ 81,080 KB
実行使用メモリ 61,888 KB
最終ジャッジ日時 2023-10-17 17:56:41
合計ジャッジ時間 9,405 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 264 ms
60,932 KB
testcase_01 AC 131 ms
57,504 KB
testcase_02 AC 146 ms
57,340 KB
testcase_03 AC 147 ms
57,712 KB
testcase_04 AC 146 ms
57,532 KB
testcase_05 AC 265 ms
61,888 KB
testcase_06 AC 255 ms
61,036 KB
testcase_07 AC 257 ms
61,112 KB
testcase_08 AC 214 ms
60,052 KB
testcase_09 AC 222 ms
60,624 KB
testcase_10 AC 273 ms
61,780 KB
testcase_11 AC 268 ms
61,424 KB
testcase_12 AC 274 ms
61,528 KB
testcase_13 AC 232 ms
60,436 KB
testcase_14 AC 231 ms
60,920 KB
testcase_15 AC 251 ms
60,688 KB
testcase_16 AC 250 ms
60,776 KB
testcase_17 AC 270 ms
61,312 KB
testcase_18 AC 261 ms
61,108 KB
testcase_19 AC 261 ms
61,772 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