Pagini recente » Cod sursa (job #1276610) | Cod sursa (job #2305022) | Cod sursa (job #608185) | Cod sursa (job #3001416) | Cod sursa (job #2671205)
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int nmax = 250000;
struct Dest {
int nod, cost;
};
vector <Dest> g[1 + nmax];
int n, m, ss, ff;
bool viz[1 + nmax];
void citire() {
scanf("%d%d%d%d", &n, &m, &ss, &ff);
for(int i = 1; i <= m; i ++) {
int x, y, k;
scanf("%d%d%d", &x, &y, &k);
g[x].push_back({y, k});
}
}
int dfs(int from, int limit) {
viz[from] = 1;
for(auto& to: g[from]) {
if(viz[to.nod] == false && to.cost <= limit) {
dfs(to.nod, limit);
}
}
}
int cautBin() {
int left = 1, right = 1000, last = -1;
while(left <= right) {
int med = (left + right) / 2;
memset(viz, 0, sizeof(viz));
dfs(ss, med);
if(viz[ff] == true) {
right = med - 1;
last = med;
} else {
left = med + 1;
}
}
return last;
}
int main() {
freopen("pscnv.in", "r", stdin);
freopen("pscnv.out", "w", stdout);
citire();
printf("%d", cautBin());
return 0;
}