Cod sursa(job #3373782)

Utilizator indianu_talpa_iuteTisca Catalin indianu_talpa_iute Data 7 noiembrie 2026 21:13:08
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
#include <vector>
#include <queue>

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

const int NMAX = 30005;

int n, m, x, y;
std::vector<std::pair<int, int>> graph[NMAX];
int dist[NMAX];
bool visited[NMAX];

void bfs() {
    std::queue<int> q;
    q.push(x);
    visited[x] = true;
    dist[x] = 0;

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

        if (node == y)
            return;

        for (auto &edge : graph[node]) {
            int next = edge.first;
            if (!visited[next]) {
                visited[next] = true;
                dist[next] = dist[node] + edge.second;
                q.push(next);
            }
        }
    }
}

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

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

    bfs();

    int result = dist[y];
    if (result < 0)
        result = -result;

    fout << result << '\n';
    return 0;
}