Cod sursa(job #2077129)

Utilizator valentinoMoldovan Rares valentino Data 27 noiembrie 2017 19:12:10
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.34 kb
#include <iostream>
#include <set>
#include <vector>
#include <queue>
#include <fstream>
#include <algorithm>
#define KMax 1000
#define NMax 250000
using namespace std;

class instream {
    public:
        instream() {}
        instream(const char *file_name) {
            input_file=fopen(file_name,"r");
            cursor=0;
            fread(buffer,SIZE,1,input_file);
        }
        inline instream &operator >>(int &n) {
            while((buffer[cursor]<'0'||buffer[cursor]>'9')&&buffer[cursor]!='-') {
                advance();
            }
            int semn=1;
            if (buffer[cursor]=='-')
                semn=-1,advance();
            n=0;
            while('0'<=buffer[cursor]&&buffer[cursor]<='9') {
                n=n*10+buffer[cursor]-'0';
                advance();
            }
            n*=semn;
            return *this;
        }
    private:
        FILE *input_file;
        static const int SIZE=1<<15;
        int cursor;
        char buffer[SIZE];
        inline void advance() {
            ++ cursor;
            if(cursor==SIZE) {
                cursor=0;
                fread(buffer,SIZE,1,input_file);
            }
        }
};

instream f("pscnv.in");
ofstream g("pscnv.out");

vector < pair < int, int > > graf[NMax + 5];
queue < int > mylist[KMax + 1];
int n, m, x, y;

void Solve()
{
    vector < int > best(n + 1, -1);
    mylist[0].push(x);
    for(int i = 0; i <= KMax; ++i)
    {
        if(mylist[i].empty()) continue;

        int nod;
        while(!mylist[i].empty())
        {
            nod = mylist[i].front();
            mylist[i].pop();

            if(nod == y)
            {
                g << i << '\n';
                return;
            }

            if(best[nod] != -1) continue;
            best[nod] = i;

            for(vector < pair < int, int > >::iterator edge = graf[nod].begin(); edge != graf[nod].end(); ++edge)
            {
                if(best[edge -> first] == -1)
                {
                    mylist[max(edge -> second, i)].push(edge -> first);
                }
            }
        }

    }
}

int main()
{
    int a, b, c;
    f >> n >> m >> x >> y;
    for(int i = 1; i <= m; ++i)
    {
        f >> a >> b >> c;
        graf[a].push_back(make_pair(b, c));
    }
    Solve();
}