Cod sursa(job #130812)

Utilizator DastasIonescu Vlad Dastas Data 1 februarie 2008 22:50:11
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <fstream>

using namespace std;

const int maxn = 250001;
const int maxk = 1002;

ifstream in("pscnv.in");
ofstream out("pscnv.out");

struct graf
{
    int nod;
    short cost;
    graf *next;
};

struct list
{
    int val;
    list *next;
};

int n, m, x, y;

int maxc = -1;

graf *a[maxn];
list *Ls[maxk] = {NULL};
list *Le[maxk] = {NULL};
int d[maxn];

void add(int to, int nod, short cost)
{
    graf *q = new graf;

    q->next = a[to];
    q->nod = nod;
    q->cost = cost;
    a[to] = q;
}

void addl(int where, int what)
{
    if ( Ls[where] == NULL )
    {
        list *q = new list;
        q->val = what;
        q->next = NULL;

        Ls[where] = q;
        Le[where] = q;

        return;
    }

    list *q = new list;
    q->val = what;
    q->next = NULL;
    Le[where]->next = q;
    Le[where] = q;
}

void read()
{
    in >> n >> m >> x >> y;

    int p, q;
    short c;

    for ( int i = 1; i <= m; ++i )
    {
        in >> p >> q >> c;

        if ( p != q )
            add(p, q, c);
    }
}

void go()
{
    for ( int i = 1; i <= n; ++i )
        d[i] = 1 << 30;

    d[x] = 0;
    addl(0, x);

    for ( int i = 0; ; )
    {
        if ( !Ls[i] )
        {
            ++i;
            continue;
        }

        int p = Ls[i]->val;

        if ( p == y )
            return;

        if ( d[p] < i )
        {
            Ls[i] = Ls[i]->next;
            continue;
        }

        for ( ; a[p] ; a[p] = a[p]->next )
        {
            int mm = i > a[p]->cost ? i : a[p]->cost;

            if ( mm < d[ a[p]->nod ] )
            {
                d[ a[p]->nod ] = mm;
                addl(mm, a[p]->nod);
            }
        }

        Ls[i] = Ls[i]->next;
    }
}

int main()
{
    read();
    go();


    out << d[y] << endl;

	return 0;
}