Cod sursa(job #1906650)

Utilizator raulmuresanRaul Muresan raulmuresan Data 6 martie 2017 15:35:12
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.24 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>
#include <cstring>

using namespace std;

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

const int dim = 250100;
const int inf = 1005;

int nodeCount, edgeCount, source, destination;
vector< pair<int, int> > graph[dim];

int dp[dim];
vector<int> lists[inf];

int Dijkstra() {

    for (int i = 1; i <= nodeCount; ++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 curr = lists[i][j];

            if (dp[curr] < i)
                continue;

            if (curr == destination)
                return i;

            for (auto adj : graph[curr]) {
                int aux = max(dp[curr], adj.second);
                if (dp[adj.first] <= aux)
                    continue;
                dp[adj.first] = aux;
                lists[aux].push_back(adj.first);
            }

        }
    }

    return inf;

}

int main() {

    fin >> nodeCount >> edgeCount >> source >> destination;

    for (int i = 1; i <= edgeCount; ++i) {
        int x, y, cost;
        fin >> x >> y >> cost;
        graph[x].push_back(make_pair(y, cost));
    }

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

    return 0;

}