Cod sursa(job #524925)

Utilizator ooctavTuchila Octavian ooctav Data 23 ianuarie 2011 16:51:39
Problema PScNv Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<deque>
#include<queue>
#include<set>
#include<vector>
using namespace std;

#define II inline
#define LL long long
#define mp make_pair
#define fs first
#define ss second
#define pb push_back
#define FOR(i, a, b) 	for(int i = a ; i <= b ; i++)
#define FORIT(it, V) 	for(vector< pair<int, int> > :: iterator it = V.begin() ; it != V.end() ; it++)
#define all(a) a, a + NMAX

const char IN[] = {"pscnv.in"};
const char OUT[] = {"pscnv.out"};
const int INF = 1000000005;
const int NMAX = 250005;
const int KMAX = 1005;

int N, M, X, Y;
vector< pair<int, int> >Graf[NMAX];
int REZ = 0;

void citi()
{
	scanf("%d%d%d%d", &N, &M, &X, &Y);
	FOR(i, 1, M)
	{
		int x, y, c;
		scanf("%d%d%d", &x, &y, &c);
		Graf[x].pb(mp(y, c));
		Graf[y].pb(mp(x, c));
	}
}

bool ok(int K)
{
	bool viz[NMAX] = {0};
	queue<int> Q;
	viz[X] = 1;
	Q.push(X);
	while(!Q.empty())
	{
		int x = Q.front();
		Q.pop();
		FORIT(it, Graf[x])
			if(!viz[it->fs] && it->ss <= K)
			{
				if(it->fs== Y)	return 1;
				viz[it->fs] = 1;
				Q.push(it->fs);
			}
	}
	return 0;
}

int caut_bin()
{
	int pas;
	for(pas = 1 ; pas < KMAX ; pas <<= 1);
	for(; pas ; pas >>= 1)
		if(REZ + pas <= N && !ok(REZ + pas))
			REZ += pas;
	return REZ + 1;
}

void scrie()
{
	printf("%d\n", caut_bin());
}

int main()
{
	freopen(IN, "r", stdin);
	freopen(OUT, "w", stdout);
	citi();
	scrie();
	return 0;
}