Cod sursa(job #490670)

Utilizator mottyMatei-Dan Epure motty Data 7 octombrie 2010 12:55:12
Problema PScNv Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
//pscnv
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<deque>

using namespace std;

const int N=250001;
const int M=1001;

int n, m, x, y, up, down, cs[N];

bool viz[N];

vector <int> g[N], c[N];

deque <int> q[M];

inline int mx(int a, int b)
{
	return (a>b ? a:b);
}

void Read()
{
	int a, b, cost;

	scanf("%d%d%d%d",&n,&m,&x,&y);

	cs[x]=0;
	
	q[0].push_back(x);
	
	while(m--)
	{
		scanf("%d%d%d",&a,&b,&cost);
		g[a].push_back(b);
		c[a].push_back(cost);
	}
}

void Check( int z, int lev)
{
	int sz=g[z].size(), aux;
	
	viz[z]=1;
	
	if(z==y)
	{
		printf("%d\n",lev);
		exit(0);
	}
	
	for( int i=0; i<sz; ++i)
		if(!viz[g[z][i]])
		{
			aux=g[z][i];
			cs[aux]=mx( cs[z], c[z][i]);
			if(cs[aux]>up)
				up=cs[aux];
			q[cs[aux]].push_back(aux);
		}
}

void Solve()
{
	int z;
	
	while(down<=up)
	{
		while(!q[down].empty())
		{
			z=q[down].front();
			if(!viz[z])
				Check( z, down);
			q[down].pop_front();
		}
		++down;
	}
}

int main()
{
	freopen("pscnv.in","r",stdin);
	freopen("pscnv.out","w",stdout);
	
	Read();
	Solve();
	
	return 0;
}