Pagini recente » Cod sursa (job #875814) | Cod sursa (job #1990802) | Cod sursa (job #473385) | Cod sursa (job #653648) | Cod sursa (job #1282589)
#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, d[NMAX];
bool vis[NMAX];
queue <int> q[1005];
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, node;
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));
}
q[0].push(x);
for(lim = 0; lim <= 1000; ++ lim)
while(!q[lim].empty()) {
node = q[lim].front();
q[lim].pop();
if(node == y) {
printf("%d\n", lim);
return 0;
}
if(vis[node])
continue;
for(ITERATOR it = graph[node].begin(); it != graph[node].end(); ++ it)
if(d[it->node] > max(d[node], it->cost) || (d[it->node] == 0 && it->node != x)) {
d[it->node] = max(d[node], it->cost);
q[d[it->node]].push(it->node);
}
}
return 0;
}