Pagini recente » Cod sursa (job #2107932) | Cod sursa (job #1562161) | Cod sursa (job #565724) | Cod sursa (job #1525993) | Cod sursa (job #2461114)
#include <fstream>
#include <queue>
FILE*f;
FILE*g;
using namespace std;
struct nod{int x;int y;int val;}nebunie[99999];
void afisare(deque<int>asdf)
{int wer=asdf.size();
for(int i=1;i<=asdf.size();i++)
{
int ert=asdf.front();
asdf.push_back(asdf.front());
asdf.pop_front();
}
fprintf(g,"/n");
}
struct compara
{
bool operator()(int x,int y)
{
return nebunie[x].val>nebunie[y].val;
}
};
priority_queue<int,vector<int>,compara>minim;
int vizitat[99999],muchia[99999],apartine[99999],s,n,m,t,k;
deque<int>arbore[99999];
int main()
{f=fopen("amp.in","r");
g=fopen("amp.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
{ int ratat=apartine[nebunie[asd].x];
if(ratat!=apartine[nebunie[asd].y])
{while(!arbore[ratat].empty())
{
int yi=arbore[ratat].front();
arbore[apartine[nebunie[asd].y]].push_front(yi);
apartine[yi]=apartine[nebunie[asd].y];
arbore[ratat].pop_front();
int rahat=arbore[apartine[nebunie[asd].x]].front();
int cacat=rahat;
}
s+=nebunie[asd].val;}
}
}
fprintf(g,"%d",s);
return 0;
}