Cod sursa(job #2019872)

Utilizator mouse_wirelessMouse Wireless mouse_wireless Data 8 septembrie 2017 19:04:13
Problema Sate Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.87 kb
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <climits>
#include <queue>
#include <stack>
using namespace std;
typedef long long LL;
typedef unsigned long long hsh;

#ifdef INFOARENA
#define ProblemName "sate"
#endif

#define MCONCAT(A, B) A B
#ifdef ProblemName
#define InFile MCONCAT(ProblemName, ".in")
#define OuFile MCONCAT(ProblemName, ".out")
#else
#define InFile "fis.in"
#define OuFile "fis.out"
#endif

const int MAXBUF = 10000010;
char parsebuf[MAXBUF], *head;
bool numeric[255];

void parseInit() {
  int off = fread(parsebuf, 1, MAXBUF, stdin);
  assert(off < MAXBUF - 10);
  parsebuf[off] = 0;
  head = parsebuf;
  memset(numeric, 0, sizeof(numeric));
  for (int i = '0'; i <= '9'; ++i)
    numeric[i] = true;
}

int nextInt() {
  for (; !numeric[*head]; ++head);
  int ans = 0;
  for (; numeric[*head]; ++head)
    ans = ans * 10 + (*head) - '0';
  return ans;
}

const int MAXN = 30010;
vector< pair<int, int> > G[MAXN];
int dst[MAXN];
int S[MAXN], ssz;

void DFS(int nod, int tar) {
  ssz = 0;
  S[ssz++] = nod;
  dst[nod] = 0;
  while(ssz) {
    int x = S[--ssz];
    for (const auto &it : G[x]) {
      if (dst[it.first] != INT_MAX)
        continue;
      dst[it.first] = dst[x] + it.second;
      if (it.first == tar)
        return;
      S[ssz++] = it.first;
    }
  }
}

int main() {
  assert(freopen(InFile, "r", stdin));
  assert(freopen(OuFile, "w", stdout));
  parseInit();
  int N, M, X, Y;
  N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt();
  for (int i = 0; i<N; ++i)
    dst[i] = INT_MAX;
  --X, --Y;
  if (X > Y)
    swap(X, Y);
  while (M--) {
    int a, b, c;
    a = nextInt(); b = nextInt(); c = nextInt();
    --a, --b;
    if (a > b)
      swap(a, b);
    G[a].push_back(make_pair(b, c));
    G[b].push_back(make_pair(a, -c));
  }
  DFS(X, Y);
  printf("%d\n", dst[Y]);
  return 0;
}