Cod sursa(job #593120)

Utilizator darrenRares Buhai darren Data 1 iunie 2011 13:52:00
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.24 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];
int minC[250002], H[250002], where[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 Push(int x)
{
    int y = x >> 1;
    while (y >= 1)
        if (minC[H[x]] < minC[H[y]])
        {
            swap(H[x], H[y]);
            swap(where[H[x]], where[H[y]]);
            x >>= 1, y >>= 1;
        }
        else break;
}
void Pop()
{
    H[1] = H[H[0]--];
    int x = 1, y = 2;

    while (y <= H[0])
    {
        if (y < H[0] && minC[H[y + 1]] < minC[H[y]]) ++y;
        if (minC[H[y]] < minC[H[x]])
        {
            swap(H[x], H[y]);
            swap(where[H[x]], where[H[y]]);
            x = y, y <<= 1;
        }
        else break;
    }
}

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)
        minC[i] = INF;
    minC[X] = 0;
    for (VP::iterator it = V[X].begin(); it != V[X].end(); ++it)
        minC[it->first] = it->second;

    for (int i = 1; i <= N; ++i)
        if (i != X)
        {
            H[++H[0]] = i;
            where[i] = H[0];
            Push(H[0]);
        }

    for (int step = 1; step < N; ++step)
    {
        int now = H[1];
        Pop();

        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);
                Push(where[it->first]);
            }
    }

    fout << minC[Y];

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