Cod sursa(job #3372147)

Utilizator RaducuSebasRaducu Sebastian RaducuSebas Data 2 noiembrie 2026 17:13:35
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m, x, y;
vector<pair<int, int>> g[30005];
int d[30005];
bool vz[30005];

void bfs(int s) {
  queue<int> q;
  vz[s] = 1;
  d[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (u == y)
      return;
    for (auto &e : g[u]) {
      int p = e.first;
      int w = e.second;
      if (!vz[p]) {
        vz[p] = 1;
        d[p] = d[u] + w;
        q.push(p);
      }
    }
  }
}

int main() {
  fin >> n >> m >> x >> y;
  int a, b, c;
  for (int i = 1; i <= m; i++) {
    fin >> a >> b >> c;
    g[a].push_back({b, c});
    g[b].push_back({a, -c});
  }

  bfs(x);

  fout << abs(d[y]) << "\n";
  return 0;
}