Cod sursa(job #1989450)

Utilizator mateibanuBanu Matei Costin mateibanu Data 7 iunie 2017 15:16:36
Problema Arbore partial de cost minim Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <stdio.h>
#include <algorithm>

using namespace std;

FILE*f=fopen("apm.in","r");
FILE*g=fopen("apm.out","w");

struct muchie
{
    int x,y,c;
}c[400001];

int s[400001],b[400001];

int cmp(muchie x, muchie y){
    return x.c<=y.c;
}

int main()
{
    int n,m,i,j,t=0,nr=0;
    fscanf(f,"%d%d",&n,&m);
    for (i=1;i<=m;i++) fscanf(f,"%d%d%d",&c[i].x,&c[i].y,&c[i].c);
    for (i=1;i<=n;i++) s[i]=i;
    sort(c+1,c+m+1,cmp);
    for (i=1;i<=m;i++){
        if (s[c[i].x]!=s[c[i].y]){
            for (j=1;j<=n;j++) if (j!=c[i].y&&s[j]==s[c[i].y]) s[j]=s[c[i].x];
            s[c[i].y]=s[c[i].x];
            t+=c[i].c;
            nr++;
        }
        else b[i]=1;
    }
    fprintf(g,"%d\n%d\n",t,nr);
    for (i=1;i<=m;i++) if (!b[i]) fprintf(g,"%d %d\n",c[i].x,c[i].y);
    fclose(f);
    fclose(g);
    return 0;
}