Pagini recente » Cod sursa (job #2152069) | Cod sursa (job #1344513) | Cod sursa (job #2669161) | Cod sursa (job #3174849) | Cod sursa (job #1033325)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
#define MAX 200100
typedef long long int lli;
int fr[MAX],cost,i, n, v, m, j;
struct sebi{
int x,y,c;
};
sebi a[2*MAX];
inline bool cmp(sebi d, sebi f)
{
return d.c<f.c;
}
void rec(int k)
{
if(k==n-1)
fout<<cost<<"\n"<<n-1<<"\n";
else
{
if(fr[a[i].x]!=fr[a[i].y])
{
cost+=a[i].c;
int d=a[i].x;
int e=a[i].y;
v=fr[a[i].x];
m=fr[a[i].y];
for(j=1;j<=n;j++)
{
if(fr[j]==m)
{
fr[j]=v;
}
}
i++;
rec(k+1);
fout<<e<<" "<<d<<"\n";
}
else
{
i++;
rec(k);
}
}
}
int main()
{
fin>>n;
fin>>m;
for(i=1;i<=m;i++)
{
fin>>a[i].x>>a[i].y>>a[i].c;
}
for(i=1;i<=n;i++)
fr[i]=i;
sort(a+1,a+m+1,cmp);
i=1;
rec(0);
return 0;
}