Cod sursa(job #1401448)

Utilizator Ionut228Ionut Calofir Ionut228 Data 25 martie 2015 21:39:32
Problema PScNv Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.26 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 nod1, nod2, cost;
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 (sir[i] != ' ')
    {
        N = N * 10 + int(sir[i] - '0');
        ++i;
    }
    ++i;
    while (sir[i] != ' ')
    {
        M = M * 10 + int(sir[i] - '0');
        ++i;
    }
    ++i;
    while (sir[i] != ' ')
    {
        x = x * 10 + int(sir[i] - '0');
        ++i;
    }
    ++i;
    while (i <= lg - 1)
    {
        y = y * 10 + int(sir[i] - '0');
        ++i;
    }
}

void parsare2()
{
    fin.getline(sir, 50);
    nod1 = 0;
    nod2 = 0;
    cost = 0;
    int lg = strlen(sir);
    int i = 0;
    while (sir[i] != ' ')
    {
        nod1 = nod1 * 10 + int(sir[i] - '0');
        ++i;
    }
    ++i;
    while (sir[i] != ' ')
    {
        nod2 = nod2 * 10 + int(sir[i] - '0');
        ++i;
    }
    ++i;
    while (i <= lg - 1)
    {
        cost = cost * 10 + int(sir[i] - '0');
        ++i;
    }
}

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; i <= M; ++i)
    {
        //fin >> nod1 >> nod2 >> k;
        parsare2();
        V[nod1].push_back(make_pair(nod2, cost));
    }

    bellman(x);

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

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