Cod sursa(job #1768930)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 1 octombrie 2016 18:30:57
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define DIM 1000000
#define MAXN 250050
#define inf 0x3f3f3f3f

using namespace std;

char s[DIM];
int cursor;

void moveCursor()
{
    cursor++;
    if (cursor == DIM) {
        fread(s, 1, DIM, stdin);
        cursor = 0;
    }
}

int getInt()
{
    while (!(s[cursor] >= '0' && s[cursor] <= '9'))
        moveCursor();
    int nr = 0;
    while (s[cursor] >= '0' && s[cursor] <= '9') {
        nr = nr*10 + s[cursor] - '0';
        moveCursor();
    }
    return nr;
}

int n, m, x, y;
vector<pair<int, int> > graf[MAXN];
queue<int> cozi[1001];
int best[MAXN];

void citire()
{
    n = getInt();
    m = getInt();
    x = getInt();
    y = getInt();
    for (int i = 1; i <= m; i++) {
        int x = getInt();
        int y = getInt();
        int k = getInt();
        graf[x].push_back(make_pair(y, k));
    }
    for (int i = 1; i <= n; i++)
        best[i] = 1000;
}

int solve()
{
    cozi[0].push(x);
    best[x] = 0;
    for (int crt = 0; crt <= 1000; ) {
        if (cozi[crt].empty()) {
            crt++;
            continue;
        }
        int top = cozi[crt].front();
        if (top == y)
            return crt;
        cozi[crt].pop();
        if (best[top] != crt) continue;
        for (pair<int, int> p : graf[top]) {
            if (max(best[top], p.second) < best[p.first]) {
                best[p.first] = max(best[top], p.second);
                cozi[best[p.first]].push(p.first);
            }
        }
    }
}

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

    citire();
    int rez = solve();
    printf("%d ", rez);

    return 0;
}