Cod sursa(job #3302952)

Utilizator brianabucur11Briana Bucur brianabucur11 Data 12 iulie 2025 12:30:10
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <bits/stdc++.h>

using namespace std;

const int nmax=3e4+5;

vector <pair<int,int>> v[nmax];

int n, m, x, y, d[nmax];

void dfs (int nod)
{
    for (auto it:v[nod])
    {
        if (!d[it.first])
        {
            d[it.first]=d[nod]+it.second;
            dfs(it.first);
        }
    }
}

signed main()
{
    cin >> n >> m >> x >> y;
    for (int i=1; i<=m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        if (b<a)
            swap(a,b);
        v[a].push_back({b,c});
        v[b].push_back({a,-c});
    }
    d[x]=1;
    dfs(x);
    cout << d[y]-1;
    return 0;
}