Cod sursa(job #536865)

Utilizator darrenRares Buhai darren Data 19 februarie 2011 16:23:18
Problema PScNv Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <cstring>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

inline bool compare(const pair<int, int>& P1, const pair<int, int>& P2)
{
    return P1.second < P2.second;
}

char parse[5000000], *now;
int N, M, X, Y, K, Kmax;
vector<pair<int, int> > V[250002];
int minC[250002];
bool inQueue[250002];
queue<int> Q;

int get()
{
    int number = 0;
    while (!('0' <= *now && *now <= '9')) ++now;
    while ('0' <= *now && *now <= '9')
        number *= 10, number += *now - '0', ++now;
    return number;
}

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

    fin.getline(parse, sizeof(parse), '\0');
    now = parse;

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

    for (int i = 1; i <= N; ++i)
        sort(V[i].begin(), V[i].end(), compare);
    memset(minC, -1, sizeof(minC));

    minC[X] = 0;
    inQueue[X] = true;
    Q.push(X);

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

        for (vector<pair<int, int> >::iterator it = V[i].begin(); it != V[i].end(); ++it)
            if (max(minC[i], it->second) < minC[it->first] || minC[it->first] == -1)
            {
                minC[it->first] = max(minC[i], it->second);
                if (!inQueue[it->first])
                {
                    Q.push(it->first);
                    inQueue[it->first] = true;
                }
            }
    }

    fout << minC[Y];

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