Pagini recente »
Borderou de evaluare (job #582016)
|
Borderou de evaluare (job #201172)
|
Borderou de evaluare (job #750977)
|
Borderou de evaluare (job #144908)
|
Cod sursă (job #804478)
Cod sursă (job
#804478)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("zapada.in");
ofstream fout("zapada.out");
set<unsigned short> s;
set<unsigned short> :: iterator it;
vector< pair<short, short> > v[100005];
vector< pair<short, short> > :: iterator it2;
short n, k, t[10002];
int m, costTotal;
void Union(short x, short y)
{
t[y] = x;
}
short Find(short x)
{
short y, rad = x;
while (t[rad] != 0)
rad = t[rad];
while (x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
void Citire()
{
int c;
short x, y;
fin >> n >> m >> k;
for (int i = 1; i <= m; i++)
{
fin >> x >> y >> c;
s.insert(c);
v[c].push_back({x, y});
}
}
void Afis()
{
fout << costTotal << "\n";
}
void Kruskal()
{
short nrc = n, x, y;
int cost;
pair<short, short> a;
for(it = s.begin();it != s.end() && nrc > 1;it++)
{
cost = *it;
for(it2 = v[cost].begin();it2 != v[cost].end() && nrc > 1;it2++)
{
a = *it2;
x = a.first;
y = a.second;
x = Find(x);
y = Find(y);
if (x != y)
{
Union(x, y);
nrc--;
costTotal += cost;
}
}
}
Afis();
}
int main()
{
int c;
short x, y;
Citire();
Kruskal();
for(short i = 1;i <= k;i++)
{
fin >> x >> y >> c;
s.insert(c);
v[c].push_back({x, y});
costTotal = 0;
for(short j = 1;j <= n;j++)
t[j] = 0;
Kruskal();
}
fout.close();
return 0;
}