Pagini recente » Cod sursa (job #2078237) | Profil Ovidiu-Antonio | Cod sursa (job #385918) | Profil adelcosma | Cod sursa (job #1640484)
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
using namespace std;
#define pi pair < int, int >
const int N = 250100;
const int KMAX = 1005;
const int BSIZE = 1 << 10;
int n, m;
vector < pi > adj[N];
vector < int > nod_dist[KMAX];
int dmin[N];
char buff[BSIZE];
int parse(int &i) {
int v = 0;
while(isdigit(buff[i])) {
v = v * 10 + buff[i] - '0';
i++;
}
return v;
}
int dijkstra_small(int x, int y) {
int i, j, k, d, nod;
for(i = 1; i <= n; i++) dmin[i] = 0x3fffffff;
dmin[x] = 0;
nod_dist[0].push_back(x);
for(i = 0; i < KMAX; i++) {
for(j = 0; j < nod_dist[i].size(); j++) {
if(dmin[nod_dist[i][j]] < i) continue;
for(k = 0; k < adj[nod_dist[i][j]].size(); k++) {
nod = adj[nod_dist[i][j]][k].first;
d = max(i, adj[nod_dist[i][j]][k].second);
if(dmin[nod] <= d) continue;
dmin[nod] = d;
nod_dist[d].push_back(nod);
}
}
}
return dmin[y];
}
int main() {
freopen("pscnv.in", "r", stdin);
freopen("pscnv.out", "w", stdout);
int x, y, i, j, a, b, c;
scanf("%d %d %d %d\n", &n, &m, &x, &y);
for(i = 1; i <= m; i++) {
gets(buff);
a = parse(j = 0);
b = parse(++j);
c = parse(++j);
adj[a].push_back(make_pair(b, c));
}
printf("%d\n", dijkstra_small(x, y));
return 0;
}