Cod sursa(job #524943)

Utilizator ooctavTuchila Octavian ooctav Data 23 ianuarie 2011 18:05:25
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 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;
int d[NMAX];
char lin[100];
inline int maxim(int a, int b)
{
	if(a > b)
		return a;
	return b;
}

void citi()
{
	scanf("%d%d%d%d\n", &N, &M, &X, &Y);
	FOR(i, 1, M)
	{
		gets(lin);
		int v[4] = {0}, nm = 0;
		for(int k = 0 ; lin[k] && lin[k] != '\n' ; k++)
			if(lin[k] == ' ')
			{
				v[++v[0]] = nm;
				nm = 0;
			}
			else	nm = 10 * nm + lin[k] - '0';
		if(nm)	v[++v[0]] = nm;
			
		int x = v[1], y = v[2], c = v[3];
		Graf[x].pb(mp(y, c));
	}
}

void dj_bucket()
{
	queue<int> Q[KMAX];
	fill(d + 1, d + NMAX, INF);d[X] = 0;
	Q[0].push(X);
	FOR(i, 0, KMAX - 5)
		while(!Q[i].empty())
		{
			int x = Q[i].front();Q[i].pop();
			if(d[x] != i)	continue;
			FORIT(it, Graf[x])
			{
				int e = maxim(d[x], it->ss);
				if(d[it->fs] > e)
				{
					d[it->fs] = e; 
					Q[e].push(it->fs);
				}
			}
		}
}


void scrie()
{
	printf("%d\n", d[Y]);
}

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