Pagini recente »
Istoria paginii runda/2024-01-19-clasa-5-tema-21/clasament
|
Tema 7 clasele 9-10 2014/15
|
Istoria paginii runda/c21_6/clasament
|
Istoria paginii runda/2021-06-10-clasa-5-tema-33/clasament
|
Cod sursă (job #675999)
Cod sursă (job
#675999)
//in the name of God
#include <bits/stdc++.h>
using namespace std;
ifstream fin("zapada.in");
ofstream fout("zapada.out");
const int NMAX = 10007;
int n, m, k, c, cost;
int t[NMAX], rad[NMAX], viz[NMAX];
struct vrajeala
{
int a, b, cost;
};
vector <vrajeala> nod, nou;
bool cmp(vrajeala a, vrajeala 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] != rad)
rad = t[rad];
///comprimare
while (x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
int main()
{
fin >> n >> m >> k;
int x, y;
for (int i = 1; i <= m; i++)
{
fin >> x >> y >> c;
nod.push_back({ x,y,c });
}
sort(nod.begin(), nod.end(), cmp);
for (int i = 1; i <= n; i++)
t[i] = i;
for (int i = 0; i < m; i++)
{
x = Find(nod[i].a);
y = Find(nod[i].b);
if (x != y)
{
Union(x, y);
nou.push_back(nod[i]);
cost += nod[i].cost;
}
cout << 8;
}
fout << cost << '\n';
while (k--)
{
fin >> x >> y >> c;
if (c < nou[nou.size() - 1].cost) /// nu are rost sa caut daca e deja mai mare si drumul era posibil
{
nou.push_back({ x,y,c });
sort(nou.begin(), nou.end(), cmp);
/// copy pasta de la inceput
/// dar dau si reset
nod = nou;
nod.clear();
cost = 0;
for (int i = 1; i <= n; i++)
{
t[i] = i;
rad[i] = 0;
}
for (int i = 0; i < n; i++)
{
x = Find(nod[i].a);
y = Find(nod[i].b);
if (x != y)
{
Union(x, y);
nou.push_back(nod[i]);
cost += nod[i].cost;
}
}
fout << cost << '\n';
}
else fout << cost << '\n'; /// ramamee neschimbat
}
return 0;
}