Cod sursa(job #886939)

Utilizator tudor.lozbaTudor Lozba tudor.lozba Data 23 februarie 2013 13:56:28
Problema Arbore partial de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
int L[200010], n,m,x,y,i,j,cost,k,b[200010],p,q;
struct muchie{
int x,y,c;
}M[400010];

bool sort_type(muchie a, muchie b){return a.c < b.c;}

int main()
{
    f>>n>>m;

    for(i=1; i<=m; i++)

        f>>M[i].x>>M[i].y>>M[i].c;

    sort(M+1, M+m+1, sort_type);

    for(i=1; i<=n; i++)
        L[i]=i;

    cost=0;

    for(i=1; i<=m; i++)
        if(L[M[i].x] != L[M[i].y]){

        k++;
        b[k]=i;
        cost+=M[i].c;
        p=L[M[i].x];
        q=L[M[i].y];

        for(j=1; j<=n; j++)
            if(L[j]==q)
                L[j]=p;

        if(k==n-1)
        break;}


    g<<cost<<'\n';
    g<<k<<'\n';

    for(i=1; i<=k; i++)
        g<< M[b[i]].y <<' '<< M[b[i]].x<<'\n';

    return 0;
}