Cod sursa(job #950916)

Utilizator antonioteoZait Teodor Antonio antonioteo Data 18 mai 2013 18:34:11
Problema PScNv Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <cstdio>
#include <vector>
#include <cstring>
#define dim 8192
using namespace std;
const char iname[] = "pscnv.in";
const char oname[] = "pscnv.out";
struct nod{
    int y, c; nod(){}; nod(int m, int n){ y = m; c = n;};
};
vector <nod> v[250004];
bool viz[250004];
int x;
char ax[dim];
int pz;
inline void cit(int &x){
    x = 0;
    while (ax[pz] < '0' || ax[pz] > '9')
        if (++pz == dim)
            fread (ax, 1, dim, stdin), pz = 0;
    while (ax[pz] >= '0' && ax[pz] <= '9'){
        x = x * 10 + ax[pz] - '0';
        if (++pz == dim)
            fread (ax, 1, dim, stdin), pz = 0;
    }
}
int N, M, A, B, i, j, g, h, mv, ANS;
inline void DFS(int node, int X)
{
    vector <nod> :: iterator it, fin;
    viz[node] = true;
    it = v[node].begin();
    fin = v[node].end();
    for (; it != fin; ++it)
    {
        if (!viz[it -> y] && it -> c <= X)
            DFS(it -> y, X);
    }
}
inline int BS(){
    int Mij, St = 1, Dr = mv;
    while (St <= Dr){
        Mij = (St + Dr) / 2;
        DFS(A, Mij);
        if (!viz[B]) St = Mij + 1;
        else{
            Dr = Mij - 1;
            ANS = Mij;
        }
        memset(viz, 0, sizeof(viz));
    }
    return ANS;
}
int main()
{
    freopen(iname,"r",stdin);
    freopen(oname,"w",stdout);
    cit(N); cit(M); cit(A); cit(B);
    while (M--){
        cit(g); cit(h); cit(i);
        if (i > mv) mv = i;
        v[g].push_back(nod(h, i));
    }
    printf("%d\n", BS());
    return 0;
}