未验证 提交 91ce393c 编写于 作者: F Filip Hlasek 提交者: GitHub

fix: linter for kruskal (#1036)

* fix: linter for kruskal

* using instead of typedef.
上级 d8b4da68
#include <iostream> #include <iostream>
#include <vector>
#include <algorithm>
#include <array>
//#include <boost/multiprecision/cpp_int.hpp> //#include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision; // using namespace boost::multiprecision;
const int mx = 1e6 + 5; const int mx = 1e6 + 5;
const long int inf = 2e9; using ll = int64_t;
typedef long long ll;
#define rep(i, n) for (i = 0; i < n; i++) std::array<ll, mx> parent;
#define repp(i, a, b) for (i = a; i <= b; i++) ll node, edge;
#define pii pair<int, int> std::vector<std::pair<ll, std::pair<ll, ll>>> edges;
#define vpii vector<pii>
#define vi vector<int>
#define vll vector<ll>
#define r(x) scanf("%d", &x)
#define rs(s) scanf("%s", s)
#define gc getchar_unlocked
#define pc putchar_unlocked
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
using namespace std;
void in(int &x) {
register int c = gc();
x = 0;
int neg = 0;
for (; ((c < 48 || c > 57) && c != '-'); c = gc())
;
if (c == '-') {
neg = 1;
c = gc();
}
for (; c > 47 && c < 58; c = gc()) {
x = (x << 1) + (x << 3) + c - 48;
}
if (neg)
x = -x;
}
void out(int n) {
int N = n, rev, count = 0;
rev = N;
if (N == 0) {
pc('0');
return;
}
while ((rev % 10) == 0) {
count++;
rev /= 10;
}
rev = 0;
while (N != 0) {
rev = (rev << 3) + (rev << 1) + N % 10;
N /= 10;
}
while (rev != 0) {
pc(rev % 10 + '0');
rev /= 10;
}
while (count--) pc('0');
}
ll parent[mx], arr[mx], node, edge;
vector<pair<ll, pair<ll, ll>>> v;
void initial() { void initial() {
int i; for (int i = 0; i < node + edge; ++i) {
rep(i, node + edge) parent[i] = i; parent[i] = i;
}
} }
int root(int i) { int root(int i) {
while (parent[i] != i) { while (parent[i] != i) {
parent[i] = parent[parent[i]]; parent[i] = parent[parent[i]];
...@@ -75,41 +23,42 @@ int root(int i) { ...@@ -75,41 +23,42 @@ int root(int i) {
} }
return i; return i;
} }
void join(int x, int y) { void join(int x, int y) {
int root_x = root(x); // Disjoint set union by rank int root_x = root(x); // Disjoint set union by rank
int root_y = root(y); int root_y = root(y);
parent[root_x] = root_y; parent[root_x] = root_y;
} }
ll kruskal() { ll kruskal() {
ll mincost = 0, i, x, y; ll mincost = 0;
rep(i, edge) { for (int i = 0; i < edge; ++i) {
x = v[i].second.first; ll x = edges[i].second.first;
y = v[i].second.second; ll y = edges[i].second.second;
if (root(x) != root(y)) { if (root(x) != root(y)) {
mincost += v[i].first; mincost += edges[i].first;
join(x, y); join(x, y);
} }
} }
return mincost; return mincost;
} }
int main() { int main() {
fast; while (true) {
while (1) { int from = 0, to = 0, cost = 0, totalcost = 0;
int i, j, from, to, cost, totalcost = 0; std::cin >> node >> edge; // Enter the nodes and edges
cin >> node >> edge; // Enter the nodes and edges if (node == 0 && edge == 0) {
if (node == 0 && edge == 0)
break; // Enter 0 0 to break out break; // Enter 0 0 to break out
}
initial(); // Initialise the parent array initial(); // Initialise the parent array
rep(i, edge) { for (int i = 0; i < edge; ++i) {
cin >> from >> to >> cost; std::cin >> from >> to >> cost;
v.pb(mp(cost, mp(from, to))); edges.emplace_back(make_pair(cost, std::make_pair(from, to)));
totalcost += cost; totalcost += cost;
} }
sort(v.begin(), v.end()); sort(edges.begin(), edges.end());
// rep(i,v.size()) std::cout << kruskal() << std::endl;
// cout<<v[i].first<<" "; edges.clear();
cout << kruskal() << endl;
v.clear();
} }
return 0; return 0;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册