Cod sursa(job #1576623)

Utilizator Vertex10Alexandru Pokharel Vertex10 Data 22 ianuarie 2016 17:46:09
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <fstream>
#include <stdlib.h>

using namespace std;
ifstream f ("sate.in");
ofstream g ("sate.out");

int m, n, x, y, vf[200050], lst[200050], urm[200050], cost[200050], nr;
bool viz[30001];

void adauga (int a, int b, int c) //adaug pe a in lista lui b
{
    ++nr;
    vf[nr]=b;
    urm[nr] = lst[a];
    cost[nr] = c;
    lst[a]=nr;
}

void DFS (int nod_curent, bool &ok, int lung)
{
    viz[nod_curent] = 1;
    if (nod_curent==y)
    {
        ok=1;
        return;
    }

    int p = lst[nod_curent];
    while(p)
    {
        int j = vf[p];
        if (!viz[j])
        {
            if (j > nod_curent)
                lung += cost[p];
            else
                lung -= cost[p];

            DFS(j, ok, lung);

            //g << lung;
            if (ok) //daca am ajuns la y
            {
                g << lung;
                exit(0);
            }
        }
        p=urm[p];
    }
}

int main()
{

    f >> n >> m >> x >> y;
    int a, b, cost;
    bool ok=0;
    for (int i=1; i<=m; ++i)
    {
        f >> a >> b >> cost;
        adauga (a, b, cost);
        adauga (b, a, cost);
    }

    DFS(x, ok, 0);

    return 0;
}