Cod sursa(job #1135302)

Utilizator ELHoriaHoria Cretescu ELHoria Data 7 martie 2014 17:41:37
Problema PScNv Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;


int main()
{
	ifstream cin("pscnv.in");
	ofstream cout("pscnv.out");
	int n, m, x, y;
	vector <vector< pair<int,short> > > graph;
	queue<int> q;
	vector<bool> visited;
	cin >> n >> m >> x >> y;
	x--;
	y--;
	graph.resize(n);
	visited.resize(n);
	char str[32];
	for (int a, b, c; m; m--) {
		cin.getline(str, 32);
		sscanf(str, "%d %d %d", &a, &b, &c);
		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()) {
			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);
				}
			}
		}

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

	cout << l;
	return 0;
}