Cod sursa(job #2121403)

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

using namespace std;

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;
    ifstream 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;
}