Pagini recente »
Istoria paginii utilizator/alexandrapaunescu
|
Cod sursă (job #222493)
|
Monitorul de evaluare
|
Monitorul de evaluare
|
Cod sursă (job #758985)
Cod sursă (job
#758985)
#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], v[102005];
int t[10005];
int n, m, k, l;
int FindRoot(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 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--;
v[++l] = {a[i].x, a[i].y, a[i].c};
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);
l = 0;
total = Kruskal();
fout << total << "\n";
for(i=1; i<=k; i++)
{
fin >> x >> y >> c;
if(v[l].c > c)
{
l++;
v[l] = {x, y, c};
for(j=l;j>=1;j--)
if(v[j].c < v[j - 1].c) swap(v[j], v[j - 1]);
else break;
for(j=1;j<=l;j++)
{
a[j] = v[j];
v[j] = {0, 0, 0};
}
l = 0;
for(j=1; j<=n; j++)
t[j] = 0;
total = Kruskal();
fout << total << "\n";
}
else fout << total << "\n";
}
fin.close();
fout.close();
return 0;
}