Cod sursa(job #3374488)

Utilizator TintsooTint Sabei Soe Win Tintsoo Data 9 noiembrie 2026 01:18:25
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue> 

using namespace std; 

int n, m, X, Y;
vector<pair<int, int>> adj[30005]; 
int pos[30005];                    
bool visited[30005];
void bfs(int s){

    queue<int> q;
    q.push(s);
    visited[s] = true;
    pos[s] = 0;

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

        for (auto e : adj[u])
        {
            int v = e.first;
            int w = e.second;

            if (!visited[v])
            {
                visited[v] = true;
                pos[v] = pos[u] + w; 
                q.push(v);
            }
        }
    }
}

int main(){

    ifstream fin("sate.in");
    ofstream fout("sate.out");
    
    fin >> n >> m >> X >> Y;

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

    bfs(X);
    fout << abs(pos[Y]) << endl; 

    return 0;
}