Cod sursa(job #780640)

Utilizator tzipleatudTudor Tiplea tzipleatud Data 20 august 2012 22:25:17
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <fstream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#define NM 250010

using namespace std;

FILE* fin=fopen("pscnv.in","r");
FILE* fout=fopen("pscnv.out","w");
const int maxb=8192;
char buf[maxb];
int ptr=0;

inline int GetInt()
{
    int nr=0;
    while(buf[ptr]<'0'||'9'<buf[ptr])
        if (++ptr>=maxb) fread(buf,maxb,1,fin),ptr=0;
    while ('0'<=buf[ptr]&&buf[ptr]<='9')
    {
        nr=nr*10+buf[ptr]-'0';
        if (++ptr>=maxb) fread(buf,maxb,1,fin),ptr=0;
    }
    return nr;
}

typedef pair<int, int> PI;
vector<PI> G[NM];
bool V[NM];
queue<int> Q;

int N,M,i,j,a,b,c,S,F,K;


void Read ()
{
    N=GetInt();
    M=GetInt();
    S=GetInt();
    F=GetInt();

    for (i=1; i<=M; i++)
    {
        a=GetInt();
        b=GetInt();
        c=GetInt();
        G[a].push_back(make_pair(b,c));
        K=max(K,c);
    }
    fclose(fin);
}

bool Check (int K)
{
    memset(V,0,sizeof(V));
    Q.push(S);
    V[S]=1;
    int nod;
    unsigned int i;
    while (!Q.empty())
    {
        nod=Q.front();
        Q.pop();
        for (i=0; i<G[nod].size(); i++)
            if (!V[G[nod][i].first] && G[nod][i].second<=K)
            {
                V[G[nod][i].first]=1;
                Q.push(G[nod][i].first);
            }
    }
    return V[F];
}

int Search ()
{
    int p=0,u=K,m,ANS=K;

    while (p<=u)
    {
        m=(p+u)/2;
        if (Check(m))
        {
            ANS=m;
            u=m-1;
        }
        else
            p=m+1;
    }

    return ANS;
}

void Print ()
{
    fprintf(fout,"%d\n",Search());
    fclose(fout);
}

int main ()
{
    Read();
    Print();
    return 0;
}