Cod sursa(job #3145988)

Utilizator andrei_C1Andrei Chertes andrei_C1 Data 17 august 2023 18:56:23
Problema PScNv Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.91 kb
#include <bits/stdc++.h>

using namespace std;

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp ==  65536) {
			sp = 0;
			fread(buff, 1,  65536, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[65536]();
		sp = 65536 - 1;
	}
	
	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
	
	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

InParser fin("pscnv.in");
ofstream fout("pscnv.out");

const int kN = 25e4;

int x, y, k;
vector<vector<pair<int, int>>> adj;
vector<bool> vis;

void maxSelf(int &x, int y) {
	if(y > x) {
		x = y;
	}
}

bool dfs(int u = x) {
	if(u == y) {
		return 1;
	}
	bool ans = 0;
	vis[u] = 1;
	for(const auto &it: adj[u]) {
		if(!vis[it.first] && it.second <= k) {
			ans |= dfs(it.first);
		}
		if(ans == 1) {
			break;
		}
	}
	return ans;
}

int main() {
	int n, m;
	fin >> n >> m >> x >> y;
	x--; y--;
	adj = vector<vector<pair<int, int>>>(n);
	int kmax = 1;
	vector<int> vals;
	for(int i = 0; i < m; i++) {
		int u, v, k;
		fin >> u >> v >> k;
		u--; v--;
		adj[u].emplace_back(v, k);
		maxSelf(kmax, k);
		vals.emplace_back(k);
	}
	sort(vals.begin(), vals.end());
	vals.erase(unique(vals.begin(), vals.end()), vals.end());
	int l = 0, r = vals.size() - 1, mid;
	while(l <= r) {
		mid = (l + r) >> 1;
		k = vals[mid];
		vis = vector<bool>(n, 0);
		if(dfs()) {
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	fout << vals[r + 1] << '\n';
	return 0;
}