Cod sursa(job #169803)

Utilizator gabitzish1Gabriel Bitis gabitzish1 Data 1 aprilie 2008 23:00:18
Problema PScNv Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <stdio.h>

int n, m, sursa, dest, ok, max, min = 1001, ok2;

typedef struct nod
{
	int x, c;
	nod *a;
} *pNod;
pNod v[250006];

void citire()
{
	freopen("pscnv.in","r",stdin);
	freopen("pscnv.out","w",stdout);

	char s[100];
	scanf("%d %d %d %d\n",&n,&m,&sursa,&dest);

	int i, a, b, c, j;
	pNod q;
	for (i = 1; i <= m; i++)
	{
		fgets(s,100,stdin);
		j = a = b = c = 0;
		while (s[j] <= '9' && s[j] >= '0') {a = a * 10 + s[j] - '0'; j++;}
		j++;
		while (s[j] <= '9' && s[j] >= '0') {b = b * 10 + s[j] - '0'; j++;}
		j++;
		while (s[j] <= '9' && s[j] >= '0') {c = c * 10 + s[j] - '0'; j++;}
		
		q = new nod;
		q -> x = b;
		q -> c = c;
		q -> a = v[a];
		v[a] = q;
		if (c > max) max = c;
		if (c < min) min = c;
	}
}

void DF(int nod, int d)
{
	pNod p;
	if (nod == dest) ok = 1; 
	if (ok && ok2) return;
	for (p = v[nod]; p != NULL; p = p -> a)
	{
		if (p -> c <= d) 
		{
			if (p -> c == d) ok2 = 1;
			DF(p -> x, d);
		}
	}	
}

void BF(int d)
{
	int p , u, c[250005];
	pNod q;
	p = u = 1;
	c[1] = sursa;
	while (p <= u)
	{
		if (ok && ok2) return;
		for (q = v[ c[p] ]; q != NULL; q = q -> a)
			if (q -> c <= d)
			{
				if (q -> x == dest) { ok = 1; return; }
				c[++u] = q -> x;
				if (q -> c == d) ok2 = 1;			
			}
		p++;
	}
}


int caut()
{
	int p = min, u = max, m;
	m = (p + u) / 2;

	while (p <= u)
	{
		ok = ok2 = 0;
		BF(m);
		if (ok)
		{
			if (ok2) return m;
			else u = m - 1;
		}
		else p = m + 1;
		m = (p + u) / 2;
	}
	return 0;
}

int main()
{
	citire();
	printf("%d\n",caut());
	return 0;
}