Cod sursa(job #2077196)

Utilizator flibiaVisanu Cristian flibia Data 27 noiembrie 2017 19:56:46
Problema PScNv Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <bits/stdc++.h>

using namespace std;

#define dim 10000001
char buff[dim];
int pos = 0;

void read(int &nr){
	nr = 0;
	while(buff[pos] < '0' || buff[pos] > '9')
		if(++pos == dim) fread(buff, 1, dim, stdin), pos = 0;
	while(buff[pos] >= '0' && buff[pos] <= '9'){
		nr = 10*nr + buff[pos] - '0';
		if(++pos == dim) fread(buff, 1, dim, stdin), pos = 0;
	}
}

int n, m, x, y, st, dr, mid, from, to, c;
vector <pair <int, int> > v[250100];
bool viz[250100];
queue <int> q;

bool ride(int sz){
	memset(viz, 0, sizeof viz);
	while(!q.empty()) 
		q.pop();
	q.push(from);
	viz[from] = 1;
	while(!q.empty()){
		int dad = q.front();
		if(dad == to)
			return 1;
		q.pop();
		for(auto son : v[dad])
			if(son.second <= sz && !viz[son.first]){
				viz[son.first] = 1;
				q.push(son.first);
			}
	}
	return 0;
}

int main(){
	freopen("pscnv.in", "r", stdin);
	freopen("pscnv.out", "w", stdout);
	cin >> n >> m >> from >> to;
	for(int i = 1; i <= m; i++){
		read(x); read(y); read(c);
		v[x].push_back({y, c});
	}
	st = 1; dr = 1000;
	while(st <= dr){
		mid = (st + dr) >> 1;
		if(ride(mid)) 
			dr = mid - 1;
		else st = mid + 1;
	}
	cout << st;
}