Pagini recente » Cod sursa (job #360172) | Cod sursa (job #1956554) | Cod sursa (job #1283859) | Cod sursa (job #1518567) | Cod sursa (job #2317019)
#include <fstream>
#include <algorithm>
#define MMAX 400004
#define NMAX 200004
using namespace std;
ifstream fin("apm.in");
ofstream gut("apm.out");
int n, m, cc[NMAX], sol[NMAX], ct;
int find (int x){
if (cc[x] == x)
return x;
return cc[x] = find(cc[x]);
}
void ren(int x, int y){
cc[find(x)] = find(y);
}
struct s{
int x, y, cost;
bool operator < (const s &other) const{
return cost < other.cost;
}
} v[NMAX];
int main(){
fin >> n >> m;
for (int i = 1; i <= m ; i++){
fin >> v[i].x >> v[i].y >>v[i].cost;
}
for (int i = 1; i <= n; i++)
cc[i] = i;
sort(v+1, v+m+1);
for (int i=1; i<=m; i++){
if (find(v[i].x) != find(v[i].y)){
ren(v[i].x, v[i].y);
ct += v[i].cost;
sol[0]++;
sol[sol[0]] = i;
}
}
gut << ct << '\n' <<sol[0] << '\n';
for (int i=1; i<=sol[0]; i++)
gut << v[sol[i]].x << ' ' << v[sol[i]].y <<'\n';
fin.close();
gut.close();
return 0;
}