Pagini recente » Cod sursa (job #1875211) | Cod sursa (job #2790997) | Cod sursa (job #2471370) | Cod sursa (job #2672709) | Cod sursa (job #1768930)
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define DIM 1000000
#define MAXN 250050
#define inf 0x3f3f3f3f
using namespace std;
char s[DIM];
int cursor;
void moveCursor()
{
cursor++;
if (cursor == DIM) {
fread(s, 1, DIM, stdin);
cursor = 0;
}
}
int getInt()
{
while (!(s[cursor] >= '0' && s[cursor] <= '9'))
moveCursor();
int nr = 0;
while (s[cursor] >= '0' && s[cursor] <= '9') {
nr = nr*10 + s[cursor] - '0';
moveCursor();
}
return nr;
}
int n, m, x, y;
vector<pair<int, int> > graf[MAXN];
queue<int> cozi[1001];
int best[MAXN];
void citire()
{
n = getInt();
m = getInt();
x = getInt();
y = getInt();
for (int i = 1; i <= m; i++) {
int x = getInt();
int y = getInt();
int k = getInt();
graf[x].push_back(make_pair(y, k));
}
for (int i = 1; i <= n; i++)
best[i] = 1000;
}
int solve()
{
cozi[0].push(x);
best[x] = 0;
for (int crt = 0; crt <= 1000; ) {
if (cozi[crt].empty()) {
crt++;
continue;
}
int top = cozi[crt].front();
if (top == y)
return crt;
cozi[crt].pop();
if (best[top] != crt) continue;
for (pair<int, int> p : graf[top]) {
if (max(best[top], p.second) < best[p.first]) {
best[p.first] = max(best[top], p.second);
cozi[best[p.first]].push(p.first);
}
}
}
}
int main()
{
freopen("pscnv.in", "r", stdin);
freopen("pscnv.out", "w", stdout);
fread(s, 1, DIM, stdin);
citire();
int rez = solve();
printf("%d ", rez);
return 0;
}