Cod sursă (job #675965)

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

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

struct vrajeala
{
    int x, y, cost;
};

bool cmp(vrajeala A, vrajeala B)
{
    return A.cost < B.cost;
}

vrajeala a[101005];
int n, m, k;
int t[10005];

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;
}

int main()
{
    int x, y, cost_min;
    fin >> n >> m >> k;
    for (int i = 1; i <= m; i++)
        fin >> a[i].x >> a[i].y >> a[i].cost;
    sort(a + 1, a + m + 1, cmp);
    cost_min = 0;
    for (int i = 1; i <= m; i++)
    {
        x = Find(a[i].x);
        y = Find(a[i].y);
        if (x != y)
        {
            Union(x, y);
            cost_min += a[i].cost;
        }
    }
    fout << cost_min << "\n";
    for (int i = m + 1; i <= m + k; i++)
    {
        fin >> a[i].x >> a[i].y >> a[i].cost;
        sort(a + 1, a + i + 1, cmp);
        for (int j = 1; j <= n; j++)
            t[j] = 0;
        cost_min = 0;
        for (int j = 1; j <= i; j++)
        {
            x = Find(a[j].x);
            y = Find(a[j].y);
            if (x != y)
            {
                Union(x, y);
                cost_min += a[j].cost;
            }
        }
        fout << cost_min << "\n";
    }
    return 0;
}