Cod sursa(job #2550739)

Utilizator PetyAlexandru Peticaru Pety Data 19 februarie 2020 09:11:00
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, d[30002], viz[30002], x, y;
vector<pair<int, int> > G[30002];

int bfs (int x) {
  viz[x] = 1;
  queue<int>q;
  q.push(x);
  while (!q.empty()) {
    int nod = q.front();
    q.pop();
    for (auto it : G[nod]) {
      if (!viz[it.first]) {
        viz[it.first] = 1;
        d[it.first] = d[nod] + it.second;
        q.push(it.first);
      }
    }
  }
  return abs(d[y]);
}

int main()
{
  fin >> n >> m >> x >> y;
  for (int i = 1; i <= m; i++) {
    int x, y, d;
    fin >> x >> y >> d;
    G[x].push_back({y, d});
    G[y].push_back({x, -d});
  }
  fout << bfs(x);
  return 0;
}