Pagini recente » Cod sursa (job #2150621) | Cod sursa (job #2735174) | Cod sursa (job #369367) | Cod sursa (job #2219693) | Cod sursa (job #108246)
Cod sursa(job #108246)
#include <fstream>
using namespace std;
const int maxn = 250001;
ifstream in("pscnv.in");
ofstream out("pscnv.out");
struct graf
{
int nod;
short cost;
graf *next;
};
int n, m, x, y;
int maxc = -1;
graf *a[maxn];
void add(int to, int nod, short cost)
{
graf *q = new graf;
q->next = a[to];
q->nod = nod;
q->cost = cost;
a[to] = q;
}
void read()
{
in >> n >> m >> x >> y;
int p, q;
short c;
for ( int i = 1; i <= m; ++i )
{
in >> p >> q >> c;
if ( c > maxc )
maxc = c;
if ( p != q )
add(p, q, c);
}
}
struct tail
{
int nod;
tail *next;
};
int nr = 0;
char viz[maxn];
int check(int k)
{
tail *p = new tail;
tail *u = new tail;
p->next = NULL;
p->nod = x;
u = p;
while ( p )
{
graf *q = a[ p->nod ];
while ( q )
{
if ( viz[q->nod] != nr && q->cost <= k )
{
tail *t = new tail;
t->next = NULL;
t->nod = q->nod;
u->next = t;
u = t;
if ( u->nod == y )
return 1;
viz[u->nod] = nr;
}
q = q->next;
}
tail *t = p;
p = p->next;
delete t;
}
return 0;
}
int main()
{
read();
int st = 1, dr = maxc;
int m = 0;
int t = 0;
srand(time(0));
while ( st < dr )
{
m = st + rand() % (dr - st);
++nr;
t = check(m);
if ( t )
dr = m;
else
st = m+1;
}
out << st;
return 0;
}