Pagini recente » Cod sursa (job #509397) | Cod sursa (job #2519975) | Profil ionanghelina | Cod sursa (job #1897434) | Cod sursa (job #1430440)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f1("apm.in");
ofstream f2("apm.out");
#define MX 200100
struct edge
{
int x,y, cost;
};
int n, m, tata[MX], k_apm, cost_min;
edge E[2*MX], E_apm[MX];
bool operator<(edge e1, edge e2)
{
return e1.cost < e2.cost;
}
void join(int x, int y)
{
while ( tata[x] )
x= tata[x];
while ( tata[y] )
y= tata[y];
tata[y]=x;
}
bool same_set(int x, int y)
{
while ( tata[x] )
x= tata[x];
while ( tata[y] )
y= tata[y];
return x == y;
}
int main()
{
int i;
f1>>n>>m;
for (i=1; i<=m; i++)
f1>>E[i].x>>E[i].y>>E[i].cost;
sort(E+1, E+m+1);
for (i=1; i<=m && k_apm< n-1; i++)
if ( !same_set(E[i].x, E[i].y) )
{
join(E[ i ].x, E[ i ].y);
E_apm[++k_apm]= E[i];
cost_min+= E[ i ].cost;
}
f2<<cost_min<<"\n"<<k_apm<<"\n";
for (i=1; i<= k_apm; i++)
f2<<E_apm[ i ].x<<" "<<E_apm[ i ].y<<"\n";
f2.close();
return 0;
}