Problem Statement
Shortest Routes I
Implementation
void solve() {
ll n, m;
cin >> n >> m;
vector<vpll> v(n);
rep(i, m) {
ll x, y, w;
cin >> x >> y >> w;
--x, --y;
v[x].pb({ y, w }), v[y].pb({ y, w });
}
vll dis(n, 0);
function<void(ll)> dij = [&](ll node) {
set<pll> st;
rep(i, n) (i == node) ? dis[i] = 0 : dis[i] = inf;
rep(i, n) st.insert({ dis[i], i });
vll vis(n, 0);
while (sz(st)) {
auto cur = *st.begin();
vis[cur.s] = 1;
st.erase(st.begin());
for (auto& child : v[cur.s]) {
if (!vis[child.f]) {
if (dis[child.f] > dis[cur.s] + child.s) {
st.erase(st.find({ dis[child.f], child.f }));
dis[child.f] = dis[cur.s] + child.s;
st.insert({ dis[child.f], child.f });
}
}
}
}
};
dij(0);
rep(i, n) cout << dis[i] << " ";
}