結果

問題 No.3263 違法な散歩道
ユーザー elphe
提出日時 2025-08-16 13:32:08
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 1,973 bytes
コンパイル時間 3,521 ms
コンパイル使用メモリ 289,296 KB
実行使用メモリ 12,968 KB
最終ジャッジ日時 2025-08-16 13:32:14
合計ジャッジ時間 5,624 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

static inline constexpr std::vector<std::vector<uint_fast32_t>> prepare_graph(const uint_fast32_t N, const std::vector<std::pair<uint_fast32_t, uint_fast32_t>>& edges) noexcept
{
	std::vector<std::vector<uint_fast32_t>> next_of(N + 1);
	for (const auto& [u, v] : edges)
		next_of[u].push_back(v), next_of[v].push_back(u);

	return next_of;
}

static inline constexpr std::vector<bool> prepare_creature(const uint_fast32_t N, const std::vector<uint_fast32_t>& A) noexcept
{
	std::vector<bool> is_there_creature(N + 1, false);
	for (const auto& a : A)
		is_there_creature[a] = true;

	return is_there_creature;
}

// 再進入BFS解法
static inline int_fast32_t solve(const uint_fast32_t N, const std::vector<std::vector<uint_fast32_t>>& next_of, const std::vector<bool>& is_there_creature) noexcept
{
	std::vector<std::pair<uint_fast32_t, uint_fast32_t>> dist(N + 1, { 5, UINT_FAST32_MAX });
	std::queue<uint_fast32_t> q;
	dist[1] = { 0, 0 }, q.push(1);
	while (!q.empty())
	{
		const auto& cur_pos = q.front();
		for (const auto& next : next_of[cur_pos])
		{
			if (is_there_creature[next])  // もし岩井星人がいるなら
			{
				if (dist[next].first > dist[cur_pos].first + 1)  // 前回の進入時よりも欲望を抑えられているなら
					dist[next] = { dist[cur_pos].first + 1, dist[cur_pos].second + 1 }, q.push(next);
			}
			else if (dist[next].second == UINT_FAST32_MAX)  // 未踏のマスなら
				dist[next] = { 0, dist[cur_pos].second + 1 }, q.push(next);
		}

		q.pop();
	}

	return dist[N].second;
}

int main()
{
	std::cin.tie(nullptr);
	std::ios::sync_with_stdio(false);

	uint_fast32_t N, M, K;
	std::cin >> N >> M;
	std::vector<std::pair<uint_fast32_t, uint_fast32_t>> edges(M);
	for (auto& [u, v] : edges)
		std::cin >> u >> v;

	std::cin >> K;
	std::vector<uint_fast32_t> A(K);
	for (auto& a : A)
		std::cin >> a;

	std::cout << solve(N, prepare_graph(N, edges), prepare_creature(N, A)) << '\n';
	return 0;
}
0