Cod sursa(job #1584485)

Utilizator hrazvanHarsan Razvan hrazvan Data 30 ianuarie 2016 10:37:59
Problema PScNv Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 2.25 kb
#include <stdio.h>
#define MAXN 250000
#define MAXM 500000
#define BUFF (1 << 17)
FILE *in;
char s[BUFF];
int pin = BUFF;
int ult[MAXN], nod[MAXM], pnd[MAXM], next[MAXM];
int heap[MAXM], dh, bun[MAXN];

inline char cif(char x){
  if(x >= '0' && x <= '9')
    return 1;
  return 0;
}

inline char readch(){
  if(pin == BUFF){
    fread(s, 1, BUFF, in);
    pin = 0;
  }
  pin++;
  return s[pin - 1];
}

inline int readnr(){
  int rez = 0;
  char ch;
  ch = readch();
  while(!cif(ch))
    ch = readch();
  while(cif(ch)){
    rez *= 10;
    rez += ch - '0';
    ch = readch();
  }
  return rez;
}

inline void cobor(int x){
  int best, aux;
  char g = 0;
  while(2 * x + 1 < dh && !g){
    best = 2 * x + 1;
    if(2 * x + 2 < dh && pnd[heap[2 * x + 2]] < pnd[heap[best]])
      best = 2 * x + 2;
    if(pnd[heap[best]] < pnd[x]){
      aux = heap[x];  heap[x] = heap[best];  heap[best] = aux;
      x = best;
    }
    else
      g = 1;
  }
}

inline void urc(int x){
  int aux, best;
  while(x > 0 && pnd[heap[x]] < pnd[heap[(x - 1) / 2]]){
    best = (x - 1) / 2;
    aux = heap[x];  heap[x] = heap[best];  heap[best] = aux;
    x = best;
  }
}

inline void add(int p){
  heap[dh] = p;
  urc(dh);
  dh++;
}

inline int scot(){
  int rez = heap[0];
  heap[0] = heap[dh - 1];
  dh--;
  cobor(0);
  return rez;
}

inline int APMP(int x, int y){
  memset(bun, 0, sizeof bun);
  bun[x] = 1;
  int poz = ult[x], p, rez = 0;
  while(poz != -1){
    add(poz);
    poz = next[poz];
  }
  while(!bun[y]){
    p = scot();
    if(rez < pnd[p])
      rez = pnd[p];
    poz = ult[nod[p]];
    while(poz != -1){
      add(poz);
      poz = next[poz];
    }
    bun[nod[p]] = 1;
  }
  return rez;
}

int main(){
  in = fopen("pscnv.in", "r");
  int n, m, i, x, y, z, d = 0, px, py;
  n = readnr();  m = readnr();  px = readnr();  py = readnr();
  px--;  py--;
  memset(ult, -1, sizeof ult);
  for(i = 0; i < m; i++){
    x = readnr();  y = readnr();  z = readnr();
    x--;  y--;
    nod[d] = y;
    next[d] = ult[x];
    pnd[d] = z;
    ult[x] = d;
    d++;
  }
  fclose(in);
  int rez = APMP(px, py);
  FILE *out = fopen("pscnv.out", "w");
  fprintf(out, "%d", rez);
  fclose(out);
  return 0;
}