Cod sursa(job #2557447)

Utilizator popashtefan10Popa Stefan popashtefan10 Data 25 februarie 2020 20:00:12
Problema Sate Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <iostream>
#include <cstdio>
#include <vector>
#define NMAX 30000
#define pii pair<int, int>

using namespace std;

int n, m, x, y, cdist, gasit;
int uz[NMAX + 5];
vector<pii> v[NMAX + 5];

void dfs(int crt) {
  if(crt == y) {
    printf("%d", cdist);
    gasit = 1;
    return;
  }

  uz[crt] = 1;
  for(pii next: v[crt])
    if(!uz[next.first]) {
      cdist += next.second;
      dfs(next.first);
      cdist -= next.second;
      if(gasit)
        return;
    }
}

int main() {
  freopen("sate.in", "r", stdin);
  freopen("sate.out", "w", stdout);
  int a, b, d;

  scanf("%d %d %d %d", &n, &m, &x, &y);
  for(int i = 1; i <= n; i++) {
    scanf("%d %d %d", &a, &b, &d);
    v[a].push_back({b, d});
    v[b].push_back({a, -d});
  }

  dfs(x);

  return 0;
}