Cod sursa(job #2065347)

Utilizator andrei.arnautuAndi Arnautu andrei.arnautu Data 13 noiembrie 2017 18:38:50
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
/**
  *  Worg
  */
#include <queue>
#include <cstdio>
#include <vector>
#include <algorithm>

FILE *fin = freopen("pscnv.in", "r", stdin); FILE *fout = freopen("pscnv.out", "w", stdout);

const int kMaxK = 1000;

/*-------- Data --------*/
int N, M, X, Y;
std::vector<std::pair<int, int > > edges[kMaxK + 1];
std::vector<std::vector<std::pair<int, int > > > graph;

std::queue<int > list[kMaxK + 1];
/*-------- --------*/

int main() {
  scanf("%d%d%d%d", &N, &M, &X, &Y); X--; Y--;
  graph.resize(N);
  for(int i = 1; i <= M; i++) {
    int x, y, k; scanf("%d%d%d", &x, &y, &k);
    x--; y--;
    graph[x].emplace_back(y, k); graph[y].emplace_back(x, k);
  }

  std::vector<int > best(N, -1);
  list[0].push(X);

  int cursor = 0;
  while(cursor <= kMaxK) {
    if(list[cursor].empty()) {
      cursor ++; continue;
    }

    int node = list[cursor].front(); list[cursor].pop();
    if(best[node] != -1) continue;

    if(node == Y) {
      printf("%d\n", cursor); return 0;
    }

    best[node] = cursor;
    for(auto& edge : graph[node]) {
      list[std::max(cursor, edge.second)].push(edge.first);
    }
  }
    return 0;
}