Pagini recente » Cod sursa (job #987743) | Cod sursa (job #1451846) | Cod sursa (job #2133832) | Cod sursa (job #247339) | Cod sursa (job #1007625)
#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] = 0;
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] > dist[node] + p->cost)
{
dist[p->inf] = dist[node] + p->cost;
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;
}