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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| #include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <utility> #include <vector>
const int MAXN = 100000, INF = 200000;
int n, m; std::vector< std::pair<int, int> > e[MAXN + 1]; int vis[MAXN + 1], ans; int col[INF + 1];
void bfsK() { static std::queue< std::pair<int, int> > qK; memset(vis, -1, sizeof(vis)); qK.push( std::make_pair(n, 0) ); vis[n] = 0, ans = INF;
while (!qK.empty()) { auto u = qK.front(); qK.pop(); if (u.first == 1 && u.second < ans) ans = u.second; for (auto ed : e[u.first]) { if (vis[ed.first] == -1) { vis[ed.first] = u.second + 1; qK.push(std::make_pair(ed.first, u.second + 1)); } } } }
void bfsCol() { static std::vector<int> qCol; static bool mk[MAXN + 1]; memset(col, 0x3f, sizeof(col)); memset(mk, false, sizeof(mk)); qCol.clear(); qCol.push_back(1);
int cnt = ans - 1; bool flag = true; while (cnt >= 0) { static std::vector<int> uSet; if (flag) { uSet.clear(); for (int each : qCol) uSet.push_back(each); qCol.clear(); } flag = true; for (int u : uSet) { for (auto ed : e[u]) { if (vis[ed.first] == cnt) { if (!mk[ed.first]) { if (col[cnt] >= ed.second) { if (col[cnt] > ed.second) qCol.clear(); col[cnt] = ed.second; mk[ed.first] = true; flag = false; qCol.push_back(ed.first); } } else if (col[cnt] > ed.second) col[cnt] = ed.second; } } } if (flag) { cnt--; for (int u : uSet) { for (auto ed : e[u]) { if (vis[ed.first] == cnt) { if (!mk[ed.first]) { if (col[cnt] >= ed.second) { if (col[cnt] > ed.second) qCol.clear(); col[cnt] = ed.second; mk[ed.first] = true; flag = false; qCol.push_back(ed.first); } } else if (col[cnt] > ed.second) col[cnt] = ed.second; } } } } } }
int main() { while (std::cin >> n >> m) { for (int i = 1; i <= n; i++) e[i].clear();
for (int i = 0, x, y, z; i < m; i++) { scanf("%d%d%d", &x, &y, &z); if (x == y) continue; e[x].push_back( std::make_pair(y, z) ); e[y].push_back( std::make_pair(x, z) ); }
for (int i = 1; i <= n; i++) std::sort(e[i].begin(), e[i].end());
bfsK(); bfsCol();
std::cout << ans << std::endl; for (int i = ans - 1; i >= 0; i--) printf("%d%c", col[i], i == 0 ? '\n' : ' '); }
return 0; }
|