Cod sursa(job #488184)

Utilizator indestructiblecont de teste indestructible Data 27 septembrie 2010 21:38:12
Problema PScNv Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 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];
char line[LMAX];
queue <int> B[KMAX];
inline int cif(char x)
{
	return x>='0' && x<='9';
}
void read()
{
	scanf("%d%d%d%d\n",&n,&m,&a,&b);
	int i,x,y,z,poz;
	for (i=1; i<=m; i++)
	{
		fgets(line+1,LMAX,stdin);
		poz=x=y=z=0;
		while (cif(line[poz+1]))
		{
			poz++;
			x=x*10+line[poz]-'0';
		}
		while (!cif(line[poz+1])) poz++;
		while (cif(line[poz+1]))
		{
			poz++;
			y=y*10+line[poz]-'0';
		}
		while (!cif(line[poz+1])) poz++;
		while (cif(line[poz+1]))
		{
			poz++;
			z=z*10+line[poz]-'0';
		}
		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()
{
	freopen("pscnv.in","r",stdin);
	freopen("pscnv.out","w",stdout);
	read();
	init();
	solve();
	printf("%d\n",cost[b]);
	return 0;
}