Cod sursa(job #1401444)

Utilizator Ionut228Ionut Calofir Ionut228 Data 25 martie 2015 21:30:53
Problema PScNv Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>

using namespace std;

ifstream fin("pscnv.in");
ofstream fout("pscnv.out");

const int INF = 0x3f3f3f3f;

char sir[50];
int N, M, x, y;
int D[250005];
vector<pair<int, int> > V[250005];
queue<int> Q;
bool inQ[250005];
/*
void parsare1()
{
    fin.getline(sir, 50);
    int lg = strlen(sir);
    int i = 0;
    while (i != ' ')
    {
        N = N * 10 + int(sir[i] - '0');
        ++i;
    }
    ++i;
    while ()
}
*/
void bellman(int nod)
{
    memset(D, INF, sizeof(D));
    D[nod] = 0;
    Q.push(nod);
    inQ[nod] = true;

    while (!Q.empty())
    {
        int now = Q.front();
        Q.pop();
        inQ[now] = false;

        for (vector<pair<int, int> >::iterator it = V[now].begin(); it != V[now].end(); ++it)
        {
            int k = max(D[now], it->second);
            if (k < D[it->first])
            {
                D[it->first] = k;
                if (!inQ[it->first])
                {
                    Q.push(it->first);
                    inQ[it->first] = true;
                }
            }
        }
    }
}

int main()
{
    fin >> N >> M >> x >> y;
    //parsare1();
    for (int i = 1, nod1, nod2, k; i <= M; ++i)
    {
        fin >> nod1 >> nod2 >> k;
        //parsare2();
        V[nod1].push_back(make_pair(nod2, k));
    }

    bellman(x);

    fout << D[y] << '\n';

    fin.close();
    fout.close();
    return 0;
}