Cod sursa(job #2479230)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 23 octombrie 2019 16:12:49
Problema PScNv Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

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

constexpr int NMAX = 25e4 + 5;
constexpr int bsize = (1<<16);

int n, m, x, y;

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

char ch[bsize+1];
int pos=0;

void read (int &x)
{
    while (!isdigit(ch[pos]))
    {
        pos++;
        if (pos == bsize)
        {
            pos = 0;
            fread(ch, 1, bsize, stdin);
        }
    }

    x=0;

    while (isdigit(ch[pos]))
    {
        x = x * 10 + ch[pos] - '0';
        pos++;
        if (pos == bsize)
        {
            pos = 0;
            fread(ch, 1, bsize, stdin);
        }
    }
}

void dfs (int nod, int val)
{
    viz[nod] = 1;
    for (int i=0; i<G[nod].size(); ++i)
    {
        int y = G[nod][i].first;
        int cost = G[nod][i].second;

        if (!viz[y] && cost <= val) dfs(y, val);
    }
}

bool verif(int val)
{
    for (int i=1; i<=n; ++i)
        viz[i] = 0;

    dfs(x, val);

    return (viz[y] == 1);
}

int main()
{
    freopen("pscnv.in", "r", stdin);
    freopen("pscnv.out", "w", stdout);
    fread(ch, 1, bsize, stdin);
    ios_base::sync_with_stdio(false);
    pos = 0;

    read(n);
    read(m);
    read(x);
    read(y);

    int dr=0;
    for (int i=1; i<=m; ++i)
    {
        int a, b, c;
        read(a);
        read(b);
        read(c);

        G[a].pb({b, c});
        dr = max(dr, c);
    }

    int st=1;
    int sol=0;

    while (st <= dr)
    {
        int mij = (st + dr) / 2;

        if (verif(mij) == true)
        {
            sol = mij;
            dr = mij - 1;
        }
        else st = mij + 1;
    }

    cout << sol << '\n';
    return 0;
}