Pagini recente » Cod sursa (job #1995091) | Cod sursa (job #2454890) | Cod sursa (job #2336239) | Cod sursa (job #2237618) | Cod sursa (job #2677551)
#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];
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)
{ int y = x, r = x;
while(tata[r] != r) r = tata[r];
while(tata[x] != x)
{ y = tata[x];
tata[x] = r;
x = y;
}
return r;
}
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(int i=1; i<=n; i++) tata[i] = i, rang[i] = 1;
for(int 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(int i=1; i<=m; i++)
{ int 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;
}