Cod sursa(job #2918194)

Utilizator tomaionutIDorando tomaionut Data 10 august 2022 14:11:15
Problema PScNv Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

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

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

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	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");
int n, m, k, dp[250005], X, Y;
bitset <250005> viz;
vector <pair <int, int> > a[250005];
priority_queue <pair <int, int> > q;
int main()
{
	int i, j, c, x, cost;
	fin >> n >> m >> X >> Y;
	while (m--)
	{
		fin >> i >> j >> c;
		a[i].push_back({ c, j });
	}

	for (i = 1; i <= n; i++)
		dp[i] = INF;

	dp[X] = 0;
	q.push({ 0, X });
	while (!q.empty())
	{
		x = q.top().second;
		q.pop();
		if (viz[x] == 0)
		{
			viz[x] = 1;
			for (auto w : a[x])
			{
				cost = max(w.first, dp[x]);
				if (dp[w.second] > cost)
				{
					dp[w.second] = cost;
					q.push({ -cost, w.second });
				}
			}
		}
	}

	fout << dp[Y] << "\n";

	return 0;
}