Cod sursa(job #3372893)

Utilizator G_DenisaGroza Denisa-Georgiana G_Denisa Data 5 noiembrie 2026 12:50:49
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <queue>

//BFS with a structure for cost
//O(m+n)
using namespace std;

struct Muchie {
    int to;
    long long dist;
};

int main() {
    ifstream f("sate.in");

    int N, M, X, Y;
    f >> N >> M >> X >> Y;

    vector<vector<Muchie>> matrice(N + 1);

    for (int k = 0; k < M; ++k) {
        int i, j;
        long long D;
        f >> i >> j >> D;
        matrice[i].push_back({j, D});
        matrice[j].push_back({i, -D});
    }
    f.close();

    vector<long long> pos(N + 1, 0);
    vector<bool> vizitat(N + 1, false);

    queue<int> q;
    q.push(X);
    vizitat[X] = true;
    pos[X] = 0;

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

        if (u == Y) break;

        for (const auto& muchie : matrice[u]) {
            if (!vizitat[muchie.to]) {
                vizitat[muchie.to] = true;
                pos[muchie.to] = pos[u] + muchie.dist;
                q.push(muchie.to);
            }
        }
    }

    ofstream g("sate.out");
    g << abs(pos[Y]) << "\n";
    g.close();

    return 0;
}