TheAlgorithms-C-Plus-Plus/Graph/Kruskal.cpp

136 lines
2.3 KiB
C++
Raw Normal View History

2017-07-29 13:25:00 +08:00
#include "bits/stdc++.h"
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
2019-08-21 10:10:08 +08:00
const int mx = 1e6 + 5;
2017-07-29 13:25:00 +08:00
const long int inf = 2e9;
typedef long long ll;
2019-08-21 10:10:08 +08:00
#define rep(i, n) for (i = 0; i < n; i++)
#define repp(i, a, b) for (i = a; i <= b; i++)
#define pii pair<int, int>
#define vpii vector<pii>
2017-07-29 13:25:00 +08:00
#define vi vector<int>
2019-08-21 10:10:08 +08:00
#define vll vector<ll>
#define r(x) scanf("%d", &x)
#define rs(s) scanf("%s", s)
2017-07-29 13:25:00 +08:00
#define gc getchar_unlocked
#define pc putchar_unlocked
#define mp make_pair
2019-08-21 10:10:08 +08:00
#define pb push_back
2017-07-29 13:25:00 +08:00
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
2019-08-21 10:10:08 +08:00
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
2017-07-29 13:25:00 +08:00
using namespace std;
void in(int &x)
{
2019-08-21 10:10:08 +08:00
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;
2017-07-29 13:25:00 +08:00
}
2019-08-21 10:10:08 +08:00
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');
2017-07-29 13:25:00 +08:00
}
2019-08-21 10:10:08 +08:00
ll parent[mx], arr[mx], node, edge;
vector<pair<ll, pair<ll, ll>>> v;
2017-07-29 13:25:00 +08:00
void initial()
{
int i;
2019-08-21 10:10:08 +08:00
rep(i, node + edge)
2017-07-29 13:25:00 +08:00
parent[i] = i;
}
int root(int i)
{
2019-08-21 10:10:08 +08:00
while (parent[i] != i)
2017-07-29 13:25:00 +08:00
{
parent[i] = parent[parent[i]];
i = parent[i];
}
return i;
}
2019-08-21 10:10:08 +08:00
void join(int x, int y)
2017-07-29 13:25:00 +08:00
{
2019-08-21 10:10:08 +08:00
int root_x = root(x); //Disjoint set union by rank
2017-07-29 13:25:00 +08:00
int root_y = root(y);
parent[root_x] = root_y;
}
ll kruskal()
{
2019-08-21 10:10:08 +08:00
ll mincost = 0, i, x, y;
rep(i, edge)
2017-07-29 13:25:00 +08:00
{
x = v[i].second.first;
y = v[i].second.second;
2019-08-21 10:10:08 +08:00
if (root(x) != root(y))
2017-07-29 13:25:00 +08:00
{
mincost += v[i].first;
2019-08-21 10:10:08 +08:00
join(x, y);
2017-07-29 13:25:00 +08:00
}
}
return mincost;
}
2019-08-21 10:10:08 +08:00
int main()
{
2017-07-29 13:25:00 +08:00
fast;
2019-08-21 10:10:08 +08:00
while (1)
2017-07-29 13:25:00 +08:00
{
2019-08-21 10:10:08 +08:00
int i, j, from, to, cost, totalcost = 0;
cin >> node >> edge; //Enter the nodes and edges
if (node == 0 && edge == 0)
break; //Enter 0 0 to break out
initial(); //Initialise the parent array
rep(i, edge)
2017-07-29 13:25:00 +08:00
{
2019-08-21 10:10:08 +08:00
cin >> from >> to >> cost;
v.pb(mp(cost, mp(from, to)));
2017-07-29 13:25:00 +08:00
totalcost += cost;
}
2019-08-21 10:10:08 +08:00
sort(v.begin(), v.end());
2017-07-29 13:25:00 +08:00
// rep(i,v.size())
// cout<<v[i].first<<" ";
2019-08-21 10:10:08 +08:00
cout << kruskal() << endl;
2017-07-29 13:25:00 +08:00
v.clear();
}
return 0;
}