Cod sursa(job #644242)

Utilizator vlad2901Vlad Berindei vlad2901 Data 5 decembrie 2011 19:03:02
Problema Sate Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>

#define NMAX 30002

using namespace std;

typedef  vector< pair<int, int> >  lista;

lista a[NMAX];
pair<int, int> c[2 * NMAX];

int viz[NMAX], dist[NMAX];
int N, M, X, Y;

int main()
{
    pair<int, int>curent;
    int i, x, y, d, ok, yt, f;
    int front, end;
    lista::iterator it;


    freopen("sate.in", "r", stdin);
    freopen("sate.out", "w", stdout);

    scanf("%d %d %d %d", &N, &M, &X, &Y);

    for(i=0;i<M;++i)
    {
        scanf("%d %d %d", &x, &y, &d);
        a[x].push_back(make_pair(y, d));
        a[y].push_back(make_pair(x, d));
    }

    c[0] = make_pair(X, 0);
    viz[0] = 1;
    front = end = 0;
    while(front <= end)
    {
        curent = c[front++];
        //viz[curent.first] = 1;

        if(curent.first == Y)
        {
            break;
        }

        ok = 0;
        for(it=a[curent.first].begin();it!=a[curent.first].end();++it)
        {
            f = it->first;
            if(!viz[f])
            {
                viz[curent.first] = 1;
                ok = 1;
                c[++end] = *it;
                if(f > curent.first)
                    dist[f] = dist[curent.first] + it->second;
                else
                    dist[f] = dist[curent.first] - it->second;
            }
        }
    }

    if(dist[Y] < 0) dist[Y] *= -1;


    printf("%d", dist[Y]);

    return 0;
}