Pagini recente » Cod sursa (job #3242548) | Cod sursa (job #1165019) | Cod sursa (job #2568289) | Cod sursa (job #2035870) | Cod sursa (job #1135321)
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <sstream>
using namespace std;
#define DIM 5000000
char buff[DIM];
int poz = 0;
inline int nextInt() {
int numar = 0;
while (buff[poz] < '0' || buff[poz] > '9')
if (++poz == DIM)
fread(buff, 1, DIM, stdin), poz = 0;
while ('0' <= buff[poz] && buff[poz] <= '9') {
numar = numar * 10 + buff[poz] - '0';
if (++poz == DIM)
fread(buff, 1, DIM, stdin), poz = 0;
}
return numar;
}
const int nmax = (1 << 18) - 1;
vector< int > graph[nmax];
int main()
{
freopen("pscnv.in", "r", stdin);
freopen("pscnv.out", "w", stdout);
int n, m, x, y;
queue<int> q;
vector<bool> visited;
n = nextInt(); m = nextInt(); x = nextInt(); y = nextInt();
x--;
y--;
visited.resize(n);
for (int a, b, c; m; m--) {
a = nextInt(); b = nextInt(); c = nextInt();
a--;
b--;
graph[a].push_back(b | c << 18);
}
int l = 1, r = 1000;
while (l <= r) {
int mid = (l + r) / 2;
fill(visited.begin(), visited.end(),false);
visited[x] = true;
q.push(x);
while (!q.empty() && !visited[y]) {
int v = q.front();
q.pop();
for (const int& w : graph[v]) {
if ( (w >> 18) <= mid && !visited[w & nmax]) {
visited[w & nmax] = true;
q.push(w & nmax);
}
}
}
while (!q.empty()) q.pop();
if (visited[y]) {
r = mid - 1;
} else {
l = mid + 1;
}
}
printf("%d\n", l);
return 0;
}