Pagini recente » Cod sursa (job #1032597) | Cod sursa (job #1231283) | Cod sursa (job #2780937) | Cod sursa (job #812824) | Cod sursa (job #3224895)
#include <bits/stdc++.h>
#include <unordered_map>
#define nmax 200007
#define MOD 9901
#define INF 2012345678
#define ll long long
using namespace std;
//#define fin cin
//#define fout cout
ifstream fin("apm.in");
ofstream fout("apm.out");
struct Patru {
int x, y, cost, viz;
bool operator < (const Patru w) const {
return cost < w.cost;
}
};
int n, m;
Patru a[nmax];
int t[nmax];
int FindRoot(int x)
{
int y, rad;
rad = x;
while (t[rad] != 0)
rad = t[rad];
while (x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
void Union(int x, int y)
{
t[y] = x;
}
int Kruskal()
{
int i, x, y, nrc, costTotal;
costTotal = 0; nrc = n;
sort(a + 1, a + m + 1);
for (i = 1; i <= m && nrc > 1; i++)
{
x = FindRoot(a[i].x);
y = FindRoot(a[i].y);
if (x != y)
{
Union(x, y);
costTotal += a[i].cost;
a[i].viz = 1;
nrc--;
}
}
return costTotal;
}
int main()
{
int i, j, x, y, c, cnt;
fin >> n >> m;
for (i = 1; i <= m; i++)
{
fin >> x >> y >> c;
a[i].x = x; a[i].y = y; a[i].cost = c;
}
fout << Kruskal() << "\n";
cnt = 0;
for (i = 1; i <= m; i++)
if (a[i].viz == 1)
cnt++;
fout << cnt << "\n";
for (i = 1; i <= m; i++)
if (a[i].viz == 1)
fout << a[i].x << " " << a[i].y << "\n";
fout.close();
fin.close();
return 0;
}