Cod sursa(job #3307360)

Utilizator Maan002Barbu Andrei Maan002 Data 20 august 2025 13:47:35
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>
using namespace std;

vector<pair<int,int>> G[30001];
long long sol[30001];
bool viz[30001];

void BFS(int start, int target) {
    queue<int> q;
    q.push(start);
    viz[start] = true;
    sol[start] = 0;

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

        for (auto [vecin, dist] : G[nod]) {
            if (!viz[vecin]) {
                viz[vecin] = true;
                sol[vecin] = sol[nod] + dist;
                if (vecin == target) return;
                q.push(vecin);
            }
        }
    }
}

int main() {
    ifstream cin("sate.in");
    ofstream cout("sate.out");
    int N, M, X, Y;
    cin >> N >> M >> X >> Y;

    for (int i = 0; i < M; i++) {
        int a, b, d;
        cin >> a >> b >> d;
        G[a].push_back({b, d});
        G[b].push_back({a, -d});
    }

    BFS(X, Y);

    cout << abs(sol[Y]) << "\n";
    return 0;
}