Cod sursa(job #3145962)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 17 august 2023 16:04:27
Problema PScNv Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

ifstream in ("pscnv.in");
ofstream out ("pscnv.out");

const int max_size = 25e4 + 1;

struct str{
    int x, y, c;
    bool operator < (const str & aux) const
    {
        return c < aux.c;
    }
};

int t[max_size];
vector <str> mc;

int rad (int x)
{
    if (x == t[x])
    {
        return x;
    }
    return t[x] = rad(t[x]);
}

int main ()
{
    int n, m, x, y;
    in >> n >> m >> x >> y;
    for (int i = 1; i <= n; i++)
    {
        t[i] = i;
    }
    while (m--)
    {
        int a, b, c;
        in >> a >> b >> c;
        mc.push_back({a, b, c});
    }
    sort(mc.begin(), mc.end());
    for (auto f : mc)
    {
        if (rad(f.x) != rad(f.y))
        {
            t[rad(f.x)] = rad(f.y);
        }
        if (rad(x) == rad(y))
        {
            out << f.c;
            exit(0);
        }
    }
    in.close();
    out.close();
    return 0;
}