Cod sursa(job #1571348)

Utilizator Nevermore10Macovei Cosmin Nevermore10 Data 17 ianuarie 2016 23:18:02
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <cstdio>
using namespace std;

const int dmax = 30000;
const int dim = 100024;

int lst[dmax + 1];
int urm[2 * dim + 1];
int vf[2 * dim + 1];
int cost[2 * dim + 1];
int nr;

int C[dmax + 1];
bool viz[dmax + 1];

int sol[dmax + 1];

int N, M, X, Y;

void BFS(int x)
{
    int i, y, p, u, poz, COST;

    p = u = 1;

    //INSEREZ IN COADA NODUL x
    C[1] = X;
    viz[X] = true;

    while(p <= u)
    {
        x = C[p];

        //PARCURG LISTA VECINILOR y AI LUI x
        poz = lst[x];

        while(poz)
        {
            y = vf[poz];
            COST = cost[poz];

            if(!viz[y])
            {
                u++;
                C[u] = y;
                viz[y] = true;

                if(x < y) sol[y] = sol[x] + COST;
                if(x > y) sol[y] = sol[x] - COST;
            }

            poz = urm[poz];
        }

        p++;
    }
}

void adauga(int x, int y, int c)
{
    //ADAUG IN LISTA LUI x PE y
    nr++;
    vf[nr] = y;
    urm[nr] = lst[x];
    cost[nr] = c;
    lst[x] = nr;
}

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

    int i, x, y, cost;

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

    for(i = 1; i <= M; i++)
    {
        scanf("%d %d %d", &x, &y, &cost);

        adauga(x,y,cost);
        adauga(y,x,cost);
    }

    BFS(X);

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

    return 0;
}