Cod sursa(job #488185)

Utilizator indestructiblecont de teste indestructible Data 27 septembrie 2010 21:43:47
Problema PScNv Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <stdio.h>
#include <vector>
#include <queue>
#include <fstream>
#define NMAX 250005
#define KMAX 1005
#define LMAX 45
#define pb push_back
#define mp make_pair
#define f first
#define s second
using namespace std;
int n,m,a,b,cost[NMAX];
vector < pair<int,int> > A[NMAX];
queue <int> B[KMAX];
ifstream in("pscnv.in");
ofstream out("pscnv.out");
void read()
{
	in>>n>>m>>a>>b;
	int i,x,y,z,poz;
	for (i=1; i<=m; i++)
	{
		in>>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=1; i<=n; i++)
		cost[i]=KMAX;
}
void solve()
{
	int i,j,x,x1,y1,new_list;
	cost[a]=0;
	B[0].push(a);
	for (i=0; i<=1000; i++)
	{
		while (!B[i].empty())
		{
			x=B[i].front();  B[i].pop();
			if (x==b)
				return ;
			if (cost[x]==i)
			{
				for (j=0; j<A[x].size(); j++)
				{
					x1=A[x][j].f; y1=A[x][j].s;
					new_list=max(i,y1);
					if (cost[x1]>new_list)
					{
						cost[x1]=new_list;
						B[new_list].push(x1);
					}
				}
			}
		}
	}
}
int main()
{
	read();
	init();
	solve();
	out<<cost[b]<<'\n';
	return 0;
}