Pagini recente » Monitorul de evaluare | Profil [email protected] | Cod sursa (job #3162766)
#include<bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
struct muchie{
int st, dr, val, ok=0;
muchie(int x, int y, int c){
st = x, dr = y, val = c;
}
};
bool cmp(muchie a, muchie b){
return a.val < b.val;
}
vector<muchie> graf;
int v[200001], cnt=0, sum=0;
int main(){
int n, m, x, y, c;
fin>>n>>m;
for(int i=1; i<=n; i++) v[i] = i;
for(int i=1; i<=m; i++){
fin>>x>>y>>c;
graf.push_back(muchie(x, y, c));
}
sort(graf.begin(), graf.end(), cmp);
for(int k=0; k<graf.size(); k++){
x = graf[k].st; y = graf[k].dr; c = graf[k].val;
if(v[x] != v[y]){
int rx = v[x], ry = v[y];
for(int i=1; i<=n; i++) if(v[i] == rx) v[i] = ry;
graf[k].ok = 1;
cnt++;
sum += c;
}
}
FILE * fout = fopen("apm.out", "w");
fprintf(fout, "%d\n%d\n", sum, cnt);
for(int k=0; k<graf.size(); k++){
if(graf[k].ok){
fprintf(fout, "%d %d\n", graf[k].st, graf[k].dr);
}
}
fclose(fout);
}