Cod sursa(job #3146057)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 18 august 2023 13:29:59
Problema PScNv Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.91 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

class InParser
{
private:
    FILE *fin;
    char *buff;
    int sp;
    char read_ch()
    {
        ++sp;
        if (sp == 65536)
        {
            sp = 0;
            fread(buff, 1, 65536, fin);
        }
        return buff[sp];
    }
public:
    InParser(const char* nume)
    {
        fin = fopen(nume, "r");
        buff = new char[65536]();
        sp = 65536 - 1;
    }
    InParser& operator >> (int &n)
    {
        char c;
        while(!isdigit(c = read_ch()));
        n = c - '0';
        while(isdigit(c = read_ch()))
        {
            n = 10 * n + c - '0';
        }
        return *this;
    }
};

InParser cin ("pscnv.in");
ofstream cout ("pscnv.out");

using pa = pair <int, int>;

const int INF = 1001;
const int N = 25e4;
int cost[N + 1];

vector <pa> g[N + 1];

int n, m, x, y, xi, yi, ki;

bool ok (int val)
{
    for (int i = 1; i <= n; ++i)
        cost[i] = INF;
    cost[x] = 0;
    queue <int> q;
    q.push(x);
    while (!q.empty())
    {
        int node = q.front();
        q.pop();
        for (auto it : g[node])
            if (it.second <= val)
                if (cost[it.first] > max(cost[node], it.second))
                    cost[it.first] = max(cost[node], it.second), q.push(it.first);
    }
    return (cost[y] != INF);
}

int cb (int st, int dr)
{
    int med, last = 1;
    while (st <= dr)
    {
        med = (st + dr) >> 1;
        if (ok(med))
        {
            last = med;
            dr = med - 1;
        }
        else
            st = med + 1;
    }
    return last;
}

int main()
{
    cin >> n >> m >> x >> y;
    for (int i = 1; i <= m; ++i)
    {
        cin >> xi >> yi >> ki;
        if (xi != yi)
            g[xi].push_back({yi, ki});
    }
    cout << cb (1, 1000) << '\n';
    return 0;
}