https://vjudge.net/problem/UVA-1599
#include<iostream>
#include<vector>
#include<algorithm>
#include<deque>
#pragma warning(disable:4996)
using namespace std;
const int maxn = 100006;
struct node {
int v, color;
node() = default;
node(int a, int b) { v = a, color = b; }
bool operator<(const node b) const {
return v < b.v || (v == b.v && color < b.color);
}
};
vector<node> all[maxn];
int n, m, a, b, c, d[maxn], f, lev, _m,vis[maxn];
deque<int> que[2];
void init(vector<node> &T) {//可省略
if (T.size() <= 1) return;
sort(T.begin(), T.end());
int i = 0, j = 1;
while (j != T.size()) {
if (T[i].v != T[j].v) T[++i] = T[j++];
else ++j;
}
T.erase(T.begin() + i + 1, T.end());
}
int main() {
while (cin >> n >> m) {
for (int i = 1; i <= n; ++i)
all[i].clear(), d[i] = -1, vis[i] = 0;
while (m--) {
scanf("%d%d%d", &a, &b, &c);
if (a != b)
all[a].push_back(node(b, c)), all[b].push_back(node(a, c));
}
for (int i = 1; i <= n; ++i)//可省略
init(all[i]);
que[f].clear(), que[f].push_back(n), lev = d[n] = 0;
while (!que[f].empty() && d[1] == -1 && ++lev) {
que[1 - f].clear();
for (int i = 0; i<que[f].size(); ++i)
for (int j = 0; j<all[que[f][i]].size(); ++j)
if (d[all[que[f][i]][j].v] == -1)
d[all[que[f][i]][j].v] = lev, que[1 - f].push_back(all[que[f][i]][j].v);
f ^= 1;
}
cout << d[1] << endl;
que[f].clear(), que[f].push_back(1),vis[1]=1;
while (lev--) {
que[1 - f].clear(), _m = 1000000009;
for (int i = 0; i < que[f].size(); ++i)
for (int j = 0; j < all[que[f][i]].size(); ++j)
if (!vis[all[que[f][i]][j].v] && d[all[que[f][i]][j].v] + 1 == d[que[f][i]])
if (all[que[f][i]][j].color < _m) _m = all[que[f][i]][j].color;
for (int i = 0; i < que[f].size(); ++i)
for (int j = 0; j < all[que[f][i]].size(); ++j)
if (!vis[all[que[f][i]][j].v] && d[all[que[f][i]][j].v] + 1 == d[que[f][i]] && all[que[f][i]][j].color == _m)
vis[all[que[f][i]][j].v] = 1, que[1 - f].push_back(all[que[f][i]][j].v);
f ^= 1;
if (lev) printf("%d ", _m);
else printf("%d\n", _m);
}
}
}