Cod sursă (job #804459)

Utilizator avatar lucamateigabriel Luca Matei Gabriel lucamateigabriel IP ascuns
Problemă Zapada (clasele 11 și 12) Compilator cpp-32 | 1.30 kb
Rundă Arhiva de probleme Status evaluat
Dată 15 ian. 2025 09:33:50 Scor 0
#include <bits/stdc++.h>
using namespace std;

ifstream fin("zapada.in");
ofstream fout("zapada.out");
struct muchie
{
    int c;
    short x, y;
    bool operator< (muchie m) const
    {
        return c < m.c;
    }
};
muchie a[101005];
short n, k, t[10002];
int m, costTotal;

void Union(short x, short y)
{
    t[y] = x;
}

int 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()
{
    fin >> n >> m >> k;
    for (int i = 1; i <= m; i++)
        fin >> a[i].x >> a[i].y >> a[i].c;
}
void Afis()
{
    fout << costTotal << "\n";
}
void Kruskal(short l)
{
    short i, nrc = n, x, y;
    for(i = 1; i <= l && nrc > 1; i++)
    {
        x = Find(a[i].x);
        y = Find(a[i].y);
        if (x != y)
        {
            Union(x, y);
            nrc--;
            costTotal += a[i].c;
        }
    }
    Afis();
}


int main()
{
    Citire();
    Kruskal(m);
    for(short i = 1;i <= k;i++)
    {
        fin >> a[m + i].x >> a[m + i].y >> a[m + i].c;
        costTotal = 0;
        for(short j = 1;j <= n;j++)
            t[j] = 0;
        Kruskal(m + i);
    }
    fout.close();
    return 0;
}