Cod sursa(job #97203)

Utilizator DastasIonescu Vlad Dastas Data 5 noiembrie 2007 19:44:01
Problema PScNv Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.37 kb
#include <cstdio>
#include <memory>

const int maxn = 250001;
const int maxm = 500001;

FILE *in = fopen("pscnv.in","r"), *out = fopen("pscnv.out","w");

struct graf
{
    int nod;
    short cost;
};

int n, m, x, y;

int maxc = -1;

graf *a[maxn];

int X[maxm], Y[maxm], C[maxm];
int grad[maxn];
int nr[maxn];

void read()
{
    fscanf(in, "%d %d %d %d\n", &n, &m, &x, &y);

    int p, q;
    short c;

    char buf[1024];
    for ( int i = 1; i <= m; ++i )
    {
        fgets(buf, 1024, in);

        p = q = 0;
        c = 0;
        int j = 0;
        while ( buf[j] != ' ' )
            p = p * 10 + (buf[j] - '0'), ++j;
        ++j;
        while ( buf[j] != ' ' )
            q = q * 10 + (buf[j] - '0'), ++j;
        ++j;
        while ( buf[j] >= '0' && buf[j] <= '9' )
            c = c * 10 + (buf[j] - '0'), ++j;

        if ( c > maxc )
            maxc = c;

        if ( p != q )
            X[i] = p, Y[i] = q, C[i] = c, ++grad[p];
    }

    for ( int i = 1; i <= n; ++i )
        a[i] = (graf *)malloc(sizeof(graf)*grad[i] + 4);

    for ( int i = 1; i <= m; ++i )
        a[ X[i] ][ ++nr[ X[i] ] ].nod = Y[i], a[ X[i] ][ nr[ X[i] ] ].cost = C[i];
}

struct tail
{
    int nod;
    tail *next;
};

int nrr = 0;
char viz[maxn];

int check(int k)
{
    tail *p = new tail;
    tail *u = new tail;


    p->next = NULL;
    p->nod = x;

    u = p;

    while ( p )
    {
        int q = p->nod;

        if ( grad[q] )
            for ( int i = 1, N = nr[ q ]; i <= N; ++i )
                if ( viz[a[q][i].nod] != nrr && a[q][i].cost <= k )
                {
                    tail *t = new tail;

                    t->next = NULL;
                    t->nod = a[q][i].nod;
                    u->next = t;
                    u = t;

                    if ( a[q][i].nod == y )
                        return 1;

                    viz[a[q][i].nod] = nrr;
                }

        tail *t = p;
        p = p->next;

        delete t;
    }

    return 0;
}

int main()
{
    read();

    int st = 1, dr = maxc;

    int m = 0;
    int t = 0;

    while ( st < dr )
    {
        m = (st + dr) >> 1;
        ++nrr;

        t = check(m);
        if ( t )
            dr = m;
        else
            st = m+1;
    }

    fprintf(out, "%d\n", st);

	return 0;
}