Cod sursa(job #3374182)

Utilizator lea_Lea Ales lea_ Data 8 noiembrie 2026 20:02:01
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>
#include <vector>

using namespace std;

vector<pair<int, int>> adj[30005];
long long poz[30005];
bool vizitat[30005];

long long dist(int x, int y){
    vector<int> q;
    q.push_back(x);
    vizitat[x]=true;
    poz[x]=0;

    for(size_t i=0; i<q.size(); i++){
        int v=q[i];
        for(auto e: adj[v]){
            int u=e.first;
            if(!vizitat[u]){
                vizitat[u]=true;
                poz[u]=poz[v]+e.second;
                q.push_back(u);
            }
        }
    }

    long long d=poz[y]-poz[x];
    if (d<0) return -d;
    return d;
}

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

    int n, m, x, y;
    fin >> n >> m >> x >> y;

    for(int i=0; i<m; i++){
        int a,b,c;
        fin>>a>>b>>c;
        adj[a].push_back({b,c});
        adj[b].push_back({a,-c});
    }

    fout<<dist(x,y);

    return 0;
}