Cod sursa(job #1279490)

Utilizator Corina1997Todoran Ana-Corina Corina1997 Data 30 noiembrie 2014 14:25:15
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <fstream>
#include <vector>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;

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

vector<vector<pair<int, int> > > G;
vector<int> d;
vector<bool> S;
queue<int> Q;
int n, m, a, b, x, y, cost;

void BFS( int k );

int main()
{
    is >> n >> m >> a >> b;
    G.resize(n+1);
    d = vector<int>(n+1, 0);
    S = vector<bool>(n+1, false);
    for ( int i = 1; i <= m; i++ )
    {
        is >> x >> y >> cost;
        G[x].push_back(make_pair(y, cost));
        G[y].push_back(make_pair(x, cost));
    }
    BFS( a );
    os << d[b];

    is.close();
    os.close();
    return 0;
}

void BFS( int k )
{
    S[k] = true;
    d[k] = 0;
    Q.push(k);
    int aux;
    while( !Q.empty() )
    {
        aux = Q.front();
        Q.pop();
        for ( const auto& y : G[aux] )
            if ( S[y.first] == false )
            {
                if ( y.first < aux )
                    d[y.first] = d[aux] - y.second;
                if ( y.first > aux )
                    d[y.first] = d[aux] + y.second;
                S[y.first] = true;
                Q.push(y.first);
            }
        if ( d[b] != 0 ) break;
    }
}