Cod sursa(job #638603)

Utilizator vlad2901Vlad Berindei vlad2901 Data 21 noiembrie 2011 01:27:31
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>

#define NMAX 30000

using namespace std;

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

vector<lista> a(NMAX);
lista tata;
queue< pair<int, int> > c;
int viz[NMAX];
int N, M, X, Y;

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


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

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

    tata = lista(N);

    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.push(make_pair(X, 0));
    tata[X] = make_pair(0, 0);

    while(!c.empty())
    {
        curent = c.front();
        c.pop();
        viz[curent.first] = 1;

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

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

    dist = 0;
    curent = make_pair(Y, 0);

    while(tata[curent.first].first)
    {
        if(tata[curent.first].first < curent.first)
        {
            dist += tata[curent.first].second;
        }
        else
        {
            dist -= tata[curent.first].second;
        }
        curent = tata[curent.first];
    }

    if(dist < 0)
    {
        dist *= -1;
    }

    printf("%d", dist);

    return 0;
}