Cod sursă (job #675994)

Utilizator avatar cosminqf Danciu Cosmin cosminqf IP ascuns
Problemă Zapada (clasele 11 și 12) Compilator cpp-32 | 1,55 kb
Rundă Arhiva de probleme Status evaluat
Dată 15 nov. 2022 19:57:04 Scor 40
#include <bits/stdc++.h>

using namespace std;

ifstream fin("zapada.in");
ofstream fout("zapada.out");

struct Muchie
{
    int x, y, cost, ales;
} a[100005];

int t[10005], n, m, k;

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 y, rad = x;
    while (t[rad] != 0)
        rad = t[rad];
    while (x != rad)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }
    return rad;
}
/**
6 9 3
1 2 3
1 3 1
1 4 8
1 6 3
2 3 4
3 4 3
4 5 4
4 6 6
5 6 2
*/
int main()
{
    int i, m, x, y, costMin = 0, j;
    fin >> n >> m >> k;
    for (i = 1; i <= m; i++)
        fin >> a[i].x >> a[i].y >> a[i].cost;
    sort(a + 1, a + m + 1, Cmp);
    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;
            a[i].ales = 1;
        }
    }
    fout << costMin << "\n";
    for(j = 1; j <= k; j++)
    {
        m++;
        fin >> a[m].x >> a[m].y >> a[m].cost;
        for(i = 1; i <= m; i++)
            t[i] = 0;
        costMin = 0;
        sort(a + 1, a + m + 1, Cmp);
        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;
                a[i].ales = 1;
            }
        }
        fout << costMin << "\n";
    }
    return 0;
}