Cod sursa(job #3374537)

Utilizator MihaiDBRDobrin Mihai MihaiDBR Data 9 noiembrie 2026 01:54:02
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

int n, m, x, y;
vector<pair<int, int>> v[30005];
int poz[30005];
bool vizitat[30005];

int main() {
    fin >> n >> m >> x >> y;
    for (int i = 0; i < m; i++) {
        int a, b, d;
        fin >> a >> b >> d;
        v[a].push_back({b, d});
        v[b].push_back({a, -d});
    }
    queue<int> q;
    vizitat[x] = true;
    q.push(x);
    while (!q.empty() && !vizitat[y]) {
        int a = q.front();
        q.pop();
        for (pair<int, int> p : v[a]) {
            int b = p.first;
            if (!vizitat[b]) {
                vizitat[b] = true;
                poz[b] = poz[a] + p.second;
                q.push(b);
            }
        }
    }
    if (poz[y] < 0)
        fout << -poz[y] << '\n';
    else
        fout << poz[y] << '\n';
    return 0;
}