Pagini recente » Cod sursa (job #1076400) | Cod sursa (job #718706) | Clasament a_short_round | Cod sursa (job #1618915) | Cod sursa (job #530385)
Cod sursa(job #530385)
// kruskal (APM)...
#include<stdio.h>
#include<vector>
#define mp make_pair
#define pb push_back
using namespace std;
FILE *f=fopen("pscnv.in","r");
FILE *g=fopen("pscnv.out","w");
int N,M,X,Y,t[250001];
vector <pair <int,int> > cost[1024];
int tata(int x){
if(t[x]>0)
return tata(t[x]);
return x;
}
int main(){
fscanf(f,"%d%d%d%d",&N,&M,&X,&Y);
for(int i=1;i<=M;++i){
int x,y,z;
fscanf(f,"%d%d%d",&x,&y,&z);
cost[z].pb(mp(x,y));
}
memset(t,0xFF,sizeof(t));
for(int k=1;k<=1000;++k)
for(int i=0;i<cost[k].size();++i){
int t1=tata ( cost[k][i].first );
int t2=tata ( cost[k][i].second);
if( t1 != t2 )
{ if (t[t1]<=t[t2])
t[t1]+=t[t2],t[t2]=t1;
else t[t2]+=t[t1],t[t1]=t2;
}
if(tata(X)==tata(Y))
{fprintf(g,"%d",k);return 0;}
}
return 0;
}