Cod sursa(job #1560830)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 3 ianuarie 2016 13:10:52
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.05 kb
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 250000;
const int MAX_M = 500000;
const int MAX_K = 1000;
const int NIL = -1;
const int BUFF_SIZE = 65536;

struct Edge
{
    int v;
    int cost;
    int next;
};

Edge l[MAX_M];
int adj[MAX_N];
queue <int> Q[MAX_K + 1];
int dist[MAX_N];

inline char getChar()
{
    static char buff[BUFF_SIZE];
    static int pos = BUFF_SIZE;
    if ( pos == BUFF_SIZE )
    {
        fread( buff, 1, BUFF_SIZE, stdin );
        pos = 0;
    }
    return buff[pos++];
}

inline int readInt()
{
    int q = 0;
    char c;

    do
    {
        c = getChar();

    } while ( !isdigit(c) );

    do
    {
        q = (10 * q) + (c - '0');
        c = getChar();

    } while ( isdigit(c) );

    return q;
}

void addEdge( int u, int v, int cost, int pos )
{
    l[pos].v = v;
    l[pos].cost = cost;
    l[pos].next = adj[u];
    adj[u] = pos;
}

int main(void)
{
    freopen("pscnv.in", "r", stdin);
    freopen("pscnv.out", "w", stdout);
    int N, M;
    int source, sink;

    N = readInt();
    M = readInt();
    source = readInt() - 1;
    sink = readInt() - 1;

    for ( int i = 0; i < N; ++i )
    {
        adj[i] = NIL;
        dist[i] = numeric_limits <int> :: max();
    }

    for ( int i = 0; i < M; ++i )
    {
        int u, v, cost;

        u = readInt() - 1;
        v = readInt() - 1;
        cost = readInt();

        addEdge( u, v, cost, i );
    }
    fclose(stdin);

    Q[0].push( source );
    dist[source] = 0;
    for ( int i = 0; i <= MAX_K; ++i )
    {
        while ( !Q[i].empty() )
        {
            int u = Q[i].front();
            Q[i].pop();

            if ( dist[u] != i )
                continue;

            for ( int j = adj[u]; j != NIL; j = l[j].next )
            {
                int v = l[j].v;
                int cost = max( i, l[j].cost );

                if ( dist[v] > cost )
                {
                    dist[v] = cost;
                    Q[cost].push( v );
                }
            }
        }
    }

    printf( "%d\n", dist[sink] );
    fclose(stdout);
    return 0;
}