Kruskal's algorithm to sort on the side, and then make a disjoint-set maintenance, but in fact Prim has his fun place, put the distance from the point of Dijkstra's dis [v]: dis [u] + w into the side dis [ v]: w.
It is certainly better to write a little Prim. Prim feeling complexity is O ((n + m) logn), Kruskal is O (n + mlogm).
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 5005;
int n, m, s, t;
int dis[MAXN];
bool vis[MAXN];
vector<pair<int, int> > G[MAXN];
const int INF = 0x3f3f3f3f;
priority_queue<pair<int, int> >pq;
int Prim(int s) {
for(int i = 1; i <= n; ++i)
dis[i] = INF;
int cnt = 0, sum = 0;
dis[s] = 0;
pq.push({-dis[s], s});
while(!pq.empty()) {
int u = pq.top().second;
pq.pop();
if(vis[u])
continue;
vis[u] = 1;
++cnt;
sum += (dis[u]);
for(auto e : G[u]) {
int v = e.first, w = e.second;
if(!vis[v] && w < dis[v]) {
dis[v] = w;
pq.push({-dis[v], v});
}
}
}
if(cnt == n)
return sum;
return -1;
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; ++i) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
G[u].emplace_back(v, w);
G[v].emplace_back(u, w);
}
int res = Prim(1);
if(res == -1)
puts("orz");
else
printf("%d\n", res);
return 0;
}