Cod sursa(job #1207015)

Utilizator PopescuMihai95Popescu Mihai PopescuMihai95 Data 11 iulie 2014 19:12:50
Problema Arbore partial de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,i,j,cost,t[200005],nr;
struct nod
{
    int x;
    int y;
    int c;
}v[400005],sol[400005];
int r(int x)
{
    if (t[x]==x) return x;
    else return t[x]=r(t[x]);
}
int cmp(const nod a,const nod b)
{
    return a.c<b.c;
}
int main()
{
    freopen("apm.in","r",stdin);
    freopen("apm.out","w",stdout);
    scanf("%d %d",&n,&m);
    for (i=1;i<=m;i++) scanf("%d %d %d",&v[i].x,&v[i].y,&v[i].c);
    sort(v+1,v+m+1,cmp);
    for (i=1;i<=n;i++) t[i]=i;
    for (i=1;i<=m;i++) if (r(v[i].x)!=r(v[i].y))
    {
        cost=cost+v[i].c;
        sol[++nr]=v[i];
        t[r(v[i].x)]=r(v[i].y);
    }
    printf("%d\n%d\n",cost,nr);
    for (i=1;i<=nr;i++) printf("%d %d\n",sol[i].x,sol[i].y);
    return 0;
}