Cod sursa(job #3374124)

Utilizator TomikeToma Rares Tomike Data 8 noiembrie 2026 18:36:25
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 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>> g[30005];
int d[30005];
bool viz[30005];

int main(){
    fin >> n >> m >> x >> y;
    for(int k = 1; k <= m; k++){
        int i, j, c;
        fin >> i >> j >> c;
        g[i].push_back({j, c});//la dus pozitiv
        g[j].push_back({i, -c});//la intors negativ
    }

    queue<int> q;
    q.push(x);
    viz[x] = 1;
    while(!q.empty() && !viz[y]){
        int nod = q.front();
        q.pop();
        for(auto v : g[nod]){
            if(!viz[v.first]){
                viz[v.first] = 1;
                d[v.first] = d[nod] + v.second;
                q.push(v.first);
            }
        }
    }

    if(d[y] < 0) d[y] = -d[y];
    fout << d[y];
}