Cod sursa(job #1462339)

Utilizator SmarandaMaria Pandele Smaranda Data 17 iulie 2015 19:58:08
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

const int N = 2150004, K = 1002, inf = (1ll << 31) - 1;

vector <pair <int, int> > graph [N];
vector <pair <int, int> > :: iterator it;
queue <int> q [K];

int n, m, d [N];
bool used [N];

int main () {
    int i, x, y, a, b, c, last, lim = 0;

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

    scanf ("%d%d%d%d", &n, &m, &x, &y);
    for (i = 1; i <= m; i ++) {
        scanf ("%d%d%d", &a, &b, &c);
        lim = max (lim, c);
        graph [a].push_back (make_pair (b, c));
    }
    for (i = 1; i <= n; i ++) {
        if (!graph [i].empty ()) {
            sort (graph [i].begin (), graph [i].end ());
        }
        d [i] = inf;
    }
    last = 0;
    q [0].push (x);
    d [x] = 0;
    for (i = last; i <= lim; i ++) {
        while (!q [i].empty ()) {
            a = q [i].front ();
            q [i].pop ();
            if (used [a])
                continue;
            used [a] = 1;
            for (it = graph [a].begin (); it != graph [a].end (); ++ it) {
                b = (*it).first;
                c = max ((*it).second, d [a]);
                if (d [b] > c) {
                    d [b] = c;
                    q [d [b]].push (b);
                }
            }
        }
    }
    printf ("%d\n", d [y]);
    return 0;
}