Cod sursa(job #3370878)

Utilizator AnlegliftIvanus Victor Gabriel Anleglift Data 27 octombrie 2026 17:22:22
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;

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

const int MAXN = 30005;

vector<pair<int, int>> graph[MAXN];
bool visited[MAXN];
int dist[MAXN];

int main()
{
    int n, m, x, y;
    fin >> n >> m >> x >> y;

    for (int i = 0; i < m; ++i)
    {
        int a, b, d;
        fin >> a >> b >> d;
        graph[a].push_back({b, d});
        graph[b].push_back({a, -d});
    }

    queue<int> q;
    q.push(x);

    visited[x] = true;
    dist[x] = 0;

    while (!q.empty())
    {
        int node = q.front();
        q.pop();

        if (node == y)
        {
            fout << abs(dist[y]) << "\n";
            return 0;
        }

        for (int i = 0; i < graph[node].size(); i++)
        {
            int neighbour = graph[node][i].first;
            int distance = graph[node][i].second;

            if (!visited[neighbour])
            {
                visited[neighbour] = true;
                dist[neighbour] = dist[node] + distance;
                q.push(neighbour);
            }
        }
    }

    return 0;
}