Pagini recente » Cod sursa (job #683600) | Cod sursa (job #2301514) | Cod sursa (job #892453) | Cod sursa (job #2896640) | Cod sursa (job #2677553)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <vector>
#define MMAX 500000
#define NMAX 250000
using namespace std;
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
int n, m, x, y, ans, tata[NMAX+10], rang[NMAX+10], i, val1, val2, v1, v2;
struct date
{ int nod1, nod2, cost;
}K[MMAX+10];
vector <pair <int, int> > nod[NMAX+10];
bool mycmp(date a, date b)
{ if(a.cost < b.cost) return 1;
return 0;
}
int findDaddy(int x)
{ v1 = x;
v2 = x;
while(tata[v2] != v2) v2 = tata[v2];
while(tata[x] != x)
{ v1 = tata[x];
tata[x] = v2;
x = v1;
}
return v2;
}
void unite(int x, int y)
{ if(rang[x] < rang[y]) tata[x] = y;
else tata[y] = x;
if(rang[x] == rang[y]) rang[x]++;
}
void dfs(int x, int p, int c)
{ if(x == y)
{ ans = c;
return;
}
for(int i=0; i<nod[x].size(); i++)
if(nod[x][i].first != p) dfs(nod[x][i].first, x, max(c, nod[x][i].second));
}
int main()
{
InParser f("pscnv.in");
freopen("pscnv.out", "w", stdout);
f >> n >> m >> x >> y;
for(i=1; i<=n; i++) tata[i] = i, rang[i] = 1;
for(i=1; i<=m; i++)
{ int nod1, nod2, cost;
f >> K[i].nod1 >> K[i].nod2 >> K[i].cost;
}
sort(K+1, K+m+1, mycmp);
for(i=1; i<=m; i++)
{ val1 = findDaddy(K[i].nod1);
val2 = findDaddy(K[i].nod2);
if(val1 == val2) continue;
nod[K[i].nod1].push_back({K[i].nod2, K[i].cost});
nod[K[i].nod2].push_back({K[i].nod1, K[i].cost});
unite(val1, val2);
}
dfs(x, 0, 0);
printf("%d\n", ans);
return 0;
}