Cod sursa(job #2461260)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 25 septembrie 2019 11:30:13
Problema PScNv Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

const int SIZE = 1 << 17;
int pointer = SIZE;
char buffer[SIZE];
 
char Advance() {
    if (pointer == SIZE) {
        fread(buffer, 1, SIZE, stdin);
        pointer = 0;
    }
    return buffer[pointer++];
}
 
int Read() {
    int answer = 0;
    char ch = Advance();
    while (!isdigit(ch))
        ch = Advance();
    while (isdigit(ch)) {
        answer = answer * 10 + ch - '0';
        ch = Advance();
    }
    return answer;
}

const int N = 250000 + 7;
const int M = 500000 + 7;

struct edge {
	int to;
	int c;
};

int n, m, s, d;
vector <edge> g[N];
bool u[N];

bool ok(int num) {
	for (int i = 1; i <= n; i++)
		u[i] = 0;
	u[s] = 1;
	queue <int> q;
	q.push(s);
	while (!q.empty() && u[d] == 0) {
		int nod = q.front();
		q.pop();
		for (auto it : g[nod]) {
			if (u[it.to] == 0 && it.c <= num) {
				u[it.to] = 1;
				q.push(it.to);
			}
		}
	}
	return u[d];
}

int main() {
	freopen ("pscnv.in", "r", stdin);
	freopen ("pscnv.out", "w", stdout);
	n = Read();	
	m = Read();
	s = Read();
	d = Read();
	for (int i = 1; i <= m; i++) {
		int a = Read(), b = Read(), c = Read();
		g[a].push_back({ b,c });
	}
	int l = 1, r = 1000, ans = 0;
	while (l <= r) {
		int mid = (l + r) / 2;
		if (ok(mid)) {
			ans = mid;
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	printf("%d\n", ans);
	return 0;
}