aboutsummaryrefslogtreecommitdiff
path: root/04_graph_algorithms/message_route_1667.cpp
blob: 280f7d6ed46b6f036bfa43fe314c9eb62ae2f8fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <queue>
#include <ranges>
#include <vector>

constexpr size_t inf = 1999999999;

int main() {
	size_t n, m;
	std::cin >> n >> m;
	std::vector<std::vector<size_t>> a(n);
	for (size_t i = 0; i < m; i++) {
		size_t x, y;
		std::cin >> x >> y;
		a[x-1].push_back(y-1);
		a[y-1].push_back(x-1);
	}

	std::queue<size_t> q;
	std::vector<size_t> d(n, inf);
	d[0] = 0;
	q.push(0);
	while (!q.empty()) {
		auto i = q.front();
		q.pop();
		if (i == n-1) break;
		for (auto j : a[i]) {
			if (d[j] > d[i]+1) {
				d[j] = d[i] + 1;
				q.push(j);
			}
		}
	}

	if (d[n-1] == inf) {
		std::cout << "IMPOSSIBLE\n";
	} else {
		std::cout << d[n-1]+1 << "\n";
		// Backtracking
		std::vector<size_t> v;
		v.push_back(n-1);
		while (v.back() != 0) {
			for (auto j : a[v.back()]) {
				if (d[j] == d[v.back()]-1) {
					v.push_back(j);
					break;
				}
			}
		}
		for (auto x : v | std::views::reverse)
			std::cout << x+1 << " ";
		std::cout << "\n";
	}
}

Generated with cgit - Back to sebastiano.tronto.net