// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/2/GRL_2_A
#include "src/graph/prim.hpp"
#include <iostream>
using namespace std;
int main() {
int v, e;
cin >> v >> e;
Prim<int> p(v);
for (int i = 0; i < e; ++i) {
int a, b, c;
cin >> a >> b >> c;
p.add_edge(a, b, c);
}
cout << p.mst_cost() << endl;
return 0;
}
#line 1 "test/graph/prim/aoj_grl_2_a.test.cpp"
// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/2/GRL_2_A
#line 1 "src/graph/prim.hpp"
#include <functional>
#include <queue>
#include <utility>
#include <vector>
template<class T>
class Prim {
private:
int n;
std::vector<std::vector<std::pair<int, T>>> g;
public:
Prim(int n) : n(n), g(n) {}
void add_edge(int from, int to, T cost) {
g[from].emplace_back(to, cost);
g[to].emplace_back(from, cost);
}
T mst_cost() {
T cost = 0;
std::vector<bool> used(g.size());
std::priority_queue<std::pair<T, int>,
std::vector<std::pair<T, int>>,
std::greater<>>
pq;
pq.emplace(0, 0);
while (!pq.empty()) {
auto [now_cost, now_v] = pq.top();
pq.pop();
if (used[now_v]) continue;
used[now_v] = true;
cost += now_cost;
for (auto [nv, nw] : g[now_v]) pq.emplace(nw, nv);
}
return cost;
}
};
#line 4 "test/graph/prim/aoj_grl_2_a.test.cpp"
#include <iostream>
using namespace std;
int main() {
int v, e;
cin >> v >> e;
Prim<int> p(v);
for (int i = 0; i < e; ++i) {
int a, b, c;
cin >> a >> b >> c;
p.add_edge(a, b, c);
}
cout << p.mst_cost() << endl;
return 0;
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++ | 00_sample1.in |
![]() |
12 ms | 18 MB |
g++ | 00_sample2.in |
![]() |
12 ms | 18 MB |
g++ | critical1.in |
![]() |
12 ms | 16 MB |
g++ | critical2.in |
![]() |
30 ms | 20 MB |
g++ | critical3.in |
![]() |
200 ms | 25 MB |
g++ | out1.in |
![]() |
13 ms | 18 MB |
g++ | out10.in |
![]() |
243 ms | 32 MB |
g++ | out11.in |
![]() |
78 ms | 22 MB |
g++ | out12.in |
![]() |
124 ms | 25 MB |
g++ | out13.in |
![]() |
99 ms | 24 MB |
g++ | out14.in |
![]() |
121 ms | 24 MB |
g++ | out15.in |
![]() |
92 ms | 23 MB |
g++ | out2.in |
![]() |
13 ms | 18 MB |
g++ | out3.in |
![]() |
12 ms | 18 MB |
g++ | out4.in |
![]() |
12 ms | 18 MB |
g++ | out5.in |
![]() |
13 ms | 18 MB |
g++ | out6.in |
![]() |
243 ms | 32 MB |
g++ | out7.in |
![]() |
245 ms | 32 MB |
g++ | out8.in |
![]() |
243 ms | 32 MB |
g++ | out9.in |
![]() |
244 ms | 32 MB |
clang++ | 00_sample1.in |
![]() |
12 ms | 14 MB |
clang++ | 00_sample2.in |
![]() |
12 ms | 16 MB |
clang++ | critical1.in |
![]() |
11 ms | 14 MB |
clang++ | critical2.in |
![]() |
23 ms | 15 MB |
clang++ | critical3.in |
![]() |
119 ms | 21 MB |
clang++ | out1.in |
![]() |
13 ms | 16 MB |
clang++ | out10.in |
![]() |
148 ms | 27 MB |
clang++ | out11.in |
![]() |
51 ms | 16 MB |
clang++ | out12.in |
![]() |
77 ms | 18 MB |
clang++ | out13.in |
![]() |
63 ms | 17 MB |
clang++ | out14.in |
![]() |
75 ms | 18 MB |
clang++ | out15.in |
![]() |
59 ms | 16 MB |
clang++ | out2.in |
![]() |
13 ms | 14 MB |
clang++ | out3.in |
![]() |
12 ms | 14 MB |
clang++ | out4.in |
![]() |
12 ms | 14 MB |
clang++ | out5.in |
![]() |
12 ms | 14 MB |
clang++ | out6.in |
![]() |
150 ms | 27 MB |
clang++ | out7.in |
![]() |
148 ms | 25 MB |
clang++ | out8.in |
![]() |
150 ms | 25 MB |
clang++ | out9.in |
![]() |
150 ms | 29 MB |