Pagini recente »
Diferențe pentru utilizator/horsepower între reviziile 30 și 31
|
Istoria paginii runda/prega_oji2016_vi_runda5
|
Istoria paginii runda/pregatire_olimpiada_cls5a/clasament
|
OJI 2023 Clasa a VI-a - Simulare FFA
|
Cod sursă (job #624890)
Cod sursă (job
#624890)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("zapada.in");
ofstream fout("zapada.out");
int nrc, n, m, k, t[10005], cMin;
struct muchie
{
int x, y, c;
bool operator < (const muchie A) const
{
return c < A.c;
}
};
muchie h[100005];
void Union(int x, int y)
{
t[x] = y;
}
int Find(int x)
{
int rad, y;
rad = x;
while(t[rad] != 0)
rad = t[rad];
while(x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
void Kruskal(int w)
{
nrc = n;
cMin = 0;
sort(h + 1, h + w + 1);
for(int i = 1; i <= n; i++)
t[i] = 0;
for(int i = 1; i <= w && nrc > 1; i++)
{
int x = Find(h[i].x);
int y = Find(h[i].y);
if(x != y)
{
nrc--;
Union(x, y);
cMin += h[i].c;
}
}
fout << cMin << "\n";
}
int main()
{
fin >> n >> m >> k;
for(int i = 1; i <= m; i++)
fin >> h[i].x >> h[i].y >> h[i].c;
Kruskal(m);
for(int i = 1; i <= k; i++)
{
m++;
fin >> h[m].x >> h[m].y >> h[m].c;
for(int j = m - 1; j > 0 && h[j + 1].c < h[j].c; j--)
swap(h[j+1], h[j]);
Kruskal(m);
}
return 0;
}