Pagini recente » Cod sursa (job #1505307) | Cod sursa (job #1428433) | Cod sursa (job #2234717) | Cod sursa (job #58048) | Cod sursa (job #338760)
Cod sursa(job #338760)
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
struct muchie
{
int x,y,c;
};
vector<muchie> a;
muchie aux;
int root[20];
int cmpf(const muchie i,const muchie j)
{
return (i.c<j.c);
}
int find(int x)
{
if (root[x] == x)
{
return x;
}
root[x] = find(root[x]);
return root[x];
}
int main()
{
freopen("apm.in","r",stdin);
freopen("apm.out","w",stdout);
int m,n,i,cost,it;
scanf("%d%d",&n,&m);
for (i=0;i<m;i++)
{
scanf("%d%d%d",&aux.x,&aux.y,&aux.c);
a.push_back(aux);
}
sort(a.begin(),a.end(),cmpf);
for (i=1;i<=n;i++)
{
root[i] = i;
}
i = 0;
cost = 0;
it = 0;
vector<muchie> sol;
while (i<n-1)
{
if (find(a[it].x) != find(a[it].y))
{
root[find(a[it].x)] = find(a[it].y);
cost+=a[it].c;
i++;
aux.x = a[it].x;
aux.y = a[it].y;
sol.push_back(aux);
}
it++;
}
printf("%d\n",cost);
printf("%d\n",n-1);
for (i=0;i<sol.size();i++)
{
printf("%d %d\n",sol[i].x,sol[i].y);
}
return 0;
}