Pagini recente » Cod sursa (job #1142243) | Cod sursa (job #3221755) | Cod sursa (job #883260) | Cod sursa (job #402244) | Cod sursa (job #1951302)
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#define nmax 250005
using namespace std;
class instream {
public:
instream() {}
instream(const char *file_name) {
input_file=fopen(file_name,"r");
cursor=0;
fread(buffer,SIZE,1,input_file);
}
inline instream &operator >>(int &n) {
while((buffer[cursor]<'0'||buffer[cursor]>'9')&&buffer[cursor]!='-') {
advance();
}
int semn=1;
if (buffer[cursor]=='-')
semn=-1,advance();
n=0;
while('0'<=buffer[cursor]&&buffer[cursor]<='9') {
n=n*10+buffer[cursor]-'0';
advance();
}
n*=semn;
return *this;
}
private:
FILE *input_file;
static const int SIZE=1<<15;
int cursor;
char buffer[SIZE];
inline void advance() {
++ cursor;
if(cursor==SIZE) {
cursor=0;
fread(buffer,SIZE,1,input_file);
}
}
};
instream f("pscnv.in");
ofstream g("pscnv.out");
struct muchie{int a;int b;int c;} v[nmax*2];
int n,m,k,x,y,t[nmax];
int sortcomp(const muchie &x,const muchie &y)
{
return x.c<y.c;
}
int tata(int x)
{
int y=x,z;
while (t[x]>0)
x=t[x];
while (t[y]>0) {
z=t[x];
t[y]=x;
y=z;
}
return x;
}
int main()
{
int i,j,l,p;
f>>n>>m>>x>>y;
if (x==y) {
g<<1<<'\n';
return 0;
}
for (i=1;i<=m;i++)
f>>v[i].a>>v[i].b>>v[i].c;
sort(v+1,v+m+1,sortcomp);
for (i=1;i<=n;i++)
t[i]=-1;
for (i=1;i<=m;i++) {
if (tata(v[i].a)!=tata(v[i].b))
t[v[i].a]=v[i].b;
if (tata(x)==tata(y)) {
g<<v[i].c<<'\n';
return 0;
}
}
return 0;
}