-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprims_algorithm.cpp
More file actions
43 lines (40 loc) · 830 Bytes
/
Copy pathprims_algorithm.cpp
File metadata and controls
43 lines (40 loc) · 830 Bytes
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
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <math.h>
#include <string>
#include <set>
using namespace std;
const int INF = 1e9;
int main() {
int n, m;
cin >> n >> m;
vector<vector<pair<int, int>>> gr(n);
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
gr[u - 1].push_back({ v - 1, w });
gr[v - 1].push_back({ u - 1, w });
}
vector<int> d(n, INF);
d[0] = 0;
set<pair<int, int>> s;
s.insert({ 0, 0 });
long long minost = 0;
vector<bool> added(n, 0);
for (int i = 0; i < n; ++i) {
int v = s.begin()->second;
added[v] = 1;
minost += s.begin()->first;
s.erase(s.begin());
for (auto& i : gr[v]) {
int u = i.first, w = i.second;
if (d[u] > w and !added[u]) {
s.erase({ d[u], u });
d[u] = w;
s.insert({ d[u], u });
}
}
}
}