Pagini recente »
Cod sursă (job #676596)
|
Monitorul de evaluare
|
Statistici Yoloswaglord (radu_bucur)
|
2018-03-22-clasa-5-tema-33
|
Cod sursă (job #804451)
Cod sursă (job
#804451)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("zapada.in");
ofstream fout("zapada.out");
struct muchie
{
short x,y;
int c;
bool operator < (const muchie& other) const
{
return this->c < other.c;
}
}v[100005];
int r[100005], t[100005];
int n, m, x, y,c,k,totalcost;
void Union(int x, int y)
{
if (r[x] > r[y])
{
t[y] = x;
}
else
{
t[x] = y;
if (r[x] == r[y])
r[y]++;
}
}
int Find(int x)
{
if (t[x] == 0)
return x;
int y = Find(t[x]);
t[x] = y;
return y;
}
void krusk2004()
{
sort(v+1,v+m+1);
for(int i=1;i<=n;i++)
{
t[i]=0;
}
for(int i = 1; i <= m; i++)
{
int x = Find(v[i].x);
int y = Find(v[i].y);
if (x != y)
{
Union(x, y);
totalcost += v[i].c;
}
}
fout<<totalcost<<"\n";
}
int main()
{
fin >> n >> m>>k;
for(int i=1;i<=m;i++)
{
fin >> v[i].x >> v[i].y >> v[i].c;
}
krusk2004();
for(int i=1;i<=k;i++)
{
m++;
fin>>v[m].x>>v[m].y>>v[m].c;
totalcost=0;
krusk2004();
}
}