Pagini recente » Cod sursa (job #25045) | Cod sursa (job #1539013) | Cod sursa (job #974881) | Cod sursa (job #181218) | Cod sursa (job #1746420)
#include <bits/stdc++.h>
using namespace std;
int t[200005], n, m;
struct triplet
{
int x, y, cost, viz;
} ;
triplet a[400005];
inline bool cmp(const triplet A, const triplet B)
{
return A.cost < B.cost;
}
void Citire()
{
ifstream f("apm.in");
f >> n >> m;
for(int i = 1; i <= m; i++)
f >> a[i].x >> a[i].y >> a[i].cost;
sort(a + 1, a + m + 1, cmp);
f.close();
}
void Union(int x, int y)
{
t[y] = x;
}
int Find(int x)
{
int rad, y;
rad = x;
while(t[rad] != 0)
rad = t[rad];
while(x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
void Rezolv()
{
int i, costt, v, w;
costt = 0;
int nrc = n;
for(i = 1; i <= m && nrc > 1; i++)
{
v = a[i].x;
w = a[i].y;
v = Find(v);
w = Find(w);
if(v != w)
{
Union(v, w);
a[i].viz = 1;
costt += a[i].cost;
nrc--;
}
}
ofstream g("apm.out");
g << costt << "\n";
g << n - 1 << "\n";
for(i = 1; i <= m; i++)
if(a[i].viz == 1)
g << a[i].x << " " << a[i].y << "\n";
g.close();
}
int main()
{
Citire();
Rezolv();
return 0;
}