Pagini recente » Cod sursa (job #2211845) | Cod sursa (job #1210138) | Cod sursa (job #2482230) | Cod sursa (job #495868) | Cod sursa (job #1208705)
#include <stdio.h>
#include <queue>
#define inf 1000000000
using namespace std;
int n,m,i,x,y,c,t[200005],cost,h[200005],tm,v1,v2;
struct muchie
{
int x,y,c;
}e,M[400005];
class comp
{
public:
bool operator()(const muchie a,const muchie b)
{
return a.c>b.c;
}
};
priority_queue<muchie,vector<muchie>,comp>Q;
void citire()
{
scanf("%i%i",&n,&m);
for (i=1;i<=m;i++)
{
scanf("%i%i%i",&e.x,&e.y,&e.c);
Q.push(e);
}
}
int arbore(int s)
{
while (t[s]) s=t[s];
return s;
}
int main()
{
freopen ("apm.in","r",stdin);
freopen ("apm.out","w",stdout);
citire();
for (i=1;i<=n-1;i++)
{
e.x=e.y=0; v1=v2=0;
while (v1==v2)
{
e=Q.top();
Q.pop();
v1=arbore(e.x);
v2=arbore(e.y);
}
tm++;
M[tm]=e;
cost+=e.c;
if (h[v1]==h[v2])
{
t[v1]=v2;
h[v2]++;
}
else if (h[v1]<h[v2]) t[v1]=v2;
else t[v2]=v1;
}
printf("%i\n%i",cost,tm);
for (i=1;i<=tm;i++) printf("\n%i %i",M[i].x,M[i].y);
fclose(stdin);
fclose(stdout);
return 0;
}