Cod sursa(job #2121411)

Utilizator cella.florescuCella Florescu cella.florescu Data 3 februarie 2018 17:36:54
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <bits/stdc++.h>

using namespace std;

class InputReader {
  private:
    static const int BUFF_SIZE = 1 << 17;
    int bp;
    char buff[BUFF_SIZE];
    FILE *fin;
    inline void nxt() {
      if (++bp == BUFF_SIZE) {
        fread(buff, sizeof(char), BUFF_SIZE, fin);
        bp = 0;
      }
    }
  public:
    InputReader (const char *file_name) {
      fin = fopen(file_name, "r");
      bp = BUFF_SIZE - 1;
    }
    inline void close() {
      fclose(fin);
    }
    InputReader& operator >> (int &num) {
      num = 0;
      while (isdigit(buff[bp]) == 0)
        nxt();
      while (isdigit(buff[bp])) {
        num = num * 10 + buff[bp] - '0';
        nxt();
      }
      return *this;
    }
};

const int MAXN = 25e4;
const int MAXK = 1e3;
const int INF = 0x3f3f3f3f;

vector <pair <int, int>> g[MAXN + 1];
int seen[MAXN + 1];
queue <int> q[MAXK + 1];

int main()
{
    int n, m, x, y;
    InputReader fin("pscnv.in");
    fin >> n >> m >> x >> y;
    for (int i = 0; i < m; ++i) {
      int a, b, c;
      fin >> a >> b >> c;
      g[a].emplace_back(b, c);
    }
    fin.close();
    int cost = 0;
    q[0].push(x);
    memset(seen, INF, sizeof seen);
    while (seen[y] == INF) {
      if (q[cost].empty())
        ++cost;
      else {
        int node = q[cost].front();
        q[cost].pop();
        if (seen[node] == INF) {
          seen[node] = cost;
          for (auto it : g[node])
            if (seen[it.first] == INF)
              q[max(cost, it.second)].push(it.first);
        }
      }
    }
    ofstream fout("pscnv.out");
    fout << cost;
    fout.close();
    return 0;
}