Cod sursa(job #1194431)

Utilizator pop_bogdanBogdan Pop pop_bogdan Data 3 iunie 2014 21:37:38
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

ifstream is("sate.in");
ofstream os("sate.out");

#define DIM 30001

int N, M, X, Y, x, y, c;
bool Vis[DIM];
int R[DIM];
vector <pair<int,int> > V[DIM];
queue <int> Q;

int main()
{
    is >> N >> M >> X >> Y;
    for ( int i = 1; i <= M; ++i )
    {
        is >> x >> y >> c;
        V[x].push_back(make_pair(y,c));
        V[y].push_back(make_pair(x,c*(-1)));
    }

    Q.push(X); int nod; Vis[X] = true;

    while ( !Q.empty() )
    {
        nod = Q.front();
        Q.pop();
        for ( int i = 0; i < V[nod].size(); ++i )
        {
            if ( Vis[V[nod][i].first] == false )
            {
                R[V[nod][i].first] = R[nod] + V[nod][i].second;
                Q.push(V[nod][i].first);
                Vis[V[nod][i].first] = true;
                if ( V[nod][i].first == Y )
                {
                    os << R[V[nod][i].first];
                    return 0;
                }

            }
        }
    }
    return 0;
}