Cod sursa(job #379269)

Utilizator DraStiKDragos Oprica DraStiK Data 31 decembrie 2009 13:34:46
Problema PScNv Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <algorithm>
#include <vector>
using namespace std;

#define pb push_back
#define mp make_pair
#define DIM 250005
#define MAX 10005

vector <pair <int,int> > lst[DIM];
int n,m,pozitie,s,d,lim;
char buff[MAX];
int viz[DIM];

inline void cit (int &nr)
{
    for (nr=0; buff[pozitie]<'0' || buff[pozitie]>'9'; )
        if (++pozitie==MAX)
        {
            fread (buff,1,MAX,stdin);
            pozitie=0;
        }
    for ( ; '0'<=buff[pozitie] && buff[pozitie]<='9'; )
    {
        nr=nr*10+buff[pozitie]-'0';
        if (++pozitie==MAX)
        {
            fread (buff,1,MAX,stdin);
            pozitie=0;
        }
     }
}

void read ()
{
    int i,x,y,z;

    cit (n); cit (m); cit (s); cit (d);
    for (i=1; i<=n; ++i)
    {
        cit (x); cit (y); cit (z);
        lst[x].pb (mp (y,z));
        lim=max (lim,z);
    }
}

void df (int nod,int val)
{
    int i;

    viz[nod]=1;
    for (i=0; i<(int)lst[nod].size (); ++i)
        if (!viz[lst[nod][i].first] && lst[nod][i].second<=val)
            df (lst[nod][i].first,val);
}

int check (int val)
{
    int i;

    for (i=1; i<=n; ++i)
        viz[i]=0;
    viz[s]=1;
    df (s,val);
    if (viz[d])
        return 1;
    return 0;
}

void solve ()
{
    int st,dr,mij,sol;

    for (st=1, dr=sol=lim; st<=dr; )
    {
        mij=(st+dr)/2;
        if (check (mij))
        {
            sol=mij;
            dr=mij-1;
        }
        else
            st=mij+1;
    }
    printf ("%d",sol);
}

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

    read ();
    solve ();

    return 0;
}