Cod sursa(job #901016)

Utilizator RaduDoStochitoiu Radu RaduDo Data 28 februarie 2013 23:37:45
Problema Arbore partial de cost minim Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<bitset>
#define INF 0x3f3f3f3f
#define mp make_pair
#define pb push_back
#define maxn 200010
#define maxm 400010
using namespace std;
struct elem { int x,y,c; } G[maxm],t[maxn];
bitset < maxn > sel;
int n,m,i,j,cost,x,y;

int cmp(elem a, elem 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",&G[i].x,&G[i].y,&G[i].c);
    sort(G+1,G+m+1,cmp);
    sel[G[1].x]=sel[G[1].y]=1; cost=G[1].c; t[1].x=G[1].x; t[1].y=G[1].y;
    for(i=2; i<n; ++i)
    {
        j=1;
        while(sel[G[j].x] == sel[G[j].y])
            j++;
        sel[G[j].x]=sel[G[j].y]=1;
        cost+=G[j].c;
        t[i].x=G[j].x;  t[i].y=G[j].y;
    }
    printf("%d\n%d\n",cost,n-1);
    for(i=1; i<n; ++i)
        printf("%d %d\n",t[i].x,t[i].y);
    return 0;
}