Pagini recente » Cod sursa (job #454415) | Cod sursa (job #1014902) | Cod sursa (job #1077279) | Cod sursa (job #451968) | Cod sursa (job #567691)
Cod sursa(job #567691)
#include<fstream>
#include<stdlib>
#define endl "\n"
ifstream in("APM.IN");
ofstream out("APM.OUT");
struct muchie{
int x,y,c;
};
int n,m,L[100];
muchie v[100],h[100];
void citire(){
int i;
in>>n>>m;
for(i=0;i<m;i++)in>>v[i].x>>v[i].y>>v[i].c;
}
int find(int x){
if(x==L[x]) return x;
return find(L[x]);
}
void Union(int x,int y){
int px=find(x);
int py=find(y);
L[py]=px;
}
int max(int x,int y){
if(x>y) return x;
return y;
}
int min(int x,int y){
if(x<y) return x;
return y;
}
int fcmp(void const *a,void const *b){
return ((muchie*)a)->c-((muchie*)b)->c;
}
int main(){
int i,ms=0,ct=0,j,px,py;
citire();
qsort(v,m,sizeof(v[0]),fcmp);
for(i=1;i<=n;i++) L[i]=i;
i=0;
while(ms<n-1){
do{px=find(v[i].x);
py=find(v[i].y);
if(px==py) i++;
}while(px==py);
ct+=v[i].c;
ms++;
int a=max(px,py);
int b=min(px,py);
Union(b,a);
}
for(i=1;i<=n;i++) out<<h[i].x<<" "<<h[i].y<<endl;
return 0;
}