Cod sursa(job #2460174)

Utilizator serban.ionescuionescu serban mihai serban.ionescu Data 22 septembrie 2019 23:07:36
Problema Arbore partial de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.21 kb
#include <fstream>
#include <queue>
FILE*f;
FILE*g;
using namespace std;
struct nod{int x;int y;int val;}nebunie[1000];
struct compara
{
    bool operator()(int x,int y)
    {

        return nebunie[x].val>nebunie[y].val;
    }
};
priority_queue<int,vector<int>,compara>minim;

int vizitat[1000],muchia[1000],apartine[10000],s,n,m,t,k;
deque<int>arbore[1000];
int main()
{f=fopen("date.in","r");
g=fopen("date.out","w");
    fscanf(f,"%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        fscanf(f,"%d%d%d",&nebunie[i].x,&nebunie[i].y,&nebunie[i].val);
        minim.push(i);
    }
    while(t!=n)
    {
        int asd=minim.top();
        minim.pop();
        if(apartine[nebunie[asd].x]==0&&apartine[nebunie[asd].y]==0)
        {
            k++;
            arbore[k].push_front(nebunie[asd].x);
            arbore[k].push_front(nebunie[asd].y);
            apartine[nebunie[asd].x]=k;
            apartine[nebunie[asd].y]=k;
            s+=nebunie[asd].val;
            t+=2;
        }
        else
            if(apartine[nebunie[asd].x]!=0&&apartine[nebunie[asd].y]==0)
        {
            apartine[nebunie[asd].y]=apartine[nebunie[asd].x];
            arbore[apartine[nebunie[asd].y]].push_front(nebunie[asd].y);
            s+=nebunie[asd].val;
            t++;
        }
        else
            if(apartine[nebunie[asd].x]==0&&apartine[nebunie[asd].y]!=0)
        {
            apartine[nebunie[asd].x]=apartine[nebunie[asd].y];
            arbore[apartine[nebunie[asd].x]].push_front(nebunie[asd].x);
            s+=nebunie[asd].val;
            t++;
        }
        else
        {   if(apartine[nebunie[asd].x]!=apartine[nebunie[asd].y])
            while(!arbore[apartine[nebunie[asd].x]].empty())
            {
                int yi=arbore[apartine[nebunie[asd].x]].front();
                arbore[apartine[nebunie[asd].y]].push_front(yi);
                apartine[yi]=apartine[nebunie[asd].y];

                arbore[apartine[nebunie[asd].x]].pop_front();
                int rahat=arbore[apartine[nebunie[asd].x]].front();
                int cacat=rahat;
            }
             s+=nebunie[asd].val;
        }
    }
    fprintf(g,"%d",s);
    return 0;
}