Cod sursa(job #1107714)

Utilizator ilaumariusIlau Marius Constantin ilaumarius Data 14 februarie 2014 10:18:27
Problema Arbore partial de cost minim Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include<fstream>
#include<algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct muchie
{
    int x,y;
    int c;
};
int cnd(muchie a, muchie b)
{
    if (a.c > b.c) return 0;
    else return 1;
}

int n,m,l[200000],v1[200000],v2[200000];
muchie v[400000];

int main()
{
    int i,j,nr,a,b,ct=0;
    fin>>n>>m;
    for (i=1;i<=m;i++)
        fin>>v[i].x>>v[i].y>>v[i].c;
    sort(v+1,v+m+1,cnd);
    for (i=1;i<=n;i++)
        l[i]=i;
    i=1; nr=0;
    while (nr < n-1)
    {
        if (l[v[i].x]!=l[v[i].y])
            {
                nr++;
                a=l[v[i].x];
                b=l[v[i].y];
                for (j=1;j<=n;j++)
                    if (l[j]==b)
                        l[j]=a;
                ct=ct+v[i].c;
                v1[nr]=v[i].x;
                v2[nr]=v[i].y;
            }
        i++;
    }
    fout<<ct<<'\n'<<nr<<'\n';
    for (i=1;i<=nr;i++)
        {
            fout<<v1[i]<<' '<<v2[i];
            fout<<'\n';
        }
    fin.close();
    fout.close();
    return 0;
}