Pagini recente » Cod sursa (job #18248) | Cod sursa (job #631623) | Cod sursa (job #722120) | Cod sursa (job #537781) | Cod sursa (job #1457055)
#include <cstdio>
#include <vector>
#include <algorithm>
#define x first
#define y second
#define NMAX 250007
using namespace std;
int H[NMAX], T[NMAX];
int n, m, X, Y;
vector< pair< int, int > > v[1007];
void unite(int x, int y){
if(H[x] == H[y]){
++ H[x];
T[y] = x;
}
else
if(H[x] < H[y])
T[x] = y;
else
T[y] = x;
}
inline int father(int x){
if(x == T[x])
return x;
return father(T[x]);
}
int main(){
freopen("pscnv.in", "r", stdin);
freopen("pscnv.out", "w", stdout);
scanf("%d %d %d %d", &n, &m, &X, &Y);
for(int i = 1; i <= m; ++i){
int a = 0, b = 0, c = 0;
scanf("%d %d %d", &a, &b, &c);
v[c].push_back(make_pair(a, b));
T[i] = i;
H[i] = 1;
}
for(int i = 1; i <= 1000; ++i){
for(vector< pair<int, int> >::iterator it = v[i].begin(); it != v[i].end(); ++it){
int Tx = father(it->x), Ty = father(it->y);
if(Tx != Ty)
unite(Tx, Ty);
}
if(father(X) == father(Y)){
printf("%d", i);
return 0;
}
}
printf("1001\n");
return 0;
}