Cod sursa(job #3302953)

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

using namespace std;

ifstream fin ("sate.in");
ofstream fout ("sate.out");

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()
{
    fin >> n >> m >> x >> y;
    for (int i=1; i<=m; i++)
    {
        int a, b, c;
        fin >> 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);
    fout << d[y]-1;
    return 0;
}