Pagini recente »
Istoria paginii runda/2024-02-25-clasa-5-concurs04/clasament
|
Istoria paginii utilizator/ilinca-ana
|
Istoria paginii runda/2015-02-17-clasa-5-tema-25/clasament
|
Cod sursă (job #677301)
|
Cod sursă (job #757061)
Cod sursă (job
#757061)
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), fin.tie(0);
using namespace std;
ifstream fin("zapada.in");
ofstream fout("zapada.out");
class edge
{
public:
int x, y, cost;
edge(int x, int y, int cost)
{
this->x = x;
this->y = y;
this->cost = cost;
}
bool operator<(const edge& a) const
{
return this->cost < a.cost;
}
};
vector<int> t, Rank;
int Find(int i)
{
if (t[i] == i)
return i;
return t[i] = Find(t[i]);
}
void Union(int x, int y)
{
int x_root = Find(x);
int y_root = Find(y);
if (Rank[x_root] < Rank[y_root])
t[x_root] = y_root;
else if (Rank[x_root] > Rank[y_root])
t[y_root] = x_root;
else
{
t[x_root] = y_root;
Rank[y_root]++;
}
}
int kruskal(vector<edge>& edges, int n)
{
sort(edges.begin(), edges.end());
t.resize(n + 1);
Rank.resize(n + 1, 0);
for (int i = 1; i <= n; ++i)
t[i] = i;
int costMin = 0;
for (auto& e : edges)
{
int x = e.x;
int y = e.y;
int cost = e.cost;
if (Find(x) != Find(y))
{
costMin += cost;
Union(x, y);
}
}
return costMin;
}
int main()
{
fastio
int n, m, k;
fin >> n >> m >> k;
vector<edge> edges;
for (int i = 0; i < m; ++i)
{
int x, y, cost;
fin >> x >> y >> cost;
edges.emplace_back(x, y, cost);
}
int c0 = kruskal(edges, n);
fout << c0 << '\n';
for (int i = 0; i < k; ++i)
{
int x, y, cost;
fin >> x >> y >> cost;
edges.emplace_back(x, y, cost);
int ck = kruskal(edges, n);
fout << ck << '\n';
}
return 0;
}