-
Notifications
You must be signed in to change notification settings - Fork 247
/
MSTPrim.cpp
79 lines (62 loc) · 1.83 KB
/
MSTPrim.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
67
68
69
70
71
72
73
74
75
76
77
78
79
/**************************************************************************************
Minimum spanning tree using Prim algorithm. O(N ^ 2)
Based on problem 185 from informatics.mccme.ru:
https://informatics.mccme.ru/mod/statements/view3.php?id=261&chapterid=185
**************************************************************************************/
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>
using namespace std;
const long long INF = (long long) 1e12;
const int MAXN = 1050;
int n, m;
long long e[MAXN][MAXN];
long long dist[MAXN];
bool used[MAXN];
long long ans;
int main() {
//assert(freopen("input.txt","r",stdin));
//assert(freopen("output.txt","w",stdout));
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
e[i][j] = INF;
for (int i = 1; i <= m; i++) {
int from, to, w;
scanf("%d %d %d", &from, &to, &w);
e[from][to] = e[to][from] = w;
}
dist[1] = 0;
for (int i = 2; i <= n; i++)
dist[i] = INF;
for (int i = 1; i <= n; i++) {
int cur = -1;
for (int j = 1; j <= n; j++)
if (!used[j] && (cur == -1 || dist[j] < dist[cur])) {
cur = j;
}
used[cur] = true;
ans += dist[cur];
for (int j = 1; j <= n; j++) {
if (used[j])
continue;
if (dist[j] > e[cur][j])
dist[j] = e[cur][j];
}
}
cout << ans << endl;
return 0;
}