Cod sursa(job #530385)

Utilizator Cristy94Buleandra Cristian Cristy94 Data 7 februarie 2011 18:05:48
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
// 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;
}