Cod sursa(job #568764)

Utilizator chibicitiberiuChibici Tiberiu chibicitiberiu Data 31 martie 2011 17:46:56
Problema Sate Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 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:
    int 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 DFS(int root, int dest)
{
    S.push(A(root,0));

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

        for (int 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
                         ));
        
        S.pop();
    }
}

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<<DFS(from, to);
    
    in.close();
    out.close();
    return 0;
}