Pagini recente » Cod sursa (job #25873) | Cod sursa (job #2964606) | Cod sursa (job #2973380) | Cod sursa (job #2351456) | Cod sursa (job #169803)
Cod sursa(job #169803)
#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;
}