1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const int N = 2e5 + 10, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
int n, m;
vi g[N];
vll w[N];
ll dis[N], ans;
bool vis[N];
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
void addEdge(int u, int v, int ww) {
g[u].push_back(v);
g[v].push_back(u);
w[u].push_back(ww);
w[v].push_back(ww);
}
void prim() {
memset(dis, 0x3f, sizeof(dis));
dis[1] = 0;
q.push({0, 1});
while (!q.empty()) {
pair<ll, int> p = q.top();
q.pop();
int u = p.second;
ll ww = p.first;
if (vis[u]) continue;
vis[u] = true;
ans += ww;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (dis[v] > w[u][i]) {
dis[v] = w[u][i];
q.push({dis[v], v});
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data/in.txt", "r", stdin);
freopen("data/out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
ll w;
cin >> u >> v >> w;
addEdge(u, v, w);
}
prim();
for (int i = 1; i <= n; i++)
if (!vis[i]) {
cout << "orz";
return 0;
}
cout << ans;
return 0;
}
|