Cod sursa(job #780646)

Utilizator tzipleatudTudor Tiplea tzipleatud Data 20 august 2012 22:40:08
Problema PScNv Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <fstream>
#include <cstdio>
#include <cstring>
#include <vector>
#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];
vector<int> C[1010];

int N,M,i,j,a,b,c,S,F;
int D[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));
   C[0].push_back(S);
   D[S]=0;
   int c,nod,cost;
   unsigned int j;
   for (c=0; c<=1000; c++)
   {
       for (i=0; i<C[c].size(); i++)
       {
           nod=C[c][i];
           if (nod==F) return c;

           if (D[nod]!=c) continue;

           for (j=0; j<G[nod].size(); j++)
           {
               cost=max(G[nod][j].second,c);
               if (cost>=D[G[nod][i].first]) continue;
               D[G[nod][i].first]=cost;
               C[cost].push_back(G[nod][i].first);
           }
       }
   }

   return 0;
}

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

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