Cod sursa(job #3042250)

Utilizator Razvan48Capatina Razvan Nicolae Razvan48 Data 5 aprilie 2023 00:00:20
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <queue>

using namespace std;

const int NMAX = 30000;

vector<pair<int, int>> graf[1 + NMAX];

int sol[1 + NMAX];

queue<int> q;

bool vizitat[1 + NMAX];

int main()
{
    ifstream in("sate.in");
    ofstream out("sate.out");

    ios_base::sync_with_stdio(false);
    in.tie(nullptr);

    int n, m, x, y;
    in >> n >> m >> x >> y;

    for (int i = 1; i <= m; i++)
    {
        int a, b, d;
        in >> a >> b >> d;

        if (a > b)
            swap(a, b);

        graf[a].emplace_back(b, d);
        graf[b].emplace_back(a, -d);
    }

    if (x > y)
        swap(x, y);

    q.emplace(x);

    while (!q.empty())
    {
        int nod = q.front();
        q.pop();

        vizitat[nod] = true;

        for (int i = 0; i < graf[nod].size(); i++)
        {
            if (!vizitat[graf[nod][i].first])
            {
                sol[graf[nod][i].first] = sol[nod] + graf[nod][i].second;
                q.emplace(graf[nod][i].first);
            }
        }
    }

    out << sol[y] << '\n';

    in.close();
    out.close();

    return 0;
}