Cod sursa(job #2965775)

Utilizator Theodor17Pirnog Theodor Ioan Theodor17 Data 16 ianuarie 2023 08:52:39
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

ifstream cin("sate.in");
ofstream cout("sate.out");

const int NMAX = 1e5;
const int inf = 1e9;

queue <int> q;
vector <pair <int, int>> g[NMAX + 1];
int d[NMAX + 1];

void bfs(){

    while(!q.empty()){

        int x = q.front();
        q.pop();

        for(auto y : g[x]){

            if(d[y.first] > d[x] + y.second){

                d[y.first] = y.second + d[x];
                q.push(y.first);

            }
        }

    }

}

int main(){

    ios :: sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n = 0, m = 0, x1 = 0, x2 = 0;
    cin >> n >> m >> x1 >> x2;

    for(int i = 0; i < m; i++){

        int x = 0, y = 0, d = 0;
        cin >> x >> y >> d;

        g[x].push_back({y, d});
        g[y].push_back({x, -d});

    }

    for(int i = 1; i <= n; i++)
        d[i] = inf;
    
    d[x1] = 0;
    q.push(x1);

    bfs();

    cout << d[x2];

}