Pagini recente » Cod sursa (job #2553164) | Cod sursa (job #1961865) | Cod sursa (job #2046794) | Cod sursa (job #2305308) | Cod sursa (job #820213)
Cod sursa(job #820213)
#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];
void reuneste (int x, int y)
{
int tt=t[y];
for (int i=1;i<=m;i++) if (t[i]==tt) t[i]=t[x];
}
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 (t[x]!=t[y]) reuneste(x,y),cost+=c,sel[i]=true;
}
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;
}