Cod sursa(job #1135317)

Utilizator ELHoriaHoria Cretescu ELHoria Data 7 martie 2014 17:58:59
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <sstream>

using namespace std;

const int nmax = 250000;
vector< pair<int, short> > graph[nmax];

int main()
{
	ifstream cin("pscnv.in");
	ofstream cout("pscnv.out");
	int n, m, x, y;
	string buffer((istreambuf_iterator<char>(cin)), std::istreambuf_iterator<char>());
	int bufferIndex = 0;
	queue<int> q;
	vector<bool> visited;
	
	auto nextInt = [&]() {
		int ret = 0;
		while (!('0' <= buffer[bufferIndex] && buffer[bufferIndex] <= '9')) bufferIndex++;
		while ('0' <= buffer[bufferIndex] && buffer[bufferIndex] <= '9') {
			ret = ret * 10 + buffer[bufferIndex++] - '0';
		}
		return ret;
	};

	n = nextInt(); m = nextInt(); x = nextInt(); y = nextInt();
	x--;
	y--;

	visited.resize(n);

	for (int a, b, c; m; m--) {
		a = nextInt(); b = nextInt(); c = nextInt();
		a--;
		b--;
		graph[a].push_back({ b, c });
	}

	int l = 1, r = 1000;
	while (l <= r) {
		int mid = (l + r) / 2;
		fill(visited.begin(), visited.end(),false);
		visited[x] = true;
		q.push(x);
		while (!q.empty() && !visited[y]) {
			int v = q.front();
			q.pop();
			for (const auto& w : graph[v]) {
				if (w.second <= mid && !visited[w.first]) {
					visited[w.first] = true;
					q.push(w.first);
				}
			}
		}

		while (!q.empty()) q.pop();

		if (visited[y]) {
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}

	cout << l;
	return 0;
}