Pagini recente »
Monitorul de evaluare
|
Monitorul de evaluare
|
examen_clasa_5
|
Monitorul de evaluare
|
Cod sursă (job #758980)
Cod sursă (job
#758980)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("zapada.in");
ofstream fout("zapada.out");
struct Trei
{
int x, y, c;
bool operator<(const Trei e) const
{
return c < e.c;
}
} a[102005];
int t[10005];
int n, m, k;
int FindRoot(int x)
{
while(x != t[x])
x = t[x];
return x;
}
void Union(int x, int y)
{
t[y] = x;
}
int Kruskal()
{
int nrc, cost, x, y, i;
cost = 0;
nrc = n;
for(i=1; nrc > 1; i++)
{
x = FindRoot(a[i].x);
y = FindRoot(a[i].y);
if(x != y)
{
Union(x, y);
nrc--;
cost += a[i].c;
}
}
return cost;
}
int main()
{
ios_base::sync_with_stdio(0);
fin.tie(0);
fout.tie(0);
int i, j, x, y, c;
int total;
fin >> n >> m >> k;
for(i=1; i<=m; i++)
fin >> a[i].x >> a[i].y >> a[i].c;
sort(a + 1, a + m + 1);
for(j=1; j<=n; j++)
t[j] = j;
total = Kruskal();
fout << total << "\n";
for(i=1; i<=k; i++)
{
fin >> x >> y >> c;
if(a[m].c > c)
{
m++;
a[m] = {x, y, c};
for(j=m; j>=1; j--)
if(a[j].c < a[j - 1].c) swap(a[j], a[j - 1]);
else break;
for(j=1; j<=n; j++)
t[j] = j;
total = Kruskal();
fout << total << "\n";
}
else fout << total << "\n";
}
fin.close();
fout.close();
return 0;
}