Cod sursa(job #2400381)

Utilizator petru.ciocirlanPetru Ciocirlan petru.ciocirlan Data 8 aprilie 2019 18:03:27
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <vector>
#include <bitset>
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 Dist[MAX_NODES];
int N, M, X, Y;

int Q[MAX_NODES];
int Lee()
{
    int p, u;
    p = u = 0;
    Viz[X] = true;
    Q[p] = X;
    while(p <= u)
    {
        int nod = Q[p];
        int dist = Dist[nod];
        ++p;

        for(auto next : G[nod])
            if(!Viz[next.first])
            {
                Viz[next.first] = true;
                Dist[next.first] = dist + next.second;
                Q[++u] = next.first;
            }
    }
    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));
    }

    Lee();
    out << abs(Dist[Y]);

    return 0;
}