1#include <cstdio>2#include <iostream>3#include <cstring>4 #defineM 100005 #defineINF 21390621436 using namespacestd;7 intCnt=1, n,m,ans,t,d[m],q[2*m],f[m],head[m],next[Ten*m],u[Ten*m],v[Ten*m],w[Ten*m],fro[Ten*M],fr[m];8 intmp[ -][ -],xx[4]={0,0,1,-1},yy[4]={-1,1,0,0};9 voidJia1 (intA1,intA2,intA3,intA4)Ten { Onecnt++; Anext[cnt]=HEAD[A1]; -head[a1]=CNT; -fro[cnt]=A1; theu[cnt]=A2; -v[cnt]=A3; -w[cnt]=A4; - } + voidJiaintA1,intA2,intA3,intA4) - { + jia1 (A1,A2,A3,A4); AJia1 (A2,A1,0,-A4); at return; - } - BOOLSPFA () - { -memset (D,127,sizeof(int) * (t+1)); -d[0]=0; inf[0]=1; -q[1]=0; to intH=0, t=1; + for(;h<t;) - { theh++; * intp=Q[h]; $f[p]=0;Panax Notoginseng for(intI=head[p];i;i=Next[i]) - if(v[i]&&d[u[i]]>d[p]+W[i]) the { +d[u[i]]=d[p]+W[i]; Afr[u[i]]=i; the if(!F[u[i]]) + { -f[u[i]]=1; $t++; $q[t]=U[i]; - } - } the } - if(d[t]!=inf)Wuyi return 1; the return 0; - } Wu voidMCF () - { About intmx=inf; $ for(intI=fr[t];i;i=Fr[fro[i]]) -mx=min (mx,v[i]); - for(intI=fr[t];i;i=Fr[fro[i]]) - { Av[i]-=MX; +v[i^1]+=MX; theans+=mx*W[i]; - } $ return; the } the intMain () the { the Charch[ -]; -scanf"%d%d",&n,&m); int=n*m*2+1; the for(intI=1; i<=n;i++) the { Aboutscanf"%s", ch+1); the for(intj=1; j<=m;j++) the { the if(ch[j]=='L') +mp[i][j]=0; - if(ch[j]=='R') themp[i][j]=1;Bayi if(ch[j]=='D') themp[i][j]=2; the if(ch[j]=='U') -mp[i][j]=3; - } the } the for(intI=1; i<=n;i++) the for(intj=1; j<=m;j++) the { -Jia0, (I-1) *m+j,1,0); theJia ((i-1) *m+n*m+j,t,1,0); the for(intk=0;k<4; k++) the {94 intnx=i+xx[k],ny=j+Yy[k]; the if(!NX) thenx=N; the if(!NY)98ny=m; About if(nx>N) -nx=1;101 if(ny>m)102ny=1;103 if(k==Mp[i][j])104Jia ((i-1) *m+j, (nx-1) *m+ny+n*m,1,0); the Else106Jia ((i-1) *m+j, (nx-1) *m+ny+n*m,1,1);107 }108 }109 for(; SPFA ();) the MCF ();111printf"%d\n", ans); the return 0;113}
The degree of entry and exit of the loop is equal to 1, which is different from the starting direction plus the cost, running the cost flow,
Bzoj 3171: [Tjoi2013] Loop lattice