Cod sursa(job #2479270)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 23 octombrie 2019 17:11:37
Problema PScNv Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int BSIZE = (1 << 16), NMAX = 25e4 + 5;

int N, M, X, Y, Max;

vector < pair < int, int > > G[NMAX];

bool Sel[NMAX];

int pos = BSIZE - 1;
char buff[BSIZE];

static inline char C ()
{
    ++pos;

    if(pos == BSIZE)
    {
        pos = 0;

        fread(buff, 1, BSIZE, stdin);
    }

    return buff[pos];
}

static inline int I ()
{
    int r = 0;

    for(;;)
    {
        int Ch = C();

        if(isdigit(Ch))
        {
            r = (Ch - '0');

            break;
        }
    }

    for(;;)
    {
        char Ch = C();

        if(isdigit(Ch))
            r = r * 10 + (Ch - '0');
        else break;
    }

    return r;
}

static inline void Read ()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);

    freopen("pscnv.in", "r", stdin);
    freopen("pscnv.out", "w", stdout);

    N = I();
    M = I();
    X = I();
    Y = I();

    for(int i = 1; i <= M; ++i)
    {
        int nx = I(), ny = I(), k = I();

        G[nx].push_back({k, ny});

        Max = max(Max, k);
    }

    return;
}

static inline void Go (int Node, int W)
{
    Sel[Node] = true;

    for(auto it : G[Node])
        if(!Sel[it.second] && it.first <= W)
            Go(it.second, W);

    return;
}

static inline bool Ok (int W)
{
    for(int i = 1; i <= N; ++i)
        Sel[i] = false;

    Go(X, W);

    return Sel[Y];
}

static inline void Solve ()
{
    int Left = 1, Right = Max, ans = 0;

    while(Left <= Right)
    {
        int Mid = (Left + Right) / 2;

        if(Ok(Mid))
        {
            ans = Mid;

            Right = Mid - 1;
        }
        else Left = Mid + 1;
    }

    printf("%d\n", ans);

    return;
}

int main()
{
    Read();

    Solve();

    return 0;
}