Cod sursa(job #1401488)

Utilizator Ionut228Ionut Calofir Ionut228 Data 25 martie 2015 22:06:16
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.27 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];

struct muchie
{
    int nod, d;

    bool operator < (const muchie& m) const
    {
        if (m.d < d) return true;
        return false;
    }
};
priority_queue<muchie> Q;

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 add(int nod)
{
    muchie m;
    for (vector<pair<int, int> >::iterator it = V[nod].begin(); it != V[nod].end(); ++it)
    {
        if (!D[it->first])
        {
            m.nod = it->first;
            m.d = max(D[nod], it->second);
            Q.push(m);
        }
    }
}

void djk(int nod)
{
    D[nod] = 0;
    add(nod);

    muchie m;
    while (!Q.empty())
    {
        m = Q.top();
        Q.pop();

        if (D[m.nod])
            continue;

        D[m.nod] = m.d;
        if (m.nod == y)
            break;
        add(m.nod);
    }
}

int main()
{
    parsare1();
    for (int i = 1; i <= M; ++i)
    {
        parsare2();
        V[nod1].push_back(make_pair(nod2, cost));
    }

    djk(x);

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

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