Cod sursa(job #617506)

Utilizator dushmiMihai-Alexandru Dusmanu dushmi Data 14 octombrie 2011 22:55:10
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

const int N = 250005;

vector <pair <int, int> > L[N];

int n, sup, S, D;

bool used[N];

char s[30];

bool cif(char ch) {
  return '0' <= ch && ch <= '9';
}

void read() {
  int m, x, y, p, poz;

  scanf("%d%d%d%d\n", &n, &m, &S, &D);

  for (int i = 1; i <= m; ++i) {
    gets(s);

    x = y = p = poz = 0;

    while (cif(s[poz]))
      x = x * 10 + s[poz] - '0', ++poz;
    
    ++poz;

    while (cif(s[poz]))
      y = y * 10 + s[poz] - '0', ++poz;

    ++poz;
    
    while (cif(s[poz]))
      p = p * 10 + s[poz] - '0', ++poz;

    L[x].push_back(make_pair(y, p));
  }
}

void df(int nod) {
  used[nod] = 1;

  for (int j = 0; j < (int)L[nod].size() && !used[D]; ++j)
    if (!used[L[nod][j].first] && L[nod][j].second <= sup)
      df(L[nod][j].first);
}

bool valid(int x) {
  sup = x;
  
  memset(used, 0, N * sizeof(bool));
  
  df(S);

  return used[D];
}

int cautb() {
  int i = 0, pas = 1 << 9;

  for (i = 0; pas; pas >>= 1)
    if (!valid(i + pas))
      i += pas;

  return i + 1;
}

int main() {
  freopen("pscnv.in", "r", stdin);
  freopen("pscnv.out", "w", stdout);

  read();

  printf("%d", cautb());

  return 0;
}