Cod sursa(job #820355)

Utilizator zeeboBuzatu Vlad zeebo Data 20 noiembrie 2012 19:14:17
Problema Arbore partial de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#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;
}