-
Notifications
You must be signed in to change notification settings - Fork 0
/
prim.cpp
66 lines (62 loc) · 1.53 KB
/
prim.cpp
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
/* Author: NogiNonoka
Date: 2021.1.13
Time Complexity: O(V^2) */
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
const int INF = 0x3f3f3f3f;
struct Prim {
int n, m;
int graph[MAXN][MAXN];
int dis[MAXN];
bool vis[MAXN];
void Init() {
memset(graph, 0x3f, sizeof(graph));
memset(dis, 0, sizeof(dis));
memset(vis, 0, sizeof(vis));
}
int prim() {
int now = 1;
int sum = 0;
vis[now] = true;
for (int i = 1; i <= n; i++) {
dis[i] = graph[now][i];
}
for (int i = 2; i <= n; i++) {
int min = INF;
for (int nxt = 1; nxt <= n; nxt++) {
if (!vis[nxt] && dis[nxt] < min) {
now = nxt;
min = dis[nxt];
}
}
vis[now] = true;
sum += min;
for (int j = 1; j <= n; j++) {
if (!vis[j] && graph[now][j] < dis[j]) {
dis[j] = graph[now][j];
}
}
}
return sum;
}
} pr;
int main() {
pr.Init();
int u, v, w;
scanf("%d%d", &pr.n, &pr.m);
for (int i = 0; i < pr.m; i++) {
scanf("%d%d%d", &u, &v, &w);
pr.graph[v][u] = pr.graph[u][v] = min(pr.graph[u][v], w);
// 注意梨脯。。。。草
}
int res = pr.prim();
for (int i = 1; i <= pr.n; i++) {
if (!pr.vis[i]) {
printf("orz\n");
return 0;
}
}
printf("%d\n", res);
return 0;
}