Cod sursa(job #1383049)

Utilizator vladrochianVlad Rochian vladrochian Data 9 martie 2015 20:50:45
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;

const int kMaxN = 250005, kMaxK = 1005;

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

int N, M, x, y;
int mx[kMaxN];
vector<pair<int, int>> G[kMaxN];
vector<int> q[kMaxK];
char buffer[35], *p;

void Parse(int &x) {
	x = 0;
	while (!isdigit(*p))
		++p;
	while (isdigit(*p))
		x = x * 10 + *(p++) - '0';
}

int main() {
	fin.getline(p = buffer, 35);
	Parse(N);
	Parse(M);
	Parse(x);
	Parse(y);
	while (M--) {
		int x, y, k;
		fin.getline(p = buffer, 35);
		Parse(x);
		Parse(y);
		Parse(k);
		G[x].emplace_back(y, k);
	}
	memset(mx, 0x3f, sizeof mx);
	mx[x] = 0;
	q[0].push_back(x);
	for (int cost = 0; cost < kMaxK; ++cost)
		for (size_t i = 0; i < q[cost].size(); ++i) {
			int node = q[cost][i];
			if (node == y) {
				fout << cost << "\n";
				return 0;
			}
			if (mx[node] != cost)
				continue;
			for (const auto &it : G[node])
				if (max(cost, it.second) < mx[it.first]) {
					mx[it.first] = max(cost, it.second);
					q[mx[it.first]].push_back(it.first);
				}
		}
	return 69;
}