Guest User

Untitled

a guest
Sep 11th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. //WA
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <map>
  8. #include <set>
  9. #include <algorithm>
  10. #include <queue>
  11. #include <stack>
  12. #include <sstream>
  13. #include <ctime>
  14.  
  15. using namespace std;
  16.  
  17. typedef pair < int, int >     pii;
  18. typedef long long int         i64;
  19. typedef vector < pii >        vpii;
  20. typedef pair < i64, i64 >     pi64;
  21. typedef vector < int >        vi;
  22. typedef vector < vi >         vvi;
  23.  
  24. #define rep(i,a,b)            for(int i=(int)a; i<(int)b; ++i)
  25. #define rep_it(s,a,b)         for(s :: iterator it=a; it!= b; it++)
  26. #define mp                    make_pair
  27. #define pb                    push_back
  28. #define clr(v)                v.clear()
  29. #define mset(a,x)         memset(a, x, sizeof a)
  30. #define all(v)                v.begin(), v.end()
  31. #define err                   1E-8
  32. #define inf                   (1<<29)
  33. #define MAX                   1000
  34.  
  35. //#define inf_4                 0x04040404 //   67372036 ** 27
  36. //#define inf_8                 0x08080808 //  134744072 ** 28
  37. //#define inf_16                0x10101010 //  269488144 ** 29
  38. //#define inf_32                0x20202020 //  538976288 ** 30
  39. //#define inf_64                0x40404040 // 1077952576 ** 31
  40. //#define inf_127               0x7f7f7f7f // 2139062143 ** 31
  41.  
  42. const int mR[4]={1,0,-1,0}, mC[4]={0,1,0,-1};
  43.  
  44. char M[45][21];
  45. int f, mf, S, T, R, C, res[MAX][MAX];
  46. bool taken[45][21];
  47. vi p;
  48.  
  49. void augment(int u, int ed){
  50.    if(u == S) f = ed;
  51.    else if(p[u] != -1){
  52.       augment(p[u], min(res[p[u]][u], ed));
  53.       res[p[u]][u] -= f;
  54.       res[u][p[u]] += f;
  55.    }
  56. }
  57.  
  58. int maxFlow(int N){
  59.    mf = 0;
  60.    int u;
  61.    do{
  62.       f = 0;
  63.  
  64.       p = vi(N, -1);
  65.       vi dist(N, inf);
  66.       queue < int > Q;
  67.       Q.push(S);
  68.       dist[S] = 0;
  69.  
  70.       while(!Q.empty()){
  71.      u = Q.front(); Q.pop();
  72.      if(u == T) break;
  73.      rep(v,0,N) if(res[u][v] > 0 && dist[v]==inf)
  74.         dist[v]=dist[u]+1, p[v]=u, Q.push(v);
  75.       }
  76.       augment(T, inf);
  77.       mf += f;
  78.    }while(f != 0);
  79. }
  80.  
  81. #define can(_i,_j) (_i>=0 && _j>=0 && _i<R && _j<C)
  82.  
  83. int main(){
  84.    int nT, cont;
  85.    bool op;
  86.    int ir, ic, u, v;
  87.    vpii ast;
  88.    map < pii, int > mapa;
  89.    //char str[21];
  90.    string str;
  91.    //scanf("%d\n", &nT);
  92.    cin >> nT;
  93.    while(nT--){
  94.       cont = 1;
  95.       clr(ast); clr(mapa);
  96.       mset(taken, false); mset(res, 0);
  97.  
  98.       //scanf("%d%d\n", &R, &C);
  99.       cin >> R >> C;
  100.       rep(i,0,R){
  101.      //scanf("%s\n", str);
  102.      cin >> str;
  103.      rep(j,0,C) if(str[j] == '*') ast.pb(mp(i,j)), mapa[(mp(i,j))] = cont++;
  104.      rep(j,0,C) M[i][j]=str[j];
  105.       }
  106.       S = 0;
  107.       T = cont;
  108.      
  109.       rep(i,0,ast.size()){
  110.      ir = ast[i].first;
  111.      ic = ast[i].second;
  112.      if(!taken[ir][ic]){
  113.         queue < pii > Q;
  114.         M[ir][ic] = (ir + ic) % 2 == 0 ? 'A' : 'B';
  115.         Q.push(mp(ir, ic));
  116.         while(!Q.empty()){
  117.            ir = Q.front().first;
  118.            ic = Q.front().second;
  119.            Q.pop();
  120.  
  121.            if(taken[ir][ic]) continue;
  122.  
  123.            op = (M[ir][ic] == 'A');
  124.            taken[ir][ic] = true;
  125.            u = mapa[mp(ir, ic)];
  126.  
  127.            if(op) res[S][u] = 1;
  128.            else res[u][T] = 1;
  129.  
  130.            rep(m,0,4) if(can(ir + mR[m], ic + mC[m]) && M[ir + mR[m]][ic + mC[m]] == '*'){
  131.           M[ir + mR[m]][ic + mC[m]] = op ? 'B' : 'A';
  132.           Q.push(mp(ir + mR[m], ic + mC[m]));
  133.          
  134.           v = mapa[mp(ir + mR[m], ic + mC[m])];
  135.           if(op) res[u][v] = 1;
  136.           else res[v][u] = 1;
  137.            }
  138.         }
  139.      }
  140.       }
  141.  
  142.       //rep(i,0,R){rep(j,0,C) cout << M[i][j]; cout << endl;}
  143.  
  144.       maxFlow(T+1);
  145.       //printf("%d\n", cont - 1 - mf);
  146.       cout << (cont - 1 - mf) << endl;
  147.    }
  148.    return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment