Pagini recente » Cod sursa (job #2877507) | Cod sursa (job #1489213) | Cod sursa (job #1083125) | Cod sursa (job #3200525) | Cod sursa (job #1629799)
# include <cstdio>
# include <vector>
# include <algorithm>
using namespace std;
const int N_MAX = 200001;
FILE *f = freopen("apm.in", "r" ,stdin);
FILE *g = freopen("apm.out", "w", stdout);
struct vecin{
int capat;
int cost;
vecin (int &a, int &b)
{
this -> capat = a;
this -> cost = b;
}
};
struct muchie{
int capat1;
int capat2;
int cost;
muchie (int &a, int &b, int &c)
{
this -> capat1 = a;
this -> capat2 = b;
this -> cost = c;
}
bool operator < (const muchie &other) const
{
return (this -> cost < other.cost);
}
};
int n, m;
vector <vecin> G[N_MAX];
vector <muchie> candidati;
vector <vecin> solutie;
int father[N_MAX];
int sol;
int group(int x)
{
if (x != father[x])
x = group(father[x]);
return father[x];
}
int main()
{
scanf("%d %d", &n, &m);
for (int i=1; i<=m; i++)
{
int x, y, c;
scanf("%d %d %d", &x, &y, &c);
G[x].push_back(vecin(y, c));
G[y].push_back(vecin(x, c));
candidati.push_back(muchie(x, y, c));
}
sort(candidati.begin(), candidati.end());
//for (muchie i : candidati) printf("%d %d %d\n", i.capat1, i.capat2, i.cost);
for (int i=1; i<=n; i++)
father[i] = i;
for (int i=0; i<candidati.size(); i++)
{
muchie nod = candidati[i];
int g1 = group(nod.capat1);
int g2 = group(nod.capat2);
if (g1 != g2)
{
sol += nod.cost;
father[g1] = g2;
solutie.push_back(vecin(nod.capat1, nod.capat2));
}
}
printf("%d\n", sol);
printf("%u\n", solutie.size());
for (unsigned int i = 0; i<solutie.size(); i++)
{
printf("%d %d\n", solutie[i].capat, solutie[i].cost);
}
return 0;
}