Cod sursa(job #2671205)

Utilizator EuAlexOtaku Hikikomori EuAlex Data 11 noiembrie 2020 18:18:45
Problema PScNv Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

const int nmax = 250000;

struct Dest {
    int nod, cost;
};

vector <Dest> g[1 + nmax];
int n, m, ss, ff;
bool viz[1 + nmax];

void citire() {
    scanf("%d%d%d%d", &n, &m, &ss, &ff);
    for(int i = 1; i <= m; i ++) {
        int x, y, k;
        scanf("%d%d%d", &x, &y, &k);
        g[x].push_back({y, k});
    }
}

int dfs(int from, int limit) {
    viz[from] = 1;
    for(auto& to: g[from]) {
        if(viz[to.nod] == false && to.cost <= limit) {
            dfs(to.nod, limit);
        }
    }
}

int cautBin() {
    int left = 1, right = 1000, last = -1;

    while(left <= right) {
        int med = (left + right) / 2;
        memset(viz, 0, sizeof(viz));
        dfs(ss, med);
        if(viz[ff] == true) {
            right = med - 1;
            last = med;
        } else {
            left = med + 1;
        }
    }

    return last;
}

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

    citire();
    printf("%d", cautBin());

    return 0;
}