Cod sursa(job #3221668)

Utilizator Alex18maiAlex Enache Alex18mai Data 7 aprilie 2024 19:41:28
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

// ifstream cin("input.txt"); ofstream cout("output.txt");
ifstream cin("sate.in"); ofstream cout("sate.out");

int main() {

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

    vector<vector<pair<int,int>>> gr(n+1);

    int a, b, d;
    while (m--){
        cin>>a>>b>>d;
        gr[a].push_back({b, d});
        gr[b].push_back({a, -d});
    }

    vector<bool> used(n+1, false);
    queue<pair<int,int>> q;
    q.push({x, 0});
    used[x] = true;

    while (!q.empty()){
        pair<int,int> now = q.front();
        q.pop();

        if (now.first == y){
            cout<<now.second<<'\n';
            break;
        }

        for (auto &x : gr[now.first]){
            if (!used[x.first]){
                used[x.first] = true;
                q.push({x.first, now.second + x.second});
            }
        }
    }

    return 0;
}