Pagini recente » Cod sursa (job #2280161) | Cod sursa (job #1286167) | Cod sursa (job #1141227) | Cod sursa (job #2379451) | Cod sursa (job #820355)
Cod sursa(job #820355)
#include <fstream>
#include <algorithm>
#include <vector>
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
vector <pair <int, pair <int,int> > > G;
int n,x,y,c,i,m,t[400001],cost;
bool sel[400001];
int rad (int nod)
{
if (t[nod]!=nod)
t[nod]=rad(t[nod]);
return t[nod];
}
int main ()
{
f>>n>>m;
for (i=1;i<=m;i++)
{
f>>x>>y>>c;
G.pb(mp(c,mp(x,y)));
}
sort (G.begin(),G.end());
for (i=1;i<=m;i++) t[i]=i,sel[i]=false;
for (i=0;i<m;i++)
{
x=G[i].se.fi;
y=G[i].se.se;
c=G[i].fi;
if (rad(x)!=rad(y)) cost+=c,sel[i]=true,t[t[y]]=t[x];
}
g<<cost<<'\n';
g<<n-1<<'\n';
for (i=0;i<m;i++)
if (sel[i]) g<<G[i].se.fi<<' '<<G[i].se.se<<'\n';
return 0;
}