Cod sursa(job #2677397)

Utilizator bogdan62003isache bogdan bogdan62003 Data 26 noiembrie 2020 14:54:35
Problema Arbore partial de cost minim Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
#define NM 200001
#define MM 400001

using namespace std;
ifstream fin ("apm.in");
ofstream fout ("apm.out");
struct muchie
{
    int x,y,c,ok;
}v[MM]
;
int n,m,i,z[NM];
int main()
{
    fin>>n>>m;
    for(i=1;i<=m;i++)
        fin>>v[i].x>>v[i].y>>v[i].c;
    int j;
    for(i=1;i<m;i++)
        for(j=i+1;j<=m;j++)
        if(v[i].c>v[j].c)
    {
        muchie aux;
        aux=v[i];
        v[i]=v[j];
        v[j]=aux;
    }
    for(i=1;i<=n;i++)
        z[i]=i;
   int   ct=0;int  nr=0; i=1;
   int x,y,u,w;
    while(nr<n-1)
    {
        x=v[i].x;
        y=v[i].y;
        u=z[x]; w=z[y];
        if(u!=w)
        {
            nr++;
            v[i].ok=1;
            ct=ct+v[i].c;
            for(j=1;j<=n;j++)
                if(z[j]==w)
                z[j]=u;
        }
        i++;
    }
    fout<<ct<<'\n'<<n-1<<'\n';
    for(i=1;i<=m;i++)
        if(v[i].ok==1)
            fout << v[i].x<<' '<<v[i].y<<'\n';
    return 0;
}