# include # include # include # include #include # include # include using namespace std; int n,a[500]; bool vis[500]; vector > l; queue > l1,l2; int p1,p2; int difpos [500][500]; int difst [500][500]; queue > q; int where[500]; vector > bypos[500][500]; queue > ans; struct popcorn { int n; int st; popcorn *next; }; popcorn *first = new popcorn; popcorn *first1 = new popcorn; void recalculate (int br) { // cout<n; prev1 = first1; u1 = first1->next; while(u1->n!=f) {// cout<n<<" "<n<n][u1->n]n][u1->n]; x1 = prev1; } prev1 = u1; u1 = u1->next; // cout<n][u1->n]n][u1->n]; x1 = prev1; } // cout<<"kooo"<n<<" "<next->n<n][x1->next->n]; l1.push({x1->n,x1->next->n}); swap(x1->st,x1->next->st); u1 = x1->next; x1->next = x1->next->next; if(first1==u1) { first1 = u1->next; } delete u1; recalculate (br-1); } void calculate (int br) { // cout<n; prev = first; u = first->next; while(u->n!=f) {// cout<n<<" "<n<n][u->n]n][u->n]; x = prev; } prev = u; u = u->next; // cout<n][u->n]n][u->n]; x = prev; } p2=p2+ mins + difst[x->n][x->next->n]; // cout<<"kooo"<n<<" "<next->n<n,x->next->n}); swap(x->st,x->next->st); u = x->next; x->next = x->next->next; if(first==u) { first = u->next; } delete u; calculate (br-1); } void makeswaps() { int i,j,x,br; popcorn *u; popcorn *u1; for(i=1;i<=n;i++) { if(vis[i]||a[i]==i)continue; vis[i]=1; x=i; j=a[i]; popcorn *t = new popcorn; first->n = x; first->st = j; t = first; popcorn *t1 = new popcorn; first1->n = x; first1->st = j; t1 = first1; br=1; while(!vis[j]) { u = new popcorn; u1 = new popcorn; t->next = u; t=u; t1->next = u1; t1=u1; vis[j]=1; t->n = j; t->st = a[j]; t->next = new popcorn; t1->n = j; t1->st = a[j]; t1->next = new popcorn; br++; j=a[j]; } t->next = first; t1->next = first1; p1=0; p2=0; calculate(br); recalculate(br); //cout<l){mins=l;k=i;} } if(0==o)return ; trysolmake(k,a[k]); solution(); } void conc(int x, int y) { // cout<x;i--) { if(abs(i-a[i])>abs(y-a[i])) { q.push({i,y}); swap(a[i],a[y]); where[a[i]]=i; where[a[y]]=y; conc(x,i); return ; } } q.push({x,y}); swap(a[x],a[y]); where[a[x]]=x; where[a[y]]=y; return ; } void iftheyare2and0() { int i,j,o=n; for(i=1;i<=n;i++) { if(a[i]==i)continue; conc(i,where[i]); } } void conc22(int x, int y) { // cout<x;i--) { if(abs(y-a[i])+abs(max(a[x],a[i])-min(a[i],a[y]))-abs(a[x]-a[y])<(abs(i-a[i]))+abs(a[i]-a[a[i]])) { q.push({i,y}); swap(a[i],a[y]); where[a[i]]=i; where[a[y]]=y; conc(x,i); return ; } } q.push({x,y}); swap(a[x],a[y]); where[a[x]]=x; where[a[y]]=y; return ; } void iftheyare2and2() { int i,j,o=n; for(i=1;i<=n;i++) { if(a[i]==i)continue; conc22(i,where[i]); } } void make_floid() { int k,i,j,w; for (k = 1; k <= n; k++) for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) if (difpos[i][j] > (difpos[i][k] + difpos[k][j]+difpos[i][k])) { difpos[i][j] = difpos[i][k] + difpos[k][j] + difpos[i][k]; bypos[i][j].clear(); for(w=0;w>n; int i; for(i=1;i<=n;i++) {cin>>a[i]; where[a[i]]=i; } int o,j; cin>>o; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { cin>>difpos[i][j]; bypos[i][j].push_back({i,j}); } int k; cin>>k; if((o==4&&k==0)||(o==4&&k>=3)) { makeswaps(); { cout<>difst[i][j]; if(o==2&&k==0)iftheyare2and0(); else{ if(o==1&&k==0)make_floid(); solution();} //iftheyare2and2(); //cout<