Cod sursa(job #2077123)

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

ifstream 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();
}