Pagini recente » Cod sursa (job #2511222) | Cod sursa (job #1868782) | Cod sursa (job #656338) | Cod sursa (job #751093) | Cod sursa (job #2507541)
#include<fstream>
#include<algorithm>
using namespace std;
int n,m,conex[20005], cnt, sol[20005];
long long costmin;
struct muchie{
int x,y,c;
};
muchie v[20005];
void citire()
{
ifstream f("apm.in");
f>>n>>m;
for(int i=1;i<=m;++i)
f>>v[i].x>>v[i].y>>v[i].c;
for(int i=1;i<=n;++i)
conex[i]=i;
}
bool test(muchie a, muchie b)
{
return a.c<b.c;
}
void kruskal()
{
sort(v+1,v+m+1,test);
int k=1;
while(cnt<n-1)
{
if(conex[v[k].x]!=conex[v[k].y])
{
sol[++cnt]=k;
costmin+=v[k].c;
int a = conex[v[k].x];
int b = conex[v[k].y];
for(int i=1;i<=n;++i)
if(conex[i]==a)
conex[i]=b;
}
++k;
}
}
int main()
{
citire();
kruskal();
ofstream g("apm.out");
g<<costmin<<'\n';
g << n - 1 << '\n';
for(int i=1;i<n;++i)
g<<v[sol[i]].x<<' '<<v[sol[i]].y<<'\n';
g.close();
return 0;
}