Cod sursa(job #593122)

Utilizator darrenRares Buhai darren Data 1 iunie 2011 14:01:49
Problema PScNv Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.91 kb
#include <cstring>
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

typedef vector<pair<int, int> > VP;
const int INF = 0x3f3f3f3f;

//char parse[10000000], *now;
int N, M, X, Y;
vector<pair<int, int> > V[250002];
vector<int> L[1002];
int minC[250002], where[250002];
bool S[250002];

/*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();
    fin >> N >> M >> X >> Y;

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

    for (int i = 1; i <= N; ++i)
        minC[i] = INF;
    minC[X] = 0;
    for (VP::iterator it = V[X].begin(); it != V[X].end(); ++it)
    {
        minC[it->first] = it->second;
        L[minC[it->first]].push_back(it->first);
    }

    int pos = 1;
    for (int step = 1; step < N; ++step)
    {
        int now = 0;
        do
        {
            while (L[pos].empty()) ++pos;

            now = L[pos].back();
            L[pos].pop_back();

            if (S[now]) now = 0;
        } while (now == 0);

        S[now] = true;

        for (VP::iterator it = V[now].begin(); it != V[now].end(); ++it)
            if (max(minC[now], it->second) < minC[it->first])
            {
                minC[it->first] = max(minC[now], it->second);
                L[minC[it->first]].push_back(it->first);
            }
    }

    fout << minC[Y];

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