Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<string>
  4. #include<string.h>
  5. #include<stdio.h>
  6. #include<set>
  7. #include<math.h>
  8. #include<algorithm>
  9. #include<iomanip>
  10.  
  11. using namespace std;
  12.  
  13. vector<vector<int> > grid;
  14. int mem[1000][1000];
  15. int n;
  16.  
  17. /*
  18. int setbit(int  mask,int  idx,int  value = 1 ){ return ( value ) ? ( mask | (1<<idx) ) : ( mask &~ (1<<idx) ); }
  19. int getbit(int  mask , int  idx ) { return ( (mask>>idx) & 1 ) == 1; }
  20. int  cntbit( int  mask ) { int ret=0; while(mask) { if(mask%2)  ret++;  mask/=2;  }  return ret; }*/
  21.  
  22. int F(int maskR,int maskC){
  23.  
  24.     if(maskR==((1<<n)-1)&&maskC==((1<<n)-1))  return 0;
  25.  
  26.     int &x=mem[maskR][maskC];
  27.  
  28.     if(x!=-1)  return x;
  29.  
  30.  
  31.     int MAX=-1000000;
  32.     int MIN=1000000;
  33.  
  34.     for(int a=0;a<n;++a){
  35.        
  36.         if(!( maskR & (1 << a) ) ){
  37.  
  38.             //MIN=1000000;
  39.             for(int b=0;b<n;++b){
  40.  
  41.                 if(!(maskC & (1<<b) ) )
  42.                     MIN=min(MIN,grid[a][b]+F(maskR | (1<<a) ,maskC | (1<<b) ));
  43.  
  44.             }
  45.  
  46.         }
  47.  
  48.         MAX=max(MAX,MIN);
  49.  
  50.     }
  51.  
  52.     return x=MAX;
  53.  
  54.  
  55. }
  56.  
  57. int main(){
  58.  
  59.     int t,x;
  60.     vector<int> temp;
  61.     cin>>t;
  62.     for(int i=0;i<t;++i){
  63.  
  64.         memset(mem,-1,sizeof mem);
  65.         grid.clear();
  66.  
  67.         cin>>n;
  68.  
  69.         for(int a=0;a<n;++a){
  70.  
  71.             temp.clear();
  72.             for(int b=0;b<n;++b){
  73.                 cin>>x;
  74.                 temp.push_back(x);
  75.  
  76.             }
  77.             grid.push_back(temp);
  78.  
  79.         }
  80.  
  81.         cout<<F(0,0)<<"\n";
  82.  
  83.     }
  84.  
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement