Pagini recente » Cod sursa (job #144174) | Cod sursa (job #3135837) | Cod sursa (job #1320392) | Cod sursa (job #2593471) | Cod sursa (job #1457065)
#include <cstdio>
#include <fstream>
#include <vector>
#include <algorithm>
#define x first
#define y second
#define NMAX 250007
const int BUFFER_SIZE = 1 << 16;
using namespace std;
int H[NMAX], T[NMAX];
int n, m, X, Y;
vector< pair< int, int > > v[1007];
class InputReader {
public:
InputReader() {}
InputReader(const char* filename) {
file = fopen(filename, "r");
cursor = 0;
fread(buffer, BUFFER_SIZE, 1, file);
}
inline InputReader & operator >> (int &n) {
n = 0;
while(!isdigit(buffer[cursor])) {
advance();
}
while(isdigit(buffer[cursor])) {
n = n * 10 + buffer[cursor] - '0';
advance();
}
return *this;
}
private:
int cursor;
char buffer[BUFFER_SIZE];
FILE* file;
inline void advance() {
++ cursor;
if(cursor == BUFFER_SIZE) {
cursor = 0;
fread(buffer, BUFFER_SIZE, 1, file);
}
}
};
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(){
InputReader in("pscnv.in");
freopen("pscnv.out", "w", stdout);
in >> n >> m >> X >> Y;
for(int i = 1; i <= m; ++i){
int a = 0, b = 0, c = 0;
in >> 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;
}