Cod sursa(job #488167)

Utilizator indestructiblecont de teste indestructible Data 27 septembrie 2010 21:18:22
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <stdio.h>
#include <vector>
#include <queue>
#define NMAX 250005
#define KMAX 1005
#define pb push_back
#define mp make_pair
#define f first
#define s second
using namespace std;
int n,m,a,b,cost[NMAX],lvl[NMAX];
vector < pair<int,int> > A[NMAX];
char viz[NMAX];
queue <int> B[KMAX];
void read()
{
	scanf("%d%d%d%d",&n,&m,&a,&b);
	int i,x,y,z;
	for (i=1; i<=m; i++)
	{
		scanf("%d%d%d",&x,&y,&z);
		A[x].pb(mp(y,z));
		A[y].pb(mp(x,z));
	}
}
inline int max(int x,int y)
{
	return x>y ? x : y;
}
void init()
{
	int i;
	for (i=2; i<=n; i++)
		lvl[i]=KMAX;
}
void solve()
{
	int i,j,x,x1,y1,new_lvl;
	B[0].push(a);
	for (i=0; i<=1000; i++)
	{
		while (!B[i].empty())
		{
			x=B[i].front(); 
			B[i].pop();
			if (viz[x])
				continue ;
			viz[x]=1; cost[x]=i;
			if (viz[b])
				return ;
			for (j=0; j<A[x].size(); j++)
			{
				x1=A[x][j].f; y1=A[x][j].s;
				new_lvl=max(i,y1);
				if (!viz[x1])
					B[new_lvl].push(x1);
			}
		}
	}
}
int main()
{
	freopen("pscnv.in","r",stdin);
	freopen("pscnv.out","w",stdout);
	read();
	init();
	solve();
	printf("%d\n",cost[b]);
	return 0;
}