Cod sursa(job #3374642)

Utilizator valexandreiValeanu Alexandru-Andrei valexandrei Data 9 noiembrie 2026 08:22:57
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m, x, y;
vector<pair<int, int>> v[30005];
long long d[30005];
bool viz[30005];
queue<int> q;

int main()
{
    fin >> n >> m >> x >> y;
    for (int i = 1; i <= m; i++)
    {
        int a, b, c;
        fin >> a >> b >> c;
        v[a].push_back({b, c});
        v[b].push_back({a, -c});
    }
    q.push(x);
    viz[x] = true;
    while (!q.empty())
    {
        int nod = q.front();
        q.pop();
        for (auto it : v[nod])
        {
            if (!viz[it.first])
            {
                viz[it.first] = true;
                d[it.first] = d[nod] + it.second;
                q.push(it.first);
            }
        }
    }
    fout << d[y] << "\n";
    return 0;
}