Pagini recente » Cod sursa (job #519259) | Cod sursa (job #837803) | Cod sursa (job #2329988) | Cod sursa (job #3225073) | Cod sursa (job #2543854)
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#define pb push_back
using namespace std;
int cost=0,n,m,t[200010],r[200010];
struct nod{
int x,y,c;
}v[200010];
vector <pair<int,int> > a;
bool mm(nod a,nod b){
return a.c<b.c;
}
void citire(){
ifstream f("apm.in");
int x,y,c,i; f>>n>>m;
for(i=1;i<=m;i++)
f>>v[i].x>>v[i].y>>v[i].c;
sort(v+1,v+m+1,mm);
for(i=1;i<=n;i++){
t[i]=i; r[i]=1;
}
f.close();
}
int tata(int nod){
while(t[nod]!=nod)
nod=t[nod];
return nod;
}
void unire(int t1,int t2){
if(r[t1]<r[t2]) t[t1]=t2;
else if(r[t2]>r[t1]) t[t2]=t1;
else{
t[t1]=t2; r[t2]++;
}
}
void rezolvare(){
int i,t1,t2;
for(i=1;i<=m;i++){
t1=tata(v[i].x);
t2=tata(v[i].y);
if(t1!=t2){
unire(t1,t2);
a.pb(make_pair(v[i].x,v[i].y));
cost+=v[i].c;
}
}
}
void afisare(){
ofstream o("apm.out");
o<<cost<<"\n"<<n-1<<"\n";
int i;
for(i=0;i<a.size();i++)
o<<a[i].first<<" "<<a[i].second<<"\n";
o.close();
}
int main(){
citire();
rezolvare();
afisare();
}