Cod sursa(job #3352511)

Utilizator slol003Rizea Alexandru-Gabriel slol003 Data 28 aprilie 2026 12:03:55
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin ("sate.in");
ofstream fout ("sate.out");
#define cin fin
#define cout fout
const int NMAX = 3e4 + 5;
vector<vector<pair<int, int>>> adj(NMAX);
bool viz[NMAX];
int n, m, x, y;
int pos[NMAX];

void bfs(){
    queue<int> q;
    q.push(x);
    viz[x] = true;
    while (!q.empty()){
        int dx = q.front();
        q.pop();
        for (auto i : adj[dx]){
            if (viz[i.first] == false){
                pos[i.first] = pos[dx] + i.second;
                viz[i.first] = true;
                q.push(i.first);
            }
            if (i.first == y){
                cout << pos[i.first];
                return;
            }
        }
    }
}

int main(){
    cin >> n >> m >> x >> y;
    for (int i = 1, a, b, c; i <= m; ++ i){
        cin >> a >> b >> c;
        adj[a].push_back({b, c});
        adj[b].push_back({a, -c});
    }
    bfs();

    return 0;
}