Bzoj 3205: [Apio2013] Robot "dfs+ Steiner Tree +SPFA"

Source: Internet
Author: User

The first time I heard of the Steiner tree.
First DFS preprocessing out dis[i][j][k] means that the lattice (i,j) to the direction of the K transfer can be, memory word search preprocessing, note if there is a ring, special sentence
Set F[i][j][x][y] means that the composite robot i-j (x, y) generation need to push the number of steps, with SPFA transfer, because the time is tight, so optimize the SPFA, put the can transfer into a queue (exactly the stack) by F Sort, SPFA transferred to the other, Get the first small update answer for each team

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespaceStdConst intn=505, m=600003, dx[]={0,1,0,-1},dy[]={1,0,-1,0};intn,h,w,f[Ten][Ten][n][n],v[n][n][5],ti,s[m+5],va[n*n],top,inf;BOOLMk[n][n];CharC[n][n];structqwe{intx, y; Qwe (intx=0,inty=0) {x=x,y=y; }}dis[n][n][5],q[n*n],p[m+5];qwe DFS (intXintYintK) {if(V[x][y][k]==ti)returnQwe (-1,0); V[x][y][k]=ti;if(dis[x][y][k].x!=0)returnDIS[X][Y][K];intT=k;if(c[x][y]==' C ') k= (k+1)%4;if(c[x][y]==' A ') k= (k+3)%4;intXX=X+DX[K],YY=Y+DY[K];if(xx>w| | xx<1|| yy>h| | yy<1|| c[xx][yy]==' x ')returnDis[x][y][t]=qwe (x, y);returnDis[x][y][t]=dfs (xx,yy,k);}voidSPFA (intLintR) {memset (s),0,sizeof(s)); Memset (MK,0,sizeof(MK));intMn=inf,mx=-inf; for(intI=1; i<=top;i++) {s[va[i]]++;if(VA[I]&LT;MN) mn=va[i];if(VA[I]&GT;MX) mx=va[i]; mk[q[i].x][q[i].y]=1; } for(intI=mn+1; i<=mx;i++) S[i]+=s[i-1]; for(intI=1; i<=top;i++) P[s[va[i]]--]=q[i]; for(intI=1; i<=top;i++) Q[i]=p[top-i+1];intLl=0, rr=0; while(top| | LL!=RR) {intNow=ll%m+1; Qwe u;if(ll==rr| |            (Top&&f[l][r][q[top].x][q[top].y]<f[l][r][p[now].x][p[now].y])) u=q[top--];ElseU=p[ll=now];intX=U.X,Y=U.Y; mk[x][y]=0; for(intI=0;i<4; i++) {intTX=DIS[X][Y][I].X,TY=DIS[X][Y][I].Y;if(tx!=-1&&f[l][r][x][y]+1<f[l][r][tx][ty]) {F[l][r][tx][ty]=f[l][r][x][y]+1;if(!mk[tx][ty]) {mk[tx][ty]=1; P[rr=rr%m+1]=qwe (Tx,ty); }            }        }    }}intMain () {scanf ("%d%d%d", &n,&h,&w); for(intI=1; i<=w;i++) scanf ("%s", C[i]+1); Memset (F,127/3,sizeof(f)); inf=f[0][0][0][0];cerr<<inf<<endl; for(intI=1; i<=w;i++) for(intj=1; j<=h;j++)if(c[i][j]!=' x ') for(intk=0;k<4; k++) Ti++,dis[i][j][k]=dfs (i,j,k); for(intI=1; i<=w;i++) for(intj=1; j<=h;j++)if(c[i][j]>=' 0 '&&c[i][j]<=' 9 ') F[c[i][j]-48][C[I][J]-48][i][j]=0; for(intL=1; l<=n;l++) for(intx=1; x<=n-l+1; x + +) {intY=x+l-1; top=0; for(intI=1; i<=w;i++) for(intj=1; j<=h;j++) { for(intk=x;k<y;k++)if(f[x][y][i][j]>f[x][k][i][j]+f[k+1][Y][I][J]) f[x][y][i][j]=f[x][k][i][j]+f[k+1][Y][I][J];if(F[x][y][i][j]!=inf)                        {q[++top]=qwe (I,J);                    VA[TOP]=F[X][Y][I][J];        }} SPFA (x, y); }intAns=inf; for(intI=1; i<=w;i++) for(intj=1; j<=h;j++)if(f[1][n][i][j]<ans) ans=f[1][N][I][J]; printf"%d\n", Ans==inf?-1: ans);return 0;}

Bzoj 3205: [Apio2013] Robot "dfs+ Steiner Tree +SPFA"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.