Cod sursa(job #1277528)

Utilizator EpictetStamatin Cristian Epictet Data 27 noiembrie 2014 19:59:11
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <fstream>
#include <queue>
#include <vector>
#define nod first
#define cost second
#define buffer 8192
using namespace std;
ifstream fin ("pscnv.in");
ofstream fout ("pscnv.out");
int N, M, X, Y, D[250010];
vector < pair < int, int > > V[250010];
queue < int > Q[1010];
char C[buffer+16], *poz;
bool fr[250010];

void Verif()
{
    if (*poz == '\0')
    {
        fin.get(C, buffer, '\0');
        poz = C;
    }
}

int Get()
{
    Verif();
    while (*poz < '0' || *poz > '9')
    {
        poz++;
        Verif();
    }
    int number = 0;
    while (*poz >= '0' && *poz <= '9')
    {
        number = number * 10 + *poz - '0';
        poz++;
        Verif();
    }
    return number;
}

int main()
{
    poz = C;
    Verif();
    N = Get(); M = Get(); X = Get(); Y = Get();
    int x, y, c;
    for (int i=1; i<=M; i++)
    {
        x = Get(); y = Get(); c = Get();
        V[x].push_back(make_pair(y, c));
    }

    for (int i=1; i<=N; i++) D[i] = 1111;
    D[X] = 0;
    Q[0].push(X);
    for (int i=0; i<=1000; i++)
    {
        while(!Q[i].empty())
        {
            int j = Q[i].front();
            Q[i].pop();

            if (fr[j]) continue;

            fr[j] = 1;

            if (j == Y)
            {
                fout << i << '\n';
                fout.close();
                return 0;
            }

            for (vector < pair < int, int > > :: iterator it = V[j].begin(); it != V[j].end(); it++)
            {
                if (D[it->nod] > max(it->cost, D[j]))
                {
                    D[it->nod] = max(it->cost, D[j]);
                    Q[D[it->nod]].push(it->nod);
                }
            }
        }
    }
}