portal:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1345
Http://codeforces.com/gym/100803/attachments a problem
Good question!
No more pits, more difficult to cut into
The idea at the outset was violence, because the obtained is the smallest solution and this graph is too large without boundaries, so can not DFS, first think of BFS
Solution 1 Bfs+stl Queue
1#include <iostream>2#include <algorithm>3#include <Set>4#include <cstdio>5#include <cstdlib>6#include <cmath>7#include <queue>8 using namespacestd;9 #definefor (i,j,k) for (int i=j;i<=k;i++)Ten #defineFORD (i,j,k) for (int i=j;i>=k;i--) One #defineLL Long Long A #defineSZ (x) x.size () - intvis[40000]; - intX,k,n,m,s,ans,start,end1,end2,dight; the intBFS () - { -queue<int>q1,q2; - Q1.push (start); +Q2.push (1); -vis[start]=1; + while(Q1.front ()!=end1&&q1.front ()! =end2) A { at intFr=Q1.front (); - intfv=Q2.front (); -for (I,0, N-2) - { - if((fr& (1<<i)) &&! (fr& (1<< (i+1)))) - { inans=fr+ (1<<i); - if(!vis[ans]) {vis[ans]=1; Q1.push (ans); Q2.push (fv+1);} to } + Else if(! (fr& (1<<i) && (fr& (1<< (i+1)))) - { theans=fr-(1<<i); * if(!vis[ans]) {vis[ans]=1; Q1.push (ans); Q2.push (fv+1);} $ }Panax Notoginseng } - Q1.pop (); the Q2.pop (); + } A returnQ2.front ()-1; the } + intMain () - { $Cin>>n>>m; $dight=1; -FORD (i,n-1,0) - { theCin>>x; -start+=x<<i;Wuyi } the intC=N; -for (I,1, M) Wu { -Cin>>x; AboutFor (J,1, X) $ { -c--; -end1+=dight<<C; -end2+= (1-dight) <<C; A } +dight^=1; the } -COUT<<BFS () <<Endl; $ return 0; the}
the code of the egg ache
Solution 2 bfs+ Handwriting Queue
1#include <iostream>2#include <algorithm>3#include <Set>4#include <cstdio>5#include <cstdlib>6#include <cmath>7 //#include <queue>8 using namespacestd;9 #definefor (i,j,k) for (int i=j;i<=k;i++)Ten #defineFORD (i,j,k) for (int i=j;i>=k;i--) One #defineLL Long Long A #defineSZ (x) x.size () - intvis[40000]; - intX,k,n,m,s,ans,start,end1,end2,dight; the - intBFS () - { - intq1[40000];intl1=1;intr1=0; + intq2[40000];intL2=1;intR2=0; -q1[++r1]=start; +q2[++r2]=1; Avis[start]=1; at while(q1[l1]!=end1&&q1[l1]!=end2) - { - intFr=Q1[L1]; - intfv=Q2[L2]; -for (I,0, N-2) - { in if((fr& (1<<i)) &&! (fr& (1<< (i+1)))) - { toans=fr+ (1<<i); + if(!vis[ans]) {vis[ans]=1; q1[++r1]=ans;q2[++r2]=fv+1;} - } the Else if(! (fr& (1<<i) && (fr& (1<< (i+1)))) * { $ans=fr-(1<<i);Panax Notoginseng if(!vis[ans]) {vis[ans]=1; q1[++r1]=ans;q2[++r2]=fv+1;} - } the } +l1++; Al2++; the } + returnq2[l2]-1; - } $ intMain () $ { -Cin>>n>>m; -dight=1; theFORD (i,n-1,0) - {WuyiCin>>x; thestart+=x<<i; - } Wu intC=N; -for (I,1, M) About { $Cin>>x; -For (J,1, X) - { -c--; Aend1+=dight<<C; +end2+= (1-dight) <<C; the } -dight^=1; $ } theCOUT<<BFS () <<Endl; the return 0; the}
the same Code
Comparing the same case with the STL queue and the handwritten queue, I found that the STL's queue memory is 4KB more than handwritten
This is a serious problem, because the StL often pops element, is dynamic, its memory must be small, but even in the case of handwritten queue fixed size of 8e5 int, still more 4KB, that is, 1e3 int
The STL's queue can theoretically have a maximum of 16,384 elements in this topic, so in fact the equivalent length of the queue memory equivalent to 3 to 4 times times the same int
Therefore, we should be careful to use the STL, for the sake of insurance, with the queue when the limit capacity of the data input is not more than 1e7, that is, 1/10int in 32768KB capacity around
Black technology using bit arithmetic in the code of Solution 1/2
FORD (i,n-1,0) {cin>>x; Start+=x<<i;}intC=N; For (I,1, M) {cin>>x; For (J,1, X) {C--; End1+=dight<<C; End2+=(1-dight) <<C; } dight^=1;}/// if((fr& (1<<i)) &&! (fr& (1<< (i+1))) {ans=fr+ (1<<i); if(!vis[ans]) {vis[ans]=1; q1[++r1]=ans;q2[++r2]=fv+1;} } Else if(! (fr& (1<<i) && (fr& (1<< (i+1))) {ans=fr-(1<<i); if(!vis[ans]) {vis[ans]=1; q1[++r1]=ans;q2[++r2]=fv+1;} }
a magical approach
Among them (fr& (1<<i)) &&! (fr& (1<< (i+1))) The most wonderful
fr& (1<<i) indicates whether the bit I in the binary representation of FR is 1 (using a mechanism that is not 0 true for C + +)
The reason for using black technology is that it doesn't make sense to exchange the same bits in this question.
So just swap 01 and 10来 generate the current number of neighbors
Solution 3 (2) Greed
Coderforces Gym 100803a/aizu 1345/CSU 1536/uvalive 6832 Bit String reordering (not finished)