Pagini recente » Cod sursa (job #229962) | Cod sursa (job #2145745) | Cod sursa (job #2890416) | Cod sursa (job #690396) | Cod sursa (job #2065272)
/**
* Worg
*/
#include <queue>
#include <cstdio>
#include <vector>
#include <algorithm>
FILE *fin = freopen("pscnv.in", "r", stdin); FILE *fout = freopen("pscnv.out", "w", stdout);
const int kMaxK = 1000;
/*-------- Data --------*/
int N, M, X, Y;
std::vector<std::pair<int, int > > edges[kMaxK + 1];
std::vector<std::vector<std::pair<int, int > > > graph;
std::queue<int > list[kMaxK + 1];
/*-------- --------*/
int main() {
scanf("%d%d%d%d", &N, &M, &X, &Y); X--; Y--;
for(int i = 1; i <= M; i++) {
int x, y, k; scanf("%d%d%d", &x, &y, &k);
x--; y--;
edges[k].emplace_back(x, y);
}
graph.resize(N);
for(int i = 1; i <= kMaxK; i++) {
for(auto& p : edges[i]) {
graph[p.first].emplace_back(p.second, i);
graph[p.second].emplace_back(p.first, i);
}
}
std::vector<int > best(N, -1);
list[0].push(X);
int cursor = 0;
while(cursor <= kMaxK) {
if(list[cursor].empty()) {
cursor ++; continue;
}
int node = list[cursor].front(); list[cursor].pop();
if(best[node] != -1) continue;
if(node == Y) {
printf("%d\n", cursor); return 0;
}
best[node] = cursor;
for(auto& edge : graph[node]) {
list[std::max(cursor, edge.second)].push(edge.first);
}
}
return 0;
}