Cod sursa(job #1277374)

Utilizator mvcl3Marian Iacob mvcl3 Data 27 noiembrie 2014 16:36:45
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.9 kb
#include <cstdio>
#include <queue>
#include <vector>

#define Max_Size 250009
#define Max_K 1001
#define Max_Buf 32
#define isnumber(z) ('0' <= (z) && (z) <= '9')

using namespace std;

typedef pair < int , int > POINT;

const char iname[] = "pscnv.in";
const char oname[] = "pscnv.out";

int N, M, S, F, D[Max_Size];

bool Viz[Max_Size];

vector < POINT > V[Max_Size];
queue < int > Q[Max_K];

inline void Read_Data()
{
    FILE *in = fopen(iname, "r");

    fscanf(in, "%d%d%d%d", &N, &M, &S, &F);

    char buffer[Max_Buf];

    fgets(buffer, Max_Buf, in);

    for(int j, x, y, p, i = 1; i <= M; ++i)    {
        x = y = p = 0;

        fgets(buffer, Max_Buf, in);

        for(j = 0; isnumber(buffer[j]); x = x * 10 + (buffer[j] - '0'), ++j);
        for(j = j + 1; isnumber(buffer[j]); y = y * 10 + (buffer[j] - '0'), ++j);
        for(j = j + 1; isnumber(buffer[j]); p = p * 10 + (buffer[j] - '0'), ++j);

        V[x].push_back( {y, p} );
    }
}

inline void Init()
{
    for(int i = 1; i <= N; ++i) D[i] = Max_K;
}

void BF()
{
    Q[0].push(S);
    D[S] = 0;

    vector < POINT > :: iterator it;

    for(int pondere = 0; pondere < Max_K; ++pondere)    {
        while(!Q[pondere].empty()) {
            int nod = Q[pondere].front();
            Q[pondere].pop();

            if(Viz[nod])    continue;

            Viz[nod] = 1;

            if(nod == F)    {
                pondere = Max_K;
                break;
            }

            for(it = V[nod].begin(); it != V[nod].end(); ++it)
                if(D[it->first] > max(D[nod], it->second))  {
                    D[it->first] = max(D[nod], it->second);
                    Q[D[it->first]].push(it->first);
                }
        }
    }
}

int main()
{
    Read_Data();
    Init();
    BF();

    FILE *out = fopen(oname,"w");
    fprintf(out, "%d\n", D[F]);

    return 0;
}