Cod sursa(job #3342672)

Utilizator tonealexandruTone Alexandru tonealexandru Data 25 februarie 2026 10:57:52
Problema PScNv Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int NMAX = 250005;

vector<pair<int, int>> adj[NMAX];
queue<int> q;
int save[NMAX];

signed main()
{
    ifstream cin("pscnv.in");
    ofstream cout("pscnv.out");
    int n, m, x, y, a, b, val, maxx = 0;
    cin>>n>>m>>x>>y;

    for(int i=0; i<m; i++)
    {
        cin>>a>>b>>val;

        adj[a].push_back({b, val});
        maxx = max(maxx, val);
    }

    int st = 0, dr = maxx * 2 + 1, rez = -1;
    while(st < dr)
    {
        int mij = (st + dr) / 2;
        //cout<<mij<<'\n';

        for(int i=1; i<=n; i++)
            save[i] = 21e8;

        q.push(x);
        save[x] = 0;

        while(q.empty() == false)
        {
            int cur = q.front(); q.pop();

            for(auto xi : adj[cur])
            {
                if(save[xi.first] > save[cur] + xi.second && xi.second <= mij)
                {
                    save[xi.first] = save[cur] + xi.second;
                    q.push(xi.first);
                }
            }
        }

        if(save[y] != 21e8) /// e ok
        {
            dr = mij - 1;
            rez = mij;
        }
        else
            st = mij + 1;
    }

    cout<<rez;



    return 0;
}