Cod sursa(job #97310)

Utilizator DastasIonescu Vlad Dastas Data 6 noiembrie 2007 14:20:43
Problema PScNv Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <cstdio>
#include <memory>
#include <vector>

const int maxn = 250001;
const int maxk = 1001;

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];

std::vector<int> L[maxk];
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 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);

        d[p] = d[q] = 1<<20;
    }
}

int main()
{
    read();

    L[0].push_back(x);
    d[x] = 0;

    for ( int i = 0; i <= 1000; ++i )
        for ( int j = 0; j != L[i].size(); ++j )
            if ( i == d[ L[i][j] ] )
            {
                graf *q = a[ L[i][j] ];

                while ( q )
                {
                    if ( q->cost < d[ q->nod ] )
                        L[q->cost].push_back(q->nod), d[ q->nod ] = q->cost;

                    q = q->next;
                }
            }

    fprintf(out, "%d\n", d[y]);

	return 0;
}