Pagini recente » Cod sursa (job #1670986) | Cod sursa (job #2808179) | Cod sursa (job #1456380) | Cod sursa (job #700342) | Cod sursa (job #2121411)
#include <bits/stdc++.h>
using namespace std;
class InputReader {
private:
static const int BUFF_SIZE = 1 << 17;
int bp;
char buff[BUFF_SIZE];
FILE *fin;
inline void nxt() {
if (++bp == BUFF_SIZE) {
fread(buff, sizeof(char), BUFF_SIZE, fin);
bp = 0;
}
}
public:
InputReader (const char *file_name) {
fin = fopen(file_name, "r");
bp = BUFF_SIZE - 1;
}
inline void close() {
fclose(fin);
}
InputReader& operator >> (int &num) {
num = 0;
while (isdigit(buff[bp]) == 0)
nxt();
while (isdigit(buff[bp])) {
num = num * 10 + buff[bp] - '0';
nxt();
}
return *this;
}
};
const int MAXN = 25e4;
const int MAXK = 1e3;
const int INF = 0x3f3f3f3f;
vector <pair <int, int>> g[MAXN + 1];
int seen[MAXN + 1];
queue <int> q[MAXK + 1];
int main()
{
int n, m, x, y;
InputReader fin("pscnv.in");
fin >> n >> m >> x >> y;
for (int i = 0; i < m; ++i) {
int a, b, c;
fin >> a >> b >> c;
g[a].emplace_back(b, c);
}
fin.close();
int cost = 0;
q[0].push(x);
memset(seen, INF, sizeof seen);
while (seen[y] == INF) {
if (q[cost].empty())
++cost;
else {
int node = q[cost].front();
q[cost].pop();
if (seen[node] == INF) {
seen[node] = cost;
for (auto it : g[node])
if (seen[it.first] == INF)
q[max(cost, it.second)].push(it.first);
}
}
}
ofstream fout("pscnv.out");
fout << cost;
fout.close();
return 0;
}