Cod sursa(job #1208655)

Utilizator gabib97Gabriel Boroghina gabib97 Data 16 iulie 2014 12:18:29
Problema Arbore partial de cost minim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <stdio.h>
#include <queue>
#define inf 1000000000
using namespace std;
int n,m,i,x,y,c,t[200005],cost,h[200005],tm;
struct muchie
{
    int x,y,c;
}e,M[400005];
class comp
{
public:
    bool operator()(const muchie a,const muchie b)
    {
        return a.c>b.c;
    }
};
priority_queue<muchie,vector<muchie>,comp>Q;
void citire()
{
    scanf("%i%i",&n,&m);
    for (i=1;i<=m;i++)
    {
        scanf("%i%i%i",&e.x,&e.y,&e.c);
        Q.push(e);
    }
}
int arbore(int s)
{
    while (t[s]) s=t[s];
    return s;
}
int main()
{
    freopen ("apm.in","r",stdin);
    freopen ("apm.out","w",stdout);
    citire();
    for (i=1;i<=n-1;i++)
    {
        e.x=e.y=0;
        while (arbore(e.x)==arbore(e.y))
        {
            e=Q.top();
            Q.pop();
        }
        tm++;
        M[tm]=e;
        cost+=e.c;
        if (h[e.x]==h[e.y])
        {
            t[e.x]=e.y;
            h[e.y]++;
        }
        else if (h[e.x]<h[e.y]) t[e.x]=e.y;
        else t[e.y]=e.x;
    }
    printf("%i\n%i",cost,tm);
    for (i=1;i<=tm;i++) printf("\n%i %i",M[i].x,M[i].y);
    fclose(stdin);
    fclose(stdout);
    return 0;
}