Cod sursa(job #1704297)

Utilizator PripasClaudiaPripas Claudia PripasClaudia Data 18 mai 2016 16:09:26
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include<iostream>
#include<fstream>
using namespace std;
int n,nr_muchii,k=1,t[50],h[50],m[3][50],v[50],nod,cost,n_m=0;
void citire()
{
    ifstream f("graf.txt");
    f>>n;
    while(f>>m[0][k]>>m[1][k]>>m[2][k])
        k++;
	f.close();
}
void sort()
{
    int aux,i,j,ok=0;
    int  n=k-1;
    while(ok==0)
    {
        ok=1;
        for(i=1;i<=n-1;i++)
        if(m[2][i]>m[2][i+1])
        {
            aux=m[2][i];
			m[2][i]=m[2][i+1];
			m[2][i+1]=aux;
			aux=m[0][i];
			m[0][i]=m[0][i+1];
			m[0][i+1]=aux;
			aux=m[1][i];
			m[1][i]=m[1][i+1];
			m[1][i+1]=aux;
			ok=0;
        }
    }

}
int arb(int nod)
{
    while(t[nod])
        nod=t[nod];
    return nod;
}
int main()
{
    citire();
    sort();
    k=1;
    do
        {
			while(arb(m[0][k])==arb(m[1][k]))
				k++;
			nr_muchii++;
			cout<<m[0][k]<<" "<<m[1][k]<<" "<<m[2][k]<<endl;n_m++;
			cost=cost+m[2][k];
			if(h[m[0][k]]==h[m[1][k]])
				{
				t[m[0][k]]=m[1][k];
				h[m[1][k]]++;
				}
			else
			if(h[m[0][k]]<h[m[1][k]])
				t[m[0][k]]=m[1][k];
			else
			t[m[1][k]]=m[0][k];
			k++;
        }while(nr_muchii<n-1);
		cout<<endl<<"Costul minim este: "<<cost<<endl;
		cout<<"Numarul de muchii cu cost minim este: "<<n_m<<endl;
}