Cod sursa(job #1284844)

Utilizator sebinechitasebi nechita sebinechita Data 6 decembrie 2014 21:02:35
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("pscnv.in");
ofstream fout("pscnv.out");
#define MAX 250010
vector <pair<int, int> > G[MAX];
vector <int> q[1001];
typedef vector <pair <int, int> > :: iterator iter;
int n, m, xs, xe, x, y, c;
int k, viz[MAX], nod;

const unsigned maxb = 666013;
char buf[maxb];
unsigned int ptr = maxb - 1;

int getInt()
{
    int rez = 0;
    while(! (buf[ptr] >= '0' && buf[ptr] <= '9'))
    {
        if(++ptr >= maxb)
        {
            fin.read(buf, maxb);
            ptr = 0;
        }
    }
    while((buf[ptr] >= '0' && buf[ptr] <= '9'))
    {
        rez = rez * 10 + buf[ptr] - '0';
        if(++ptr >= maxb)
        {
            fin.read(buf, maxb);
            ptr = 0;
        }
    }
    return rez;
}


int main()
{
    n = getInt();
    m = getInt();
    xs = getInt();
    xe = getInt();

    while(m--)
    {
        x = getInt();
        y = getInt();
        c = getInt();
        G[x].push_back(make_pair(y, c));
    }
    q[0].push_back(xs);
    k = -1;
    while(!viz[xe])
    {
        k++;
        while(q[k].size())
        {
            nod = q[k][q[k].size() - 1];
            q[k].pop_back();
            if(viz[nod])
                continue;
            viz[nod] = 1;
            for(iter it = G[nod].begin() ; it != G[nod].end() ; it++)
            {
                if(!viz[it->first])
                {
                    if(it->second <= k)
                    {
                        q[k].push_back(it->first);
                    }
                    else
                    {
                        q[it->second].push_back(it->first);
                    }
                }
            }
            if(viz[xe])
                break;
        }
    }
    fout << k << "\n";
}