Cod sursa(job #812141)

Utilizator bulbulicaAlexandrescu Cristian bulbulica Data 13 noiembrie 2012 15:45:34
Problema Arbore partial de cost minim Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>
#include <algorithm>
using namespace std;
#define nmax 200000
#define mmax 400000
ifstream f("apm.in");
ofstream g("apm.out");
int n,m,t[nmax],cost_maxim,nr,r1,r2,vecx[mmax],vecy[mmax];
struct muchie
{
    int x,y;
    int cost;
}v[mmax];
bool cmp(muchie a,muchie b)
{
    return a.cost<b.cost;
}
int rad(int x)
{
    if (t[x]==0)
        return x;
    else
        return rad(t[x]);
}
void citire()
{
    f>>n>>m;
    for (int i=1;i<=m;++i)
        f>>v[i].x>>v[i].y>>v[i].cost;
}
int main()
{
    citire();
    sort(v+1,v+m+1,cmp);
    int i=1;
    while (nr!=n-1)
    {
        r1=rad(v[i].x);
        r2=rad(v[i].y);
        if (r1!=r2)
        {
            cost_maxim+=v[i].cost;
            nr++;
            t[r1]=r2;
            vecx[i]=v[i].x;
            vecy[i]=v[i].y;
        }
        i++;
    }
    g<<cost_maxim<<'\n'<<n-1<<'\n';
    for (i=1;i<=m;++i)
        if (vecy[i]!=0)
            g<<vecy[i]<<" "<<vecx[i]<<'\n';
    return 0;
}