Cod sursa(job #3375175)

Utilizator rxzvyMatei Razvan rxzvy Data 10 noiembrie 2026 19:30:56
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cmath>

using namespace std;

const int MAXN = 30005;

struct Edge {
    int to;
    int cost;
};

vector<Edge> adj[MAXN];
int dist[MAXN];
bool visited[MAXN];

int main() {
    ifstream cin("sate.in");
    ofstream cout("sate.out");

    int n, m, x, y;
    if (!(cin >> n >> m >> x >> y)) return 0;

    for (int k = 0; k < m; ++k) {
        int u, v, d;
        cin >> u >> v >> d;
        adj[u].push_back({v, d});
        adj[v].push_back({u, -d});
    }

    queue<int> q;
    q.push(x);
    visited[x] = true;
    dist[x] = 0;

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

        if (u == y) break;

        for (auto edge : adj[u]) {
            int v = edge.to;
            int cost = edge.cost;

            if (!visited[v]) {
                visited[v] = true;
                dist[v] = dist[u] + cost;
                q.push(v);
            }
        }
    }

    cout << abs(dist[y]) << "\n";

    return 0;
}