Advertisement
Alhiris

Untitled

Dec 30th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. #include <climits>
  4. #include <queue>
  5. using namespace std;
  6. ifstream cin("base3.in");
  7. ofstream cout("base3.out");
  8. #define inf 1<<30
  9. const int maxn=20000;
  10.  
  11. queue<int> g;
  12. int lg[maxn],viz[maxn],lung[3];
  13. char numb[maxn];
  14.  
  15. int main()
  16. {
  17.     for(int i=0;i<maxn;++i) lg[i]=inf;
  18.     for(int i=0;i<3;++i){
  19.         cin>>numb;
  20.         lung[i]=strlen(numb);
  21.         for(int k=0;k<lung[i];++k)
  22.             if(numb[k]=='1'){
  23.                 int aux=abs(lung[i]-2*k-1);
  24.                 if(!viz[aux]){
  25.                     lg[aux]=lung[i];
  26.                     viz[aux]=1;
  27.                     g.push(aux);
  28.                 }
  29.             }
  30.     }
  31.     while(!g.empty()){
  32.         int xd=g.front();
  33.         g.pop();
  34.         for(int i=0;i<3;++i){
  35.             int x=abs(xd-lung[i]);
  36.             if(lg[x]>lg[xd]+lung[i]){
  37.                 lg[x]=lg[xd]+lung[i];
  38.                 if(!viz[x])
  39.                     g.push(x),viz[x]=1;
  40.             }
  41.         }
  42.         viz[xd]=0;
  43.     }
  44.     cout<<((lg[0]!=inf)?lg[0]:0);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement