Actually, the problem is here:
for (int to=0; to<n; ++to)
if (g[v][to] < min_e[to]) {
min_e[to] = g[v][to];
sel_e[to] = v;
}
}
You should only update sel_e
and min_e
if to
hasn't been visited yet.
Otherwise, consider this case:
0 -- 1 -- 2
where w({0, 1}) = 10
, and w({1, 2} = 1)
. You would set sel_e[1] = 2
, even though you need sel_e[1] = 0
.