Pagini recente » Cod sursa (job #1774793) | Cod sursa (job #705893) | Cod sursa (job #2039211) | Cod sursa (job #2098539) | Cod sursa (job #3198340)
#include <fstream>
#include <algorithm>
#define NODE_DIM 1010
#define EDGE_DIM 1000010
using namespace std;
ifstream fin("oracol.in");
ofstream fout("oracol.out");
struct muchie{
int x, y, c;
};
int n, x, edgeCnt;
int father[NODE_DIM];
muchie edges[EDGE_DIM];
int getRoot(int node)
{
while(father[node]>0)
{
node = father[node];
}
return node;
}
void join(int root1, int root2)
{
if(father[root1]<=father[root2])
{
father[root1]+=father[root2];
father[root2]=root1;
}
else
{
father[root2]+=father[root1];
father[root1]=root2;
}
}
int main(){
fin>>n;
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
fin>>x;
edges[++edgeCnt]={i, j+1, x};
}
}
sort(edges+1, edges+edgeCnt+1, [](muchie a,muchie b) { return a.c<b.c;});
for (int i=1;i<=n+1;i++)
{
father[i]=-1;
}
int minCost=0;
for (int i=1;i<=edgeCnt;i++)
{
int root1=getRoot(edges[i].x);
int root2=getRoot(edges[i].y);
if(root1!=root2)
{
minCost+=edges[i].c;
join(root1, root2);
}
}
fout<<minCost;
return 0;
}