Cod sursa(job #3340638)

Utilizator RuxandraPro12_Metehau Ruxandra Maria RuxandraPro12_ Data 15 februarie 2026 15:24:10
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N_MAX = 30005, M_MAX = 100025;

int n, m, x, y, v[N_MAX], a, b, d;
vector<pair<int, int>> g[N_MAX];

void bfs (int nod) {
    queue<int> q;
    q.push(nod);
    while (!q.empty()) {
        int nod = q.front();
        q.pop();
        for (auto [fiu, d] : g[nod]) {
            if (v[fiu] == 0) {
                q.push(fiu);
                v[fiu] = v[nod] + d;
                ///fout << v[fiu] << " " << fiu << "\n";
            }
        }
    }
}

int main() {
    fin >> n >> m >> x >> y;
    for (int i = 1; i <= m; i++) {
        fin >> a >> b >> d;
        g[a].push_back({b, d});
        g[b].push_back({a, -d});
    }
    bfs(x);
    fout << v[y] << "\n";
    return 0;
}