Pagini recente »
Cod sursă (job #804613)
Cod sursă (job
#804613)
#include <fstream>
#include <vector>
#include <map>
#include <iostream>
#include <queue>
#include <bitset>
#include <algorithm>
using namespace std;
ifstream fin("zapada.in");
ofstream fout("zapada.out");
struct muchie {
int x, y, c;
bool operator <(const muchie& other)const
{
return(*this).c < other.c;
}
};
muchie a[100005];
int t[10005], r[10005];
int find(int nod)
{
if (t[nod] == 0)
return nod;
int y = find(t[nod]);
t[nod] = y;
return y;
}
void Union(int x, int y)
{
if (r[x] > r[y])
t[y] = x;
else
{
t[x] = y;
if (r[x] == r[y])
r[y]++;
}
}
int main()
{
int n, m, k;
fin >> n >> m >> k;
for (int i = 1; i <= m; i++)
{
int x, y, c;
fin >> a[i].x >> a[i].y >> a[i].c;
}
for (int ind = 1; ind <= k + 1; ind++)
{
int s = 0;
sort(a + 1, a + m + 1);
for (int i = 1; i <= m; i++)
{
int x = find(a[i].x);
int y = find(a[i].y);
if (x != y)
{
Union(x, y);
s += a[i].c;
}
}
fout << s<<"\n";
for (int i = 1; i <= n; i++)
r[i] = t[i] = 0;
fin >> a[++m].x >> a[m].y >> a[m].c;
}
return 0;
}