/* -*- coding: utf-8 -*- * * 2673.cc: No.2673 A present from B - yukicoder */ #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 500; const int MAX_M = 500; const int MAX_GN = MAX_N * (MAX_M * 2 + 1); const int INF = 1 << 30; /* typedef */ typedef pair pii; typedef vector vpii; /* global variables */ int as[MAX_M], ds[MAX_GN];; vpii nbrs[MAX_GN]; /* subroutines */ /* main */ int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) scanf("%d", as + i), as[i]--; int h = 2 * m + 1; for (int i = 0; i < h; i++) { if (! (i & 1)) for (int j = 0; j + 1 < n; j++) { int u = i * n + j, v = u + 1; nbrs[u].push_back({v, 1}); nbrs[v].push_back({u, 1}); } else { int u = i * n + as[i / 2], v = u + 1; nbrs[u].push_back({v, 0}); nbrs[v].push_back({u, 0}); } if (i > 0) for (int j = 0; j < n; j++) { int u = i * n + j, v = u - n; nbrs[u].push_back({v, 0}); } } int gn = h * n, st = (h - 1) * n + 0; fill(ds, ds + gn, INF); ds[st] = 0; priority_queue q; q.push({0, st}); while (! q.empty()) { auto [ud, u] = q.top(); q.pop(); ud = -ud; if (ds[u] != ud) continue; for (auto [v, w]: nbrs[u]) { int vd = ud + w; if (ds[v] > vd) { ds[v] = vd; q.push({-vd, v}); } } } for (int j = 1; j < n; j++) printf("%d%c", ds[j], (j + 1 < n) ? ' ' : '\n'); return 0; }