Cod sursa(job #2104620)

Utilizator DruffbaumPopescu Vlad Druffbaum Data 11 ianuarie 2018 22:51:48
Problema PScNv Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <cstdio>
#include <cctype>
#include <cstring>
#include <vector>

const int MAXN = 2e5 + 5e4;
const int MAXK = 1e3;
const int INF = 0x3f3f3f3f;
#define BUF_SIZE 1 << 12

struct Edge {
  int u, c;
};

std::vector <Edge> g[MAXN + 1];
std::vector <int> dist[MAXK + 1];
int dm[MAXN + 1], pos = BUF_SIZE;
char buf[BUF_SIZE];

inline char nextch(FILE *f) {
  if (pos == BUF_SIZE) {
    fread(buf, BUF_SIZE, 1, f);
    pos = 0;
  }
  return buf[pos++];
}

inline int nextnum(FILE *f) {
  int a = 0;
  char c = nextch(f);
  while (!isdigit(c)) {
    c = nextch(f);
  }
  while (isdigit(c)) {
    a = 10 * a + c - '0';
    c = nextch(f);
  }
  return a;
}

static inline int max(int a, int b) {
  return a > b ? a : b;
}

int main() {
  int n, m, s, d, u, v, c, t;
  FILE *f = fopen("pscnv.in", "r");
  n = nextnum(f), m = nextnum(f), 
  s = nextnum(f), d = nextnum(f);
  for (int i = 0; i < m; ++i) {
    u = nextnum(f), v = nextnum(f),
    c = nextnum(f);
    g[u].push_back({v, c});
  }
  fclose(f);
  memset(dm, INF, sizeof(dm));
  dm[s] = 0;
  dist[0].push_back(s);
  for (int i = 0; i <= MAXK; ++i) {
    for (auto u: dist[i]) {
      if (dm[u] >= i) {
        for (auto v: g[u]) {
          t = max(i, v.c);
          if (dm[v.u] > t) {
            dm[v.u] = t;
            dist[t].push_back(v.u);
          }
        }
      }
    }
  }
  f = fopen("pscnv.out", "w");
  fprintf(f, "%d\n", dm[d]);
  fclose(f);
  return 0;
}