Cod sursa(job #1401485)

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

char parse[100], *now;
inline void verify()
{
    if (*now == 0)
    {
        fin.get(parse, sizeof(parse), '\0');
        now = parse;
    }
}

int get()
{
    while (*now != '-' && (*now < '0' || *now > '9'))
    {
        ++now;
        verify();
    }

    bool neg = false;
    if (*now == '-')
    {
        neg = true;
        ++now;
        verify();
    }

    int num = 0;
    while (*now >= '0' && *now <= '9')
    {
        num = num * 10 + (*now - '0');
        ++now;
        verify();
    }

    if (neg) num *= -1;

    return num;
}

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()
{
    now = parse;
    verify();

    N = get();
    M = get();
    x = get();
    y = get();
    for (int i = 1, nod1, nod2, cost; i <= M; ++i)
    {
        nod1 = get();
        nod2 = get();
        cost = get();
        V[nod1].push_back(make_pair(nod2, cost));
    }

    djk(x);

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

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