Cod sursa(job #2671358)

Utilizator EuAlexOtaku Hikikomori EuAlex Data 11 noiembrie 2020 22:49:21
Problema PScNv Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <cstdio>
#include <vector>
#include <cstring>
#include <stack>
#include <climits>

using namespace std;

const int nmax = 250000;

struct Dest {
    int nod, cost;
};

vector <Dest> g[1 + nmax];
stack <int> d[1005];
int dist[1 + nmax];
int viz[1 + nmax];
int n, m, ss, ff;

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 getFirst() {
    for(int i = 1; i <= 1000; i ++) {
        if(!d[i].empty()) {
            int foo = d[i].top();
            d[i].pop();
            return foo;
        }
    }
    return -1;
}

void solve() {
    for(int i = 1; i <= n; i ++)
        dist[i] = INT_MAX;
    dist[ss] = 0;
    d[1].push(ss);

    int node = getFirst();
    while(node != -1) {

        if(viz[node] == 0) {
            viz[node] = 1;
            for(auto &to: g[node]) {
                if(to.cost < dist[to.nod]) {
                    dist[to.nod] = to.cost;
                    d[to.cost].push(to.nod);
                }
            }
        }

        node = getFirst();
    }
}

void afisare() {
    printf("%d", dist[ff]);
}

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

    citire();
    solve();
    afisare();

    return 0;
}