Cod sursa(job #3374501)

Utilizator MasterFire1990Balaban Nicolae Adrian MasterFire1990 Data 9 noiembrie 2026 01:26:31
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <bits/stdc++.h>
using namespace std;
int main() {
  ifstream f("sate.in");
  ofstream g("sate.out");
  int n, m, x, y;
  f >> n >> m >> x >> y;
  vector<vector<pair<int, int>>> a(n + 1);
  while (m--) {
    int i, j, d;
    f >> i >> j >> d;
    a[i].push_back({j, d});
    a[j].push_back({i, -d});
  }
  vector<long long> p(n + 1);
  vector<bool> v(n + 1);
  queue<int> q;
  q.push(x);
  v[x] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto e : a[u])
      if (!v[e.first]) {
        v[e.first] = 1;
        p[e.first] = p[u] + e.second;
        q.push(e.first);
      }
  }
  g << llabs(p[y]);
}