Cod sursa(job #1577054)

Utilizator TopiAlexTopala Alexandru TopiAlex Data 23 ianuarie 2016 10:56:58
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <stdio.h>
#include <queue>
#define N_MAX 30002
#define M_MAX 100026

using namespace std;

struct Pair {
    int nod;
    int cost;
};

int n, m;
int X, Y;
int lst[N_MAX];
Pair vf[M_MAX * 2];
int urm[M_MAX * 2];
bool viz[N_MAX];
int d[N_MAX];
bool found = false;

inline void citire();
void BFS(int);

int main()
{
    citire();

    BFS(X);

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

    printf("%d\n", d[Y]);
    return 0;
}

inline void citire(){
    freopen("sate.in", "r", stdin);
    freopen("sate.out", "w", stdout);

    scanf("%d%d%d%d", &n, &m, &X, &Y);
    int counter = 0;
    int x, y, c;

    for (int i = 1; i <= m; ++i){
        scanf("%d%d%d", &x, &y, &c);
        counter++;
        vf[counter].nod = y;
        vf[counter].cost = c;
        urm[counter] = lst[x];
        lst[x] = counter;

        counter++;
        vf[counter].nod = x;
        vf[counter].cost = c;
        urm[counter] = lst[y];
        lst[y] = counter;
    }
}

void BFS(int nod){
    queue<int> coada;
    int poz;
    int y, c;

    coada.push(nod);
    viz[nod] = true;

    while (!coada.empty()){
        nod = coada.front();
        poz = lst[nod];

        while (poz != 0){
            y = vf[poz].nod;
            c = vf[poz].cost;
            if (!viz[y]){
                viz[y] = true;
                coada.push(y);
                if (nod < y)
                    d[y] = d[nod] + c;
                else
                    d[y] = d[nod] - c;
            }
            poz = urm[poz];
        }
        coada.pop();
    }
}