Cod sursa(job #1204618)

Utilizator Cristian1997Vintur Cristian Cristian1997 Data 3 iulie 2014 14:46:11
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
using namespace std;
#include <fstream>
#include <queue>
ifstream fin("sate.in");
ofstream fout("sate.out");
const int Nmax = 30000;

struct lista {int first, second; lista* urm;} ;

int d[Nmax+1];
lista* v[Nmax+1];
queue<int> Q;

int mod2(int a) {return a > 0 ? a : (-a);}
void push1(int, int, int) ;

int main()
{
    int i, m, n, a, b, c, x, y, vf;
    lista *nod;
    fin >> n >> m >> x >> y;
    for(; m; --m)
    {
        fin >> a >> b >> c;
        push1(a, b, c);
        push1(b, a, c);
    }
    d[x] = 0;
    for(Q.push(x); !Q.empty(); Q.pop())
    {
        vf = Q.front();
        for(nod = v[vf]; nod != NULL; nod = nod->urm)
            if((nod->first) != x && d[nod->first] == 0)
            {
                if(vf < nod->first) d[nod->first] = d[vf] + (nod->second);
                else d[nod->first] = d[vf] - (nod->second);
                Q.push(nod->first);
                if((nod->first) == y) {fout << mod2(d[y]) << '\n'; return 0;}
            }
    }
    return 0;
}

void push1(int a, int b, int c)
{
    lista *nou, *nod;
    nou = new lista; nou->first = b; nou->second = c; nou->urm = NULL;
    if(v[a] == NULL) {v[a] = nou; return;}
    for(nod = v[a]; nod->urm != NULL; nod = nod->urm) ;
    nod->urm = nou;
}