Cod sursa(job #780644)

Utilizator tzipleatudTudor Tiplea tzipleatud Data 20 august 2012 22:29:52
Problema PScNv Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <fstream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#define NM 250010
#define INF 0x3f3f3f

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];
queue<int> Q;

int N,M,i,j,a,b,c,S,F;
int D[NM];
bool V[NM];

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));
    }
    fclose(fin);
}

int Solve ()
{
    memset(D,INF,sizeof(D));
    unsigned int i;
    int nod,cost;
    Q.push(S);
    V[S]=1;
    D[S]=0;
    while (!Q.empty())
    {
        nod=Q.front();
        Q.pop();
        V[nod]=0;
        for (i=0; i<G[nod].size(); i++)
        {
            cost=max(D[nod],G[nod][i].second);
            if (cost>=D[G[nod][i].first]) continue;
            D[G[nod][i].first]=cost;
            if (V[G[nod][i].first]) continue;
            Q.push(G[nod][i].first);
            V[G[nod][i].first]=1;
        }
    }
    return D[F];
}

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

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