結果

問題 No.3263 違法な散歩道
ユーザー elphe
提出日時 2025-08-16 13:20:52
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 1,939 bytes
コンパイル時間 2,866 ms
コンパイル使用メモリ 291,556 KB
実行使用メモリ 15,788 KB
最終ジャッジ日時 2025-08-16 13:20:59
合計ジャッジ時間 6,357 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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;
}

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::array<uint_fast32_t, 5>> dist(N + 1, { UINT_FAST32_MAX, UINT_FAST32_MAX, UINT_FAST32_MAX, UINT_FAST32_MAX, UINT_FAST32_MAX });
	std::queue<std::pair<uint_fast32_t, uint_fast32_t>> q;
	dist[1][0] = 0, q.emplace(1, 0);
	while (!q.empty())
	{
		const auto& [cur_pos, cur_state] = q.front();
		for (const auto& next : next_of[cur_pos])
		{
			if (is_there_creature[next])
			{
				if (cur_state < 4 && dist[next][cur_state + 1] == UINT_FAST32_MAX)
					dist[next][cur_state + 1] = dist[cur_pos][cur_state] + 1, q.emplace(next, cur_state + 1);
			}
			else if (dist[next][0] == UINT_FAST32_MAX)
				dist[next][0] = dist[cur_pos][cur_state] + 1, q.emplace(next, 0);
		}

		q.pop();
	}

	return dist[N][0];
}

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