Pagini recente » Cod sursa (job #2619111) | Cod sursa (job #2464109) | Cod sursa (job #965710) | Cod sursa (job #555694) | Cod sursa (job #2161219)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
const int MAXN = 200002;
int tata[MAXN];
int cost=0,n,m;
struct Muchie {
int x, y, cost;
}G[MAXN*2];
vector <int> sol;
int root(int nod) {
if (tata[nod] == 0)
return nod;
return tata[nod] = root(tata[nod]);
}
bool cmp(Muchie a, Muchie b) {
return a.cost < b.cost;
}
int main() {
fin>>n>>m;
for(int i=0; i<m; ++i) {
fin>>G[i].x>>G[i].y>>G[i].cost;
}
sort(G, G+m, cmp);
for (int i=0; i<m; ++i) {
if (root(G[i].x) != root(G[i].y)) {
tata[root(G[i].x)] = root(G[i].y);
sol.push_back(i);
cost += G[i].cost;
}
}
fout<<cost<<endl<<sol.size()<<endl;
for (unsigned i=0; i<sol.size(); ++i)
fout<<G[i].x << ' '<<G[i].y<<endl;
}