Cod sursa(job #3316610)

Utilizator herbiHreban Antoniu George herbi Data 19 octombrie 2025 14:09:16
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

typedef pair<int, int> pii;

int solve()
{
    int n, m, a, b;
    fin >> n >> m >> a >> b;
    vector<vector<pii>> g(n + 1);
    int x, y, c;
    for (int i = 0; i < m; ++i)
    {
        fin >> x >> y >> c;
        g[x].push_back({ y, c });
        g[y].push_back({ x, -c });
    }
    vector<int> dist(n + 1, -1);
    dist[a] = 0;
    queue<int> q;
    q.push(a);
    while (!q.empty())
    {
        int crt = q.front();
        if (crt == b)
            return dist[crt];
        q.pop();
        for (auto p : g[crt])
        {
            if (dist[p.first] == -1)
            {
                q.push(p.first);
                dist[p.first] = p.second + dist[crt];
            }
        }
    }
    return -1;
}

int main()
{
    fout << solve() << '\n';
    return 0;
}