Guest User

Untitled

a guest
Apr 7th, 2012
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <bitset>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. const int MAXN=16*16+1;
  8.  
  9. bitset<100000> used;
  10. bool A[16][16],B[16][16];
  11. int pi[MAXN][2];
  12. int c[MAXN][MAXN];
  13. int match[MAXN];
  14. const int INF=1000000000;
  15.  
  16. int N;
  17. int cnt1=0,cnt2=0;
  18.  
  19. void init(){
  20.     for (int i=0;i<N;i++){
  21.         int a; cin>>a;
  22.         for (int j=0;j<12;j++)
  23.             A[i][j]=((a&(1<<j))!=0);
  24.     }
  25.     for (int i=0;i<N;i++){
  26.         int a; cin>>a;
  27.         for (int j=0;j<12;j++)
  28.             B[i][j]=((a&(1<<j))!=0);
  29.    
  30.     }
  31. }
  32.  
  33. int mabs(int a){
  34.     return a<0 ? -a : a;
  35. }
  36.  
  37. int dest(pair<int,int> a,pair<int,int> b){
  38.     return mabs(a.first-b.first)+mabs(a.second-b.second);
  39. }
  40.  
  41. void to_graph(){
  42.     vector <pair<int,int> > p1,p2;
  43.     for (int i=0;i<N;i++)
  44.     for (int j=0;j<13;j++){
  45.         if (A[i][j]==1)
  46.             p1.push_back(make_pair(i,j));  
  47.     }
  48.     for (int i=0;i<N;i++)
  49.     for (int j=0;j<13;j++){
  50.         if (B[i][j]==1)
  51.             p2.push_back(make_pair(i,j));
  52.     }
  53.     cnt1=p1.size();
  54.     cnt2=p2.size();
  55.     for (int i=0;i<cnt1;i++)
  56.         for (int j=0;j<cnt2;j++){
  57.             c[i][j]=dest(p1[i],p2[j]);
  58.             pi[i][0]=0;
  59.             pi[i][1]=0;
  60.             match[j]=-1;
  61.         }
  62. }
  63.  
  64. bool khun_algorithm(int u){
  65.     if (used[u])
  66.         return false;
  67.     used[u]=true;
  68.     for (int i=0;i<cnt1;i++)
  69.         if (pi[u][0]+pi[i][1]==c[u][i] && (match[i]==-1 || khun_algorithm(match[i]))){
  70.             match[i]=u;
  71.             return true;
  72.         }
  73.     return false;
  74. }
  75.  
  76. int form_delta (int u){
  77.     if (used[u])
  78.         return INF;
  79.     used[u]=true;
  80.     int delta=INF;
  81.     for (int i=0;i<cnt1;i++){
  82.         if (pi[u][0]+pi[i][1]!=c[u][i])
  83.             delta=min(delta,c[u][i]-(pi[u][0]+pi[i][1]));
  84.         else
  85.             delta=min(delta,form_delta(match[i]));
  86.     }
  87.     return delta;
  88. }
  89.  
  90. void graph_upgrade(int u, int delta,int dole=0){
  91.     if (used[u])
  92.         return;
  93.     used[u]=true;
  94.     for (int i=0;i<cnt1;i++){
  95.         if (pi[u][0]+pi[i][1]==c[u][i]){
  96.             if (!used[match[i]]){
  97.                 graph_upgrade(match[i],delta);
  98.                 pi[i][1]-=delta;
  99.             }
  100.         }
  101.     }
  102.     pi[u][0]+=delta;
  103. }
  104.  
  105. int venger_algorithm(){
  106.     for (int i=0;i<cnt1;i++){
  107.         for(;;){
  108.             used.reset();
  109.             if (khun_algorithm(i)) break;
  110.             used.reset();
  111.             int delta=form_delta(i);
  112.             used.reset();
  113.             graph_upgrade(i,delta);
  114.         }
  115.     }
  116.     int ans=0;
  117.     for (int i=0;i<cnt1;i++)
  118.         ans+=c[match[i]][i];
  119.     return ans;
  120. }
  121.  
  122. int main(){
  123.     freopen("input.txt","r",stdin);
  124.     freopen("output.txt","w",stdout);
  125.     while(cin>>N){
  126.         if (N==0) break;
  127.         init();
  128.         to_graph();
  129.         if (cnt1!=cnt2){
  130.             cout<<"Impossible"<<endl;
  131.             continue;
  132.         }
  133.         int ans=venger_algorithm();
  134.         cout<<ans<<endl;
  135.     }
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment