Cod sursa(job #3370871)

Utilizator AnlegliftIvanus Victor Gabriel Anleglift Data 27 octombrie 2026 16:55:43
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 u, v, d;
        fin >> u >> v >> d;
        graph[u].push_back({v, d});
        graph[v].push_back({u, -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 next_node = graph[node][i].first;
            int weight = graph[node][i].second;

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

    return 0;
}