Cod sursa(job #3150244)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 15 septembrie 2023 18:03:32
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

const int NMAX = 3e4;

int n, m, x, y;
vector<pair<int, int>> G[NMAX + 1];
int D[NMAX + 1];

void BFS(int start)
{
    queue<int> Q;
    D[start] = 1;
    Q.push(start);
    while(!Q.empty())
    {
        int node = Q.front();
        Q.pop();

        for(auto next_node : G[node])
            if(D[next_node.first] == 0)
            {
                D[next_node.first] = D[node] + next_node.second;
                Q.push({next_node.first});
            }
    }
}

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

    BFS(x);
    cout << D[y] - 1;


    return 0;
}