Cod sursa(job #616696)

Utilizator thesilverhand13FII Florea Toma Eduard thesilverhand13 Data 13 octombrie 2011 07:28:17
Problema Radiatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.82 kb

 
 # include <fstream>
 # include <algorithm>
 # include <vector>

 # define dim 15002

 # define pb push_back
 
 using namespace std;

 ifstream f("radiatie.in");
 ofstream g("radiatie.out");

 struct apm
 {
	 int n1, n2, cost;
 };
 
 struct laborator
 {
	 int nod, cost;
 };

 apm a[ dim ];

 int cmp( apm a, apm b )
 {
	 return a.cost < b.cost;
 }

 int n, m, k;
 int t[ dim ], amin[ dim ], viz[ dim ], M[ dim ], C[ dim ], r[ dim ];
 int sol, cnt = 0;
 
 void conex();

 void citire()
 {
	 int i, x, y;
 
	 f >> n >> m >> k;
	 
     for ( i = 1 ; i <= m ; i++ )
		 
		 f >> a[ i ].n1 >> a[ i ].n2 >> a[ i ].cost;
	 
	 conex();
	 apm();
	 
	 for ( i = 1 ; i <= k ; i++ )
	 {
		 f >> x >> y;
		  sol = 0;
		  
		  while ( x != t[ x ] )
		  {
			  cnt ++;
			  viz[ x ] = cnt; 
			  M[ x ] = 0;
			  
			  if ( sol < C[ x ] )
				  sol = C[ x ];
			  
			  M[ t[ x ] ] = sol;
			  viz[ t[ x ] ] = cnt;
			  
			  x = t[ x ];
		  }
		  
		  while ( viz[ y ] != cnt )
		  {
			  sol = 0;
			  
			  if ( sol < C[ y ] )
				  sol = C[ y ];
			  y = t[ y ];
		  }
		  
		  if ( sol < M[ y ] )
			  sol = M[ y ];
		  g << sol << "\n";
	 }
	 
 
 }
 
 int cauta( int x )
 {
    for (; x != t[x]; x = t[x]);
    return x;
 }

 void conex()
 {
	 int i;
	 for ( i = 1 ; i <= n ; i++ )
		 t[ i ] = i;
 }
 
 
 void leaga( int x, int y, int z )
 {
	 if ( r[ x ] < r[ y ] )
	 {
		 t[ x ] = y;
		 C[ x ] = z;
	 }
	 else
	 {
		 t[ y ] = x;
		 C[ y ] = z;
	 }
	 
	 if ( r[ x ] == r[ y ] )
		 ++r[ x ];
 }

 void apm()
 {
	 int i, cnt = 0, m1, m2;
	 sort( a + 1, a + m + 1, cmp );
	
	 for ( i = 1 ; i <= m  ; i++ )
	 {
		 m1 = cauta( a[ i ].n1 );
		 m2 = cauta( a[ i ].n2 );
		 
		 if ( m1 != m2 )
			 leaga( m1, m2, a[ i ].cost );
	 }
 }
 
 

 int main()
 {
	 citire();
	 return 0;
 }