Cod sursa(job #1106490)

Utilizator gbi250Gabriela Moldovan gbi250 Data 12 februarie 2014 20:53:21
Problema PScNv Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>

#define SIZE 250002
#define cost first
#define node2 second
using namespace std;

int n, m, source, sink, min_cost, dist[SIZE];

vector < pair < int, int > > adj[SIZE];
vector <int> lst[SIZE];

inline void read();
inline void solve();
inline void write();

int main()
{
    read();
    solve();
    write();
    return 0;
}

inline void read()
{
    freopen("pscnv.in", "r", stdin);
    scanf("%d%d%d%d", &n, &m, &source, &sink);
    int x, y, cost;
    while( m-- )
    {
        scanf("%d%d%d", &x, &y, &cost);
        adj[x].push_back( make_pair( cost, y ) );
    }
}

inline void solve()
{
    memset( dist, 0x3f, sizeof(dist) );

    lst[ 0 ].push_back( source );
    dist[ source ] = 0;

    for(int cost = 0; cost <= 1000; ++cost)
    {
        min_cost = cost;
        for(vector < int > :: iterator it_lst = lst[ cost ].begin(); it_lst != lst[ cost ].end(); ++it_lst)
        {
            int node = *it_lst;

            if( node == sink )
                return ;

            if( dist[node] != min_cost )
                continue;

            for(vector < pair < int, int > > :: iterator it = adj[ node ].begin(); it != adj[ node ].end(); ++it)
            {
                int cost_tmp = max( min_cost, (*it).cost );

                if( dist[ (*it).node2 ] >= cost_tmp )
                {
                    dist[ (*it).node2 ] = cost_tmp;
                    lst[ cost_tmp ].push_back( (*it).node2);
                }
            }
        }
    }
}

inline void write()
{
    freopen("pscnv.out", "w", stdout);
    printf("%d\n", min_cost);
}