Cod sursă (job #804613)

Utilizator avatar 1gbr1 Gabara 1gbr1 IP ascuns
Problemă Zapada (clasele 11 și 12) Compilator cpp-32 | 1,34 kb
Rundă Arhiva de probleme Status evaluat
Dată 16 ian. 2025 14:57:05 Scor 40
#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;
}