Cod sursa(job #1887723)

Utilizator Vlad_317Vlad Panait Vlad_317 Data 21 februarie 2017 18:52:17
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.18 kb
#include <fstream>
#include <memory>

using namespace std;

class Reader {
  public:
    Reader(const string& filename):
            m_stream(filename),
            m_pos(kBufferSize - 1),
            m_buffer(new char[kBufferSize]) {
        next();
    }

    Reader& operator>>(int& value) {
        value = 0;
        while (current() < '0' || current() > '9')
            next();
        while (current() >= '0' && current() <= '9') {
            value = value * 10 + current() - '0';
            next();
        }
        return *this;
    }

  private:
    const int kBufferSize = (1 << 17);

    char current() {
        return m_buffer[m_pos];
    }

    void next() {
        if (!(++m_pos != kBufferSize)) {
            m_stream.read(m_buffer.get(), kBufferSize);
            m_pos = 0;
        }
    }

    ifstream m_stream;
    int m_pos;
    unique_ptr<char[]> m_buffer;
};

#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX 250001
#define INF 1001
struct muchie
{
    int x, c;
};

vector<muchie> g[MAX];
vector<int> lists[INF];
int dp[MAX];
int n, m, source, destination;

int Dijkstra()
{
    for(int i = 1; i <= n; i++)
        dp[i] = INF;

    dp[source] = 0;
    lists[0].push_back(source);

    for(int i = 0; i < INF; i++)
        for(size_t j = 0; j < lists[i].size(); j++)
        {
            int node = lists[i][j];
            if(dp[node] < i)
                continue;
            if(node == destination)
                return i;
            for(int k = 0; k < g[node].size(); k++)
            {
                int sol = max(dp[node], g[node][k].c);
                if(dp[g[node][k].x] <= sol)
                    continue;
                dp[g[node][k].x] = sol;
                lists[sol].push_back(g[node][k].x);
            }
        }
    return INF;
}

int main()
{
    Reader fin("pscnv.in");
    ofstream fout("pscnv.out");


    fin >> n >> m >> source >> destination;

    for(int i = 1; i <= m; i++)
    {
        int x, y, c;
        fin >> x >> y >> c;
        g[x].push_back({y, c});
    }

    fout << Dijkstra() << '\n';

    return 0;
}