Cod sursa(job #1005361)

Utilizator superman_01Avramescu Cristian superman_01 Data 4 octombrie 2013 21:16:12
Problema Radiatie Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <fstream>
#include <vector>
#include <algorithm>
 
 
#define MAX_SIZE 100005
#define QMAX 1005
#define SZ 3500000
 
using namespace std;
 
ifstream in ( "radiatie.in" );
ofstream out ( "radiatie.out" );
 
struct Edge {
   int x , y , c;
     inline bool operator() ( const Edge &a , const Edge &b ){
     return a.c < b. c ;
     }
};
 
int TT[MAX_SIZE];
Edge E[MAX_SIZE];
int N , M, Answer , Q ;
int Cost[QMAX];
pair < int , int > APM [QMAX];
bool used[QMAX];

char input[SZ],*ini=input;
  
inline int atoi()
{
    int nr=0;
    for(;!(*ini>='0' && *ini<='9');++ini);
    for(;*ini>='0' && *ini<='9';++ini)
        nr=nr*10+(*ini-'0');
    return nr;
}
 
inline int Find ( int x )
{
    int R , y = x;
    for( R= TT[x] ; R!= TT[R] ; R= TT[R] );
    for ( ; x != R ; y = TT[x] , TT[x] = R , x= y );
    return R;
}
 
int main ( void )
{
    int i , j ;
	in.read(input, SZ);
    N = atoi();
	M = atoi() ;
	Q = atoi();
    for ( i = 1 ; i <= M ; ++i )
       { 
		   E[i].x = atoi();
		   E[i].y = atoi();
		   E[i].c = atoi();
	}
	
    for ( i = 1 ; i <= N ; TT[i] = i , ++i ) ;
	for ( i = 1 ; i <= Q ; ++i )
		{
			APM[i].first  = atoi () ;
			APM[i].second = atoi () ;
	}
    sort ( E +1 , E+ M + 1 , Edge() ) ;
    for ( i = 1 ; i <= M ; ++i )
    {
       int x = Find( E[i].x ) , y = Find( E[i].y) ;
       if ( x != y )
       {
           Answer += E[i].c;
           TT[x] = y ;
           
       }
	   for ( j = 1 ; j <= Q ; ++j )
		   if ( used[j] == false )
		   {
		   if ( Find(APM[j].first) == Find(APM[j].second)   )
		   {
			   used[j] =  true ;
			   Cost[j]=(E[i].c - ( E[i].c-1 > 0 ) );
		   }
		   }
			   
                    
    }
	for ( i = 1 ; i <= Q ; ++i )
		out << Cost[i] +1 << "\n";
    in.close();
    out.close();
    return 0;
     
     
}