Cod sursa(job #1135329)

Utilizator ELHoriaHoria Cretescu ELHoria Data 7 martie 2014 18:10:04
Problema PScNv Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <sstream>
#include <cstring>

using namespace std;

#define DIM 5000000
char buff[DIM];
int poz;

inline int nextInt() {
	int numar = 0;
	while (buff[poz] < '0' || buff[poz] > '9')
	
	if (++poz == DIM)
		fread(buff, 1, DIM, stdin), poz = 0;
     
	while ('0' <= buff[poz] && buff[poz] <= '9') {
		numar = numar * 10 + buff[poz] - '0';
		if (++poz == DIM)
			fread(buff, 1, DIM, stdin), poz = 0;
	}
	return numar;
}

const int nmax = (1 << 18) - 1;
vector< int > graph[nmax];
bool visited[nmax];
int q[nmax];

int main()
{
	freopen("pscnv.in", "r", stdin);
	freopen("pscnv.out", "w", stdout);
	int n, m, x, y;

	n = nextInt(); m = nextInt(); x = nextInt(); y = nextInt();
	x--;
	y--;

	for (int a, b, c; m; m--) {
		a = nextInt(); b = nextInt(); c = nextInt();
		a--;
		b--;
		graph[a].push_back(b | c << 18);
	}

	int l = 1, r = 1000;
	while (l <= r) {
		int mid = (l + r) / 2;
		int p, u;
		visited[x] = true;
		p = u = 0;
		q[u++] = x;
		while (p < u && !visited[y]) {
			int v = q[p++];
			for (const int& w : graph[v]) {
				if ( (w >> 18) <= mid && !visited[w & nmax]) {
					visited[w & nmax] = true;
					q[u++] = w & nmax;
				}
			}
		}

		if (visited[y]) {
			r = mid - 1;
		} else {
			l = mid + 1;
		}

		if (l <= r)
			for (int i = 0; i < u; i++) visited[q[i]] = 0;
	}

	printf("%d\n", l);
	return 0;
}