Cod sursa(job #1007622)

Utilizator costi23Constantin Radu costi23 Data 9 octombrie 2013 12:57:12
Problema PScNv Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Nmax 250001
#define Mmax 500001
#define INF 1000001

struct nod 
{
	int inf;
	int cost;
	struct nod* next;
} *G[Nmax];
int n, m;
int queue[Mmax];
int dist[Nmax];
int start, end;

void add(int x, int y, int c)
{
	struct nod *aux = (struct nod*)malloc(sizeof(struct nod));
	aux->inf = y;
	aux->cost = c;
	aux->next = G[x];
	G[x] = aux;
}

int isPossible(int x)
{
	int i;
	for (i = 0 ; i <= n;++i)
		dist[i] = INF;
	dist[start] = 1;
	int left = 0;
	int right = 1;
	queue[0] = start;
	while (left <= right)
	{
		int node = queue[left];
		++left;
		struct nod *p;
		for (p = G[node]; p; p=p->next) {
			if (p->cost <= x && dist[p->inf] == INF)
			{
				dist[p->inf] = 1;
				queue[right++] = p->inf;
				if (p->inf == end) return 1;
			}
		}
	}
	if (dist[end] == INF) return 0;
	return 1;
}

int solve()
{
	int left = 0, right = 1000;
	while (left <= right)
	{
		int mid = (left+right) / 2;
		if (isPossible(mid))
			right = mid - 1;
		else left = mid + 1;
	}
	if (isPossible(right)) return right;
	else return left;
}


int main()
{
	int x,y,c,i;
	freopen("pscnv.in","r",stdin);
	freopen("pscnv.out","w",stdout);
	scanf("%d %d %d %d", &n,&m, &start, &end);
	for (i = 0; i < m ; ++i)
	{
		scanf("%d %d %d", &x, &y, &c);
		add(x,y,c);
	}
	printf("%d\n",solve());
	return 0;
}