Pagini recente » Cod sursa (job #2063190) | Cod sursa (job #1693395) | Cod sursa (job #1393159) | Cod sursa (job #1781581) | Cod sursa (job #1282577)
#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
#define ITERATOR vector <EDGE>::iterator
const int NMAX = 250005;
struct EDGE {
int node, cost;
EDGE (int node = 0, int cost = 0) {
this->node = node;
this->cost = cost;
}
};
vector <EDGE> graph[NMAX];
int x, y, lim;
bool vis[NMAX];
bool bfs () {
queue <int> q;
memset(vis, 0, sizeof(vis));
q.push(x);
vis[x] = 1;
while(!q.empty()) {
int node = q.front();
if(node == y) return 1;
q.pop();
for(ITERATOR it = graph[node].begin(); it != graph[node].end(); ++ it)
if(!vis[it->node] && it->cost <= lim) {
vis[it->node] = 1;
q.push(it->node);
}
}
return 0;
}
int bs (int left, int right) {
int mid, last;
while(left <= right) {
mid = (left + right) / 2;
lim = mid;
if(bfs())
right = mid - 1,
last = mid;
else
left = mid + 1;
}
return last;
}
char ln[100];
int p;
void getint(int &x) {
x = 0;
while(ln[p] >= '0' && ln[p] <= '9') {
x = x * 10 + ln[p] - '0';
++ p;
}
++ p;
}
int main() {
freopen("pscnv.in", "r", stdin);
freopen("pscnv.out", "w", stdout);
int n, m, i, c, a, b;
scanf("%d%d%d%d\n", &n, &m, &x, &y);
for(i = 1; i <= m; ++ i) {
gets(ln);
p = 0;
getint(a);
getint(b);
getint(c);
graph[a].push_back(EDGE(b, c));
}
printf("%d\n", bs(1, 1000));
return 0;
}