This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_12_A"
#include <iostream>
#include "graph/minimumSpanningTree.hpp"
#include "macros.hpp"
using namespace std;
int main() {
int N;
long long c;
cin >> N;
Edges<long long> edges;
rep(i, N) {
rep(j, N) {
cin >> c;
if (c != -1) edges.emplace_back(i, j, c);
}
}
MinimumSpanningTree<long long> mst = kruskal(edges, N);
cout << mst.cost << endl;
}#line 1 "test/graph/kruskal.aoj_ALDS1_12_A.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_12_A"
#include <iostream>
#line 2 "graph/graphTemplate.hpp"
#include <vector>
#line 4 "graph/graphTemplate.hpp"
using namespace std;
template <typename T = int>
struct Edge {
int from, to;
T cost;
int idx;
Edge() = default;
Edge(int from, int to, T cost = 1, int idx = -1)
: from(from), to(to), cost(cost), idx(idx) {}
operator int() const { return to; }
};
template <typename T = int>
struct Graph {
vector<vector<Edge<T>>> g;
int es;
Graph() = default;
explicit Graph(int n) : g(n), es(0) {}
size_t size() const { return g.size(); }
void add_directed_edge(int from, int to, T cost = 1) {
g[from].emplace_back(from, to, cost, es++);
}
void add_edge(int from, int to, T cost = 1) {
g[from].emplace_back(from, to, cost, es);
g[to].emplace_back(to, from, cost, es++);
}
void read(int M, int padding = -1, bool weighted = false,
bool directed = false) {
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
a += padding;
b += padding;
T c = T(1);
if (weighted) {
cin >> c;
}
if (directed) {
add_directed_edge(a, b, c);
} else {
add_edge(a, b, c);
}
}
}
inline vector<Edge<T>> &operator[](const int &k) { return g[k]; }
inline const vector<Edge<T>> &operator[](const int &k) const { return g[k]; }
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 2 "structure/unionFind.hpp"
#include <algorithm>
#line 4 "structure/unionFind.hpp"
struct UnionFind {
std::vector<int> data;
UnionFind() = default;
explicit UnionFind(size_t sz) : data(sz, -1) {}
bool unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return false;
if (data[a] < data[b]) std::swap(a, b);
data[a] += data[b];
data[b] = a;
return true;
}
int find(int a) {
if (data[a] < 0) return a;
return data[a] = find(data[a]);
}
int same(int a, int b) { return find(a) == find(b); }
int size(int k) { return -data[find(k)]; }
std::vector<std::vector<int>> groups() {
int n = (int)data.size();
std::vector<std::vector<int>> ret(n);
for (int i = 0; i < n; i++) {
ret[find(i)].emplace_back(i);
}
ret.erase(std::remove_if(std::begin(ret), std::end(ret),
[&](const std::vector<int>& v) { return v.empty(); }),
std::end(ret));
return ret;
}
};
#line 4 "graph/minimumSpanningTree.hpp"
template <typename T>
struct MinimumSpanningTree {
T cost;
Edges<T> edges;
};
template <typename T>
MinimumSpanningTree<T> kruskal(Edges<T> &edges, int V) {
sort(begin(edges), end(edges),
[](const Edge<T> &a, const Edge<T> &b) { return a.cost < b.cost; });
UnionFind uf(V);
T total = T();
Edges<T> es;
for (auto &e : edges) {
if (uf.unite(e.from, e.to)) {
es.emplace_back(e);
total += e.cost;
}
}
return {total, es};
}
#line 2 "macros.hpp"
#define rep(i, n) for (int i = 0; i < (n); i++)
#line 7 "test/graph/kruskal.aoj_ALDS1_12_A.test.cpp"
using namespace std;
int main() {
int N;
long long c;
cin >> N;
Edges<long long> edges;
rep(i, N) {
rep(j, N) {
cin >> c;
if (c != -1) edges.emplace_back(i, j, c);
}
}
MinimumSpanningTree<long long> mst = kruskal(edges, N);
cout << mst.cost << endl;
}