Cod sursa(job #1462340)

Utilizator SmarandaMaria Pandele Smaranda Data 17 iulie 2015 20:00:14
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.16 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

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

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

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

class myIfstream {
private :
    char buffer [SIZE];
    FILE *input;
    int cursor;
    void advance () {
        ++ cursor;
        if (cursor == SIZE) {
            cursor = 0;
            fread (buffer, 1, SIZE, input);
        }
    }
public :
    myIfstream ();
    myIfstream (const char *inputName) {
        input = fopen (inputName, "r");
        fread (buffer, 1, SIZE, input);
        cursor = 0;
    }
    myIfstream &operator >> (int &value) {
        value = 0;
        while (!(buffer [cursor] >= '0' && buffer [cursor] <= '9'))
            advance ();
        while (buffer [cursor] >= '0' && buffer [cursor] <= '9') {
            value = value * 10 + buffer [cursor] - '0';
            advance ();
        }
        return *this;
    }
};

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

   myIfstream fin ("pscnv.in");
    freopen ("pscnv.out", "w", stdout);

    fin >> n >> m >> x >> y;
    for (i = 1; i <= m; i ++) {
        fin >> 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;
}