Cod sursă (job #676005)

Utilizator avatar brianna_enache Enache Brianna brianna_enache IP ascuns
Problemă Zapada (clasele 11 și 12) Compilator cpp-32 | 1,19 kb
Rundă vaslui_cls1112_15.11 Status evaluat
Dată 15 nov. 2022 20:07:05 Scor 40
#include <bits/stdc++.h>
using namespace std;

ifstream in("zapada.in");
ofstream out("zapada.out");
struct Muchie
{
    int x, y, cost, ales;
};

int t[1005], n, m, k;
Muchie a[5001];

bool Cmp(Muchie A, Muchie B)
{
    return A.cost < B.cost;
}

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

int Find(int x)
{
    int rad = x, y;
    while(t[rad] != 0)
        rad = t[rad];
    while(x != rad)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }
    return rad;
}
int SolMN(int m, Muchie a[])
{
    int i, x, y, costMin = 0;
    sort(a + 1,a + m + 1, Cmp);
    for(i = 1; i <= n; i++) t[i]=0;
    for(i = 1; i <= m; i++)
    {
        x = Find(a[i].x);
        y = Find(a[i].y);
        if(x != y)
        {
            Union(x,y);
            costMin += a[i].cost;
        }
    }
    return costMin;
}

int main()
{
    int i, x, y, costMin = 0;
    in >> n >> m >> k;
    for(i = 1; i <= m; i++)
        in >> a[i].x >> a[i].y >> a[i].cost;
    out << SolMN(m, a) << "\n";
    for(int p = 1; p <= k; p++)
    {
        in >> a[m + p].x >> a[m + p].y >> a[m + p].cost;
        out << SolMN(m + p, a) << "\n";
    }
    return 0;
}