Cod sursa(job #2400363)

Utilizator petru.ciocirlanPetru Ciocirlan petru.ciocirlan Data 8 aprilie 2019 17:46:52
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;

#define FILE_NAME "sate"
ifstream in (FILE_NAME".in");
ofstream out(FILE_NAME".out");

constexpr int MAX_NODES = 30000 + 4;
vector < pair < int, int > > G[MAX_NODES];
bitset < MAX_NODES > Viz;
int N, M, X, Y;

queue < pair < int, int > > Q;
int Lee()
{
    Q.push(make_pair(X, 0));
    Viz[X] = true;
    while(!Q.empty())
    {
        int nod = Q.front().first;
        int dist = Q.front().second;
        Q.pop();

        if(nod == Y)
        {
            if(dist < 0)
                return -dist;
            return dist;
        }

        for(auto next : G[nod])
            if(!Viz[next.first])
            {
                Viz[next.first] = true;
                Q.push(make_pair(next.first, next.second + dist));
            }
    }
    return 0;
}

int main()
{
    in >> N >> M >> X >> Y;
    while(M--)
    {
        int a, b, c;
        in >> a >> b >> c;
        if(a > b) swap(a, b);
        G[a].push_back(make_pair(b, c));
        G[b].push_back(make_pair(a, -c));
    }

    out << Lee();

    return 0;
}