Cod sursa(job #1454398)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 26 iunie 2015 14:26:32
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <fstream>
#include <iostream>
#include <list>
#include <vector>
#include <utility>
using namespace std;

template <int max_v>
class dial_pq{
	int cur = 0;
	vector<list<int> > buf;
	vector<list<int>::iterator> poz;
	vector<int> dist;
	void flush(){
		while(buf[cur].empty()){
			++cur; } }
public:
	dial_pq(const int n):
		buf(max_v+2),
		poz(n),
		dist(n){
		for(int i = 0; i < n; ++i){
			dist[i] = max_v+1;
			buf[max_v+1].push_front(i);
			poz[i] = buf[max_v+1].begin(); } }
	int top(){
		flush();
		return buf[cur].front(); }
	void pop(){
		flush();
		buf[cur].pop_front(); }
	void decrease_key(const int x, const int la){
		buf[dist[x]].erase(poz[x]);
		dist[x] = la;
		buf[la].push_front(x);
		poz[x] = buf[la].begin(); } };

constexpr int max_d = 1000;

int dial(const vector<vector<pair<int, int> > >& graf, const int x, const int y){
	vector<int> dist(graf.size(), max_d+1);
	dial_pq<max_d> pq(graf.size());
	pq.decrease_key(x, 0);
	dist[x] = 0;
	while(true){
		const int cur = pq.top();
		if(cur == y){
			return dist[cur]; }
		pq.pop();
		for(const auto next : graf[cur]){
			if(dist[next.first] > max(dist[cur], next.second)){
				dist[next.first] = max(dist[cur], next.second);
				pq.decrease_key(next.first, dist[next.first]); } } } }

void citeste_date(ifstream& f, vector<vector<pair<int, int> > >& graf, int& x, int& y){
	int n, m;
	f >> n >> m;
	graf.resize(n);
	f >> x >> y;
	--x, --y;
	for(int i = 0, a, b, c; i < m; ++i){
		f >> a >> b >> c;
		--a, --b;
		graf[a].emplace_back(b, c); } }

int main(){
	ifstream f("pscnv.in");
	ofstream g("pscnv.out");
	vector<vector<pair<int, int> > > graf;
	int x, y;
	citeste_date(f, graf, x, y);
	g << dial(graf, x, y);
	return 0; }