Cod sursa(job #536860)

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

using namespace std;

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

char parse[2000000], *now;
int N, M, X, Y, K, Kmax;
vector<pair<int, int> > V[250002];
bool S[250002];
int testnow;

void Dfs(int A)
{
    S[A] = true;
    for (vector<pair<int, int> >::iterator it = V[A].begin(); it != V[A].end() && it->second <= testnow; ++it)
        if (!S[it->first])
            Dfs(it->first);
}
bool Test(int value)
{
    testnow = value;
    memset(S, 0, sizeof(S));
    Dfs(X);
    return S[Y];
}

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));
        Kmax = max(Kmax, cost);
    }

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

    int step;
    for (step = 1; (step << 1) <= Kmax; step <<= 1);
    for (K = Kmax + 1; step; step >>= 1)
        if (K - step >= 1 && Test(K - step))
            K -= step;

    fout << K;

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