Cod sursa(job #2677539)

Utilizator FrostfireMagirescu Tudor Frostfire Data 26 noiembrie 2020 19:35:09
Problema PScNv Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 250000

using namespace std;

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

int n, m, x, y, maxi;
bool viz[NMAX+10];
vector <pair <int, int> > nod[NMAX+10];
queue <int> Q;

int isOk(int val)
{   Q.push(x);
    for(int i=1; i<=n; i++) viz[i] = 0;
    viz[x] = 1;
    while(!Q.empty())
        {   int x = Q.front();
            Q.pop();
            if(x == y) return 1;
            for(int i=0; i<nod[x].size(); i++)
                if(!viz[nod[x][i].first] && nod[x][i].second <= val)
                    {   viz[nod[x][i].first] = 1;
                        Q.push(nod[x][i].first);
                    }
        }
    return 0;
}

int main()
{
    f >> n >> m >> x >> y;
    for(int i=1; i<=m; i++)
        {   int nod1, nod2, cost;
            f >> nod1 >> nod2 >> cost;
            nod[nod1].push_back({nod2, cost});
            nod[nod2].push_back({nod1, cost});
            maxi = max(maxi, cost);
        }
    int st = 1, dr = maxi, ans = 0;
    while(st <= dr)
        {   int mij = (st + dr) / 2;
            if(isOk(mij)) ans = mij, dr = mij - 1;
            else st = mij + 1;
        }
    g << ans << '\n';
    return 0;
}