Cod sursa(job #568776)

Utilizator chibicitiberiuChibici Tiberiu chibicitiberiu Data 31 martie 2011 17:53:27
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#define DEBUG 0

#if DEBUG==0
#define LIMIT 30005
#else
#define LIMIT 100
#endif

#include<fstream>
#include<vector>
#include<queue>
using namespace std;

class A
{
public:
    short Node;
    int Distance;

    A() { Node = Distance = 0; }
    A(int n, int d) { Node = n; Distance = d; }
};


vector<A> graf[LIMIT];
bool Vizited[LIMIT];
int NrVf, NrM;
queue<A> S;

int BFS(int root, int dest)
{
    S.push(A(root,0));
    Vizited[root] = true;

    while (!S.empty())
    {
        if (S.front().Node == dest) return S.front().Distance;

        for (unsigned i=0; i < graf[S.front().Node].size(); i++)
            if (!Vizited[graf[S.front().Node][i].Node])
            {
                S.push(A (graf[S.front().Node][i].Node,
                          S.front().Distance + graf[S.front().Node][i].Distance
                         ));
                Vizited[graf[S.front().Node][i].Node] = true;
            }
        
        S.pop();
    }

    return -1;
}

int main()
{
    ifstream in ("sate.in");
    ofstream out ("sate.out");

    int from, to, NrVf, NrM;
    in>>NrVf>>NrM>>from>>to;

    for (int i=0; i < NrM; i++)
    {
        int x,y,dist;
        in>>x>>y>>dist;

        graf[x].push_back(A(y, dist));
        graf[y].push_back(A(x, -dist));
    }

    out<<BFS(from, to);
    
    in.close();
    out.close();

    return 0;
}