第一个5分,后面全WA
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <cstring>
using namespace std;
const int MAXN = 1000006;
const int INF = 0x3f3f3f3f;
struct Edge {
int to, cost;
};
vector<Edge> graph[MAXN];
bool visited[MAXN];
int dist[MAXN];
int prim(int n) {
int totalCost = 0;
memset(visited, false, sizeof(visited));
fill(dist, dist + n + 1, INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push(make_pair(0, 1)); // 从1号国家开始
while (!pq.empty()) {
int u = pq.top().second;
int d = pq.top().first;
pq.pop();
if (visited[u]) continue;
visited[u] = true;
totalCost += d;
for (const Edge& e : graph[u]) {
int v = e.to;
int cost = e.cost;
if (!visited[v] && cost < dist[v]) {
dist[v] = cost;
pq.push(make_pair(cost, v));
}
}
}
return totalCost;
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
int totalCost = prim(n);
cout << totalCost << endl;
return 0;
}