structEdge { int to, w; booloperator<(const Edge &other) const { return w > other.w; } };
vector<Edge> v[N]; bool vis[N];
intPrime(){ priority_queue<Edge> q; int cnt = 0, x, ans = 0; for (int i = 0; i < v[1].size(); ++i)q.push(v[1][i]); vis[1] = 1; while (cnt < n - 1) { Edge e = q.top();//排序每次需要logn的时间 q.pop(); x = e.to; if (!vis[x]) {//最多走m条边 vis[x] = 1; ans += e.w, cnt++; for (int i = 0; i < v[x].size(); ++i)q.push(v[x][i]); } } return ans; }
intmain(){ cin >> n >> m; for (int i = 0; i < m; ++i) { int x, y, w; cin >> x >> y >> w; v[x].push_back(Edge{y, w}); v[y].push_back(Edge{x, w}); } cout << Prime() << endl; return0; }