Pagini recente »
Istoria paginii runda/2024-02-11-clasa-8-tema-17
|
Atașamentele paginii Clasament olimpiada_pe_scoala_9_2018
|
Istoria paginii runda/lasm_21_12_2021_cl12
|
Istoria paginii runda/pregatire_lot_juniori_runda1
|
Cod sursă (job #675994)
Cod sursă (job
#675994)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("zapada.in");
ofstream fout("zapada.out");
struct Muchie
{
int x, y, cost, ales;
} a[100005];
int t[10005], n, m, k;
bool Cmp(Muchie A, Muchie B)
{
return A.cost < B.cost;
}
void Union(int x, int y)
{
t[y] = x;
}
int Find(int x)
{
int y, rad = x;
while (t[rad] != 0)
rad = t[rad];
while (x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
/**
6 9 3
1 2 3
1 3 1
1 4 8
1 6 3
2 3 4
3 4 3
4 5 4
4 6 6
5 6 2
*/
int main()
{
int i, m, x, y, costMin = 0, j;
fin >> n >> m >> k;
for (i = 1; i <= m; i++)
fin >> a[i].x >> a[i].y >> a[i].cost;
sort(a + 1, a + m + 1, Cmp);
for (i = 1; i <= m; i++)
{
x = Find(a[i].x);
y = Find(a[i].y);
if (x != y)
{
Union(x, y);
costMin += a[i].cost;
a[i].ales = 1;
}
}
fout << costMin << "\n";
for(j = 1; j <= k; j++)
{
m++;
fin >> a[m].x >> a[m].y >> a[m].cost;
for(i = 1; i <= m; i++)
t[i] = 0;
costMin = 0;
sort(a + 1, a + m + 1, Cmp);
for (i = 1; i <= m; i++)
{
x = Find(a[i].x);
y = Find(a[i].y);
if (x != y)
{
Union(x, y);
costMin += a[i].cost;
a[i].ales = 1;
}
}
fout << costMin << "\n";
}
return 0;
}