"Bzoj 3171" [Tjoi2013] loop lattice

Source: Internet
Author: User

Description A loop lattice is a matrix in which all the elements are arrows, pointing to adjacent four squares. Each element has a coordinate (row, column), where the upper-left corner of the element coordinates (0,0). Given a starting position (R,C)

, you can walk along an arrow line between the squares. That is, if (r,c) is a left arrow, then go to (r,c-1), if it is the right arrow then walk to (r,c+1), if it is the arrow then walk to (R-1,C), if it is down arrow then go (r+1,c), each row and each column is circular, that is, if you go out of the boundary, you will appear on the other
A perfect loop lattice is defined as: for any starting position, you can follow the arrows and eventually return to the starting position. If a loop does not fit perfectly, you can modify the arrows of any element to perfection. Given a loop lattice, you need to calculate the minimum number of elements that need to be modified to make it perfect.

Input

The first row of two integers r,c. Represents rows and columns, followed by R lines, each line of C characters Lrud, indicating left and right.

Output

An integer that represents the minimum number of elements that need to be modified to make a given loop perfect

Sample Input3 4
Rrrd
Urll
LrrrSample Output2HINT

1<=r,l<=15

Maybe it's obvious, but I really don't see it as a network flow. Hzwer of each lattice in the degree of one, out of a split, each lattice to the right edge of the lattice around it, the cost is 0 or 1, run the minimum cost flow
1#include <cstdio>2#include <cstring>3 using namespacestd;4 Const intn= -, inf=1000000;5 structee{intTo,next,f,w;} e[n*n* $];6 intS,t,cnt=1, n,k,ans,timer,m,u,v,w;7 inthead[n*n],dis[n*n],pre[n*n],q[n*N],map[n][n];8 BOOLInq[n];9 intfx[4]={-1,1,0,0},fy[4]={0,0,-1,1};Ten voidInsintUintVintFintW) { Onee[++cnt].to=v,e[cnt].next=head[u],e[cnt].f=f,e[cnt].w=w,head[u]=CNT; Ae[++cnt].to=u,e[cnt].next=head[v],e[cnt].f=0, e[cnt].w=-w,head[v]=CNT; - } -    the BOOLSPFA () { -      for(intI=1; i<=t;i++) dis[i]=inf; -     intH=0, t=1; -q[t]=s;dis[s]=0; inq[s]=1; +      while(h!=t) { -         intNOW=Q[++H];if(h==t) h=0; +          for(intI=head[now];i;i=E[i].next) { A             intv=e[i].to; at             if(dis[v]>dis[now]+e[i].w&&e[i].f) { -dis[v]=dis[now]+E[I].W; -pre[v]=i; -                 if(!Inq[v]) { -Q[++t]=v;if(t==t) t=0; -inq[v]=1; in                 } -             } to         } +inq[now]=0; -     } the     if(Dis[t]==inf)return 0; *     return 1; $ }Panax Notoginseng    - voidUpdata () { the     inttmp=T; +      while(tmp!=S) { A         intl=pre[tmp],v=e[l].to; thee[l].f-=1; e[l^1].f+=1; +tmp=e[l^1].to; -     } $ans+=Dis[t]; $ } - intMain () { -scanf"%d%d",&n,&m); thet=n*m*2+1; -     Chars[ -];Wuyi      for(intI=1; i<=n;i++){ thescanf"%s", s+1); -          for(intj=1; j<=m;j++){ Wu             if(s[j]=='U') map[i][j]=0; -             if(s[j]=='D') map[i][j]=1; About             if(s[j]=='L') map[i][j]=2; $             if(s[j]=='R') map[i][j]=3;  -         } -     } -      for(intI=1; i<=n;i++) A          for(intj=1; j<=m;j++){ +             intu= (I-1) *m+J; theIns (S,u,1,0); Ins (U+n*m,t,1,0); -              for(intk=0;k<4; k++){ $                 intnx=i+fx[k],ny=j+Fy[k]; the                 if(nx>n) nx=1;if(nx<1) nx=N; the                 if(ny>m) ny=1;if(ny<1) ny=m; the                 if(K==map[i][j]) ins (U, (nx-1) *m+ny+m*n,1,0); the                 ElseIns (U, (nx-1) *m+ny+m*n,1,1); -             } in         } the      while(SPFA ()) the Updata (); Aboutprintf"%d", ans); the}

"Bzoj 3171" [Tjoi2013] loop lattice

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.