Cod sursa(job #97168)

Utilizator DastasIonescu Vlad Dastas Data 5 noiembrie 2007 18:31:04
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.07 kb
#include <cstdio>

const int maxn = 250001;

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

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

int n, m, x, y;

int maxc = -1;

graf *a[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 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 )
            add(p, q, c);
    }
}

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

int nr = 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 )
    {
        graf *q = a[ p->nod ];

        while ( q )
        {
            if ( viz[q->nod] != nr && q->cost <= k )
            {
                tail *t = new tail;

                t->next = NULL;
                t->nod = q->nod;
                u->next = t;
                u = t;

                if ( u->nod == y )
                    return 1;

                viz[u->nod] = nr;
            }
            q = q->next;
        }
        tail *t = p;
        p = p->next;

        delete t;
    }

    return 0;
}

int main()
{
    read();

    int st = 1, dr = maxc;

    int m = 0;

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

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

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

	return 0;
}