Cod sursa(job #1435605)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 13 mai 2015 20:49:10
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.78 kb
#include <fstream>
#include <string>
#include <cstdlib>
#include <utility>
#include <array>
#include <vector>
#include <stack>
using namespace std;

constexpr int inf = 1001;

struct muchie{
	int first : 20, second : 12;
	constexpr muchie(const int x, const int y):
		first(x), second(y){} };

class dial_pq {
	array<stack<int, vector<int> >, inf + 1> buf;
	int poz = 0;
	void clear() {
		while (buf[poz].empty()) {
			++poz;
		}
	}

public:
	void emplace(const int x, const int y) { buf[y].push(x); }
	const muchie top() {
		clear();
		return muchie(buf[poz].top(), poz);
	}
	void pop() {
		clear();
		buf[poz].pop();
	}
};

int dial(const vector<vector<muchie> > &vecini, const int x,
				 const int y) {

	dial_pq st;
	vector<int> rez(vecini.size(), inf);

	st.emplace(x, 0);
	rez[x] = 0;

	int cur = 0, cost_cur = 0, ipotetic = 0;

	while (true) {
		cur = st.top().first;
		cost_cur = st.top().second;
		st.pop();
		if (cur == y) {
			return rez[y];
		}
		if (rez[cur] == cost_cur) {
			for (const auto next : vecini[cur]) {
				ipotetic = max((int)next.second, cost_cur);
				if (ipotetic < rez[next.first]) {
					rez[next.first] = ipotetic;
					st.emplace(next.first, ipotetic);
				}
			}
		}
	}
}

void citeste_date(vector<vector<muchie> > &vecini, int &x, int &y) {
	ifstream f("pscnv.in");
	int n, m;
	f >> n >> m >> x >> y >> ws;
	--x, --y;
	vecini.resize(n);
	const int bufsz = 20*m;
	string buf(bufsz, '\0');
	char* poz = &buf[0];
	f.read(poz, bufsz);
	for (int i = 0, a, b, c; i < m; ++i) {
		a = strtol(poz, &poz, 10);
		b = strtol(poz, &poz, 10);
		c = strtol(poz, &poz, 10);
		--a, --b;
		vecini[a].emplace_back(b, c);
	}
}

int main() {
	muchie m(249999, 1000);
	vector<vector<muchie> > vecini;
	int x, y;
	citeste_date(vecini, x, y);
	ofstream g("pscnv.out");
	g << dial(vecini, x, y);
	return 0;
}