Cod sursa(job #203413)

Utilizator tvladTataranu Vlad tvlad Data 16 august 2008 11:01:12
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

const int N = 250000;

int n,m,x,y;
vector< pair<int,int> > g[N];

bool ok ( int k ) {
	queue<int> q;
	vector<bool> used(n);
	for (int i = 0; i < n; ++i) used[i] = false;
	used[x] = true;
	for (q.push(x); !q.empty(); q.pop()) {
		for (vector< pair<int,int> >::iterator i = g[q.front()].begin(); i != g[q.front()].end(); ++i) {
			if (k >= i->second && !used[i->first]) {
				if (i->first == y) return true;
				q.push(i->first);
			}
		}
	}
	return false;
}

int search() {
	int step = 1 << 10, k = 0;
	for (; step; step >>= 1) {
		if (!ok(k+step)) {
			k += step;
		}
	}
	return k+1;
}

int main() {
	freopen("pscnv.in","rt",stdin);
	freopen("pscnv.out","wt",stdout);
	scanf("%d %d %d %d",&n,&m,&x,&y);
	--x; --y;
	for (int i = 0, a, b, c; i < m; ++i) {
		scanf("%d %d %d",&a,&b,&c);
		--a; --b;
		g[a].push_back(make_pair(b,c));
	}
	printf("%d\n",search());
	return 0;
}