Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.29 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. #define forn( i,n ) for ( int i=0; i<(int)(n); i++ )
  6.  
  7. const int inf = (int)1e9;
  8.  
  9. int n, m, a, b, f[110][110][22], ci, cj, ans;
  10. int cntI[110], cntJ[110], x[22], y[22];
  11.  
  12. void precalc()
  13. {
  14.   for ( int n=1; n<=100; n++ )
  15.     for ( int m=1; m<=100; m++ )
  16.       for ( int k=1; k<=20; k++ )
  17.         if ( k > n*m ) f[n][m][k] = inf;
  18.         else f[n][m][k] = min( f[n-1][m][k-min(k,m)] + m, f[n][m-1][k-min(k,n)] + n );
  19. }
  20.  
  21. void check()
  22. {
  23.   int cur = ci * m + cj * n - ci * cj;
  24.   if ( cur >= a+b ) ans = min( ans, cur );
  25.   else ans = min( ans, cur + f[n-ci][m-cj][a+b-cur] );
  26. }
  27.  
  28. void rec( int i )
  29. {
  30.   if ( i == a ) check();
  31.   else
  32.   {
  33.     if ( cntI[x[i]] == 0 ) ci++;
  34.     cntI[x[i]]++;
  35.     rec( i+1 );
  36.     cntI[x[i]]--;
  37.     if ( cntI[x[i]] == 0 ) ci--;
  38.  
  39.     if ( cntJ[y[i]] == 0 ) cj++;
  40.     cntJ[y[i]]++;
  41.     rec( i+1 );
  42.     cntJ[y[i]]--;
  43.     if ( cntJ[y[i]] == 0 ) cj--;
  44.   }
  45. }
  46.  
  47. void solve()
  48. {
  49.   scanf( "%d %d %d %d", &n, &m, &a, &b );
  50.   forn( i, a ) scanf( "%d %d %*d", &x[i], &y[i] );
  51.  
  52.   forn( i, n ) cntI[i] = 0;
  53.   forn( j, m ) cntJ[j] = 0;
  54.  
  55.   ans = inf;
  56.   ci = 0, cj = 0;
  57.   rec( 0 );
  58.   printf( "%d\n", ans );
  59. }
  60.  
  61. int main()
  62. {
  63.   precalc();
  64.  
  65.   int tc;
  66.   scanf( "%d", &tc );
  67.   while ( tc-- ) solve();
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement