Pagini recente » Istoria paginii runda/rar23 | Cod sursa (job #136573) | Cod sursa (job #2485011) | Istoria paginii runda/delaceorashimulare | Cod sursa (job #1007622)
#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;
}