Mai intai trebuie sa te autentifici.
Cod sursa(job #566511)
Utilizator | Data | 29 martie 2011 09:31:41 | |
---|---|---|---|
Problema | Arbore partial de cost minim | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.75 kb |
#include<stdlib.h>
#include<fstream.h>
#define NM 200
#define NM 400
#define endl"\n"
ifstream in("kruskal.in");
ofstream out("kruskal.out");
struct muchie{ int x,y,c;
};
int n,m,L[NM];
muchie v[NM],h[NM];
void citire(){
int i;
in>>n>>m;
for(i=0;i<m;i++) in>>v[i].x>>v[i].y>>v[i].c;
}
int fcmp(void const *a,void const *b){
return ((muchie*)a)->c-((muchie*)b)->c;
}
int main(){
int i,ms=0,j,ct=0;
citire();
qsort(v,m,sizeof(v[0]),fcmp);
for(i=1;i<=n;i++) L[i]=i;
i=0;
while(ms<n-1){ while(L[v[i].x]==L[v[i].y]) i++;
h[ms]=v[i];
ms++;
ct=ct+v[i].c;
for(j=1;j<=n;j++)
if(L[j]==L[v[i].y]) L[j]=L[v[i].x];
}
out<<ct<<endl<<n-1<<endl;;
for(i=0;i<n-1;i++) out<<h[i].x<<" "<<h[i].y<<endl;
return 0; }