Cod sursa(job #3373988)

Utilizator AbRobertAbabei Robert-Petronel AbRobert Data 8 noiembrie 2026 13:46:44
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;

std::ifstream fin("sate.in");
std::ofstream fout("sate.out");

int n, m, x, y;
std::vector<pair<int, int>> L[30005];
long long d[30005];
std::queue<int> q;
std::bitset<30005> viz;

// algoritmul standard de BFS pentru determinarea distantelor minime
void BFS(int k)
{
    for (int i = 0; i <= n; i++)
        d[i] = 1e15;
    d[k] = 0;
    q.push(k);
    while (!q.empty())
    {
        k = q.front();
        q.pop();
        viz[k] = 1;
        for (auto e : L[k])
            if (!viz[e.first])
            {
                q.push(e.first);
                d[e.first] = d[k] + e.second;
            }
            else d[e.first] = std::min(d[e.first], d[k] + e.second);
    }
}

int main()
{
    int i, a, b, c;
    fin >> n >> m >> x >> y;
    for (i = 1; i <= m; i++)
    {
        fin >> a >> b >> c;
        L[a].push_back({b, c});
        L[b].push_back({a, -c});
    }
    // BFS din nodul x:
    BFS(x);
    fout << d[y];
    return 0;
}