USACO 2.3 Controlling Companies

USACO 2.3 Controlling Companies

每加一个新结点,更新一下对各公司控制值,递归地加入新控制的结点即可。用一个布尔向量记录一下已经控制的公司,以免无穷递归。



#include  < iostream >
#include 
< fstream >

using   namespace  std;

ifstream fin(
" concom.in " );
ofstream fout(
" concom.out " );

#ifdef _DEBUG
#define  out cout
#define  in cin
#else
#define  out fout
#define  in fin
#endif

int  stock[ 101 ][ 101 ];
int  n;
bool  control[ 101 ];

void  add_control( int  cr, int  ce)
{
    
if (control[ce])  return ;

    control[ce] 
=   true ;
    
for ( int  i  =   1 ; i <= 100 ++ i){
        
if (i != cr &&! control[i]){
            stock[cr][i]
+= stock[ce][i];

            
if (stock[cr][i] > 50 )
                add_control(cr,i);
        }
    }
}

void  solve()
{
    memset(stock,
0 , sizeof (stock));
    
    
in >> n;

    
int  i,j,p;

    
while (n -- ){
        
in >> i >> j >> p;
        stock[i][j] 
=  p;
    }
    

   
for ( int  i = 1 ;i <= 100 ; ++ i){

       memset(control,
0 , sizeof (control));

       
for ( int  j = 1 ;j <= 100 ; ++ j){

           
if (i == j)  continue ;

           
if ( stock[i][j] > 50 ){
               add_control(i,j);
           }
       }

       
for ( int  j = 1 ;j <= 100 ; ++ j){
           
if (control[j]){
               
out << i << '   ' << j << endl;
           }
       }
   } 
}

int  main( int  argc, char   * argv[])
{
    solve(); 
    
return   0 ;
}



你可能感兴趣的:(USACO 2.3 Controlling Companies)