FJ Provincial Team Training final Test T3

Source: Internet
Author: User

Idea: State compression dp,f[i][j[[k] stands for I row J column this lattice, continuous state is k, what is this continuous state? It is

The X-Grid represents the place where I am now, and here the state is the red part, that is, the state of a continuous section of n, we calculate each bit, so it can be shifted, note that when the current point at the bottom of the time to calculate an additional contribution to 1.

Pit father, INF set small only 30 points.

1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6 #definell Long Long7 Constll inf=1e18;8ll f[50005][ A],bin[ -],sum[ -],ans;9ll a[6][20005],b[6][20005],c1[6][20005],c2[6][20005];Tenll s[220005]; One intn,m; A ll Read () { -ll t=0, f=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} the      while('0'<=ch&&ch<='9') {t=t*Ten+ch-'0'; ch=GetChar ();} -     returnt*F; - } -ll query (intIdintJintSt) { +     inttop=0; -      for(intI=1; i<=n;i++){ +s[++top]=st%2; ASt/=2; at     } -s[0]=s[top];s[top+1]=s[1]; -ll res=0; -      for(intI=1; i<=n;i++) -res+= ((S[i]*bin[id]) ^ (A[i][j]&bin[id]) *B[i][j]; -      for(intI=1; i<=n;i++) inres+= ((S[i]*bin[id]) ^ (bin[id]*s[i+1]))*C2[i][j]; -     returnRes; to } + voiddpintID) { -     intTot= (1<<n)-1, cnt=0; the      for(intI=1; i<=n*m;i++) *       for(intj=0; j<=tot;j++) $f[i][j]=inf;Panax Notoginseng      for(intI=0; i<=tot;i++) -F[n][i]=query (ID,1, i);  the      for(intj=2; j<=m;j++) +       for(intI=1; i<=n;i++){ A         intNow= (J-1) *n+i,pre=now-1; the          for(intst=0; st<=tot;st++){ +             intst1=st&sum[n-2],lst= ((st&bin[n-2]) >0), ths= ((st&bin[n-1]) >0); -ll tmp=0; $             if(i!=1) tmp+= ((lst^ths) *c2[i-1][J]) *Bin[id]; $             if(i==n) tmp+= ((st&bin[0]) ^ths) *c2[i][j]) *Bin[id]; -tmp+= ((A[i][j]&bin[id]) ^ (Ths*bin[id]) *B[i][j]; -              for(intk=0; k<=1; k++){ the                 intSt2=st1*2+k,beh= ((st2&bin[0]) >0); -ll tmp2= (beh^ths) *bin[id]*c1[i][j-1];WuyiF[now][st]=std::min (f[now][st],f[pre][st2]+tmp+tmp2); the             } -         } Wu      } -ll tmp=inf; About       for(intI=0; i<=tot;i++) $Tmp=std::min (f[n*m] [I],tmp]; -ans+=Tmp; - } - intMain () { AN=read (); M=read (); bin[0]=1; +      for(intI=1; i<= +; i++) bin[i]=bin[i-1]*2; sum[0]=bin[0]; the      for(intI=1; i<= +; i++) sum[i]=sum[i-1]+Bin[i]; -      for(intI=1; i<=n;i++) $       for(intj=1; j<=m;j++) thea[i][j]=read (); the      for(intI=1; i<=n;i++) the       for(intj=1; j<=m;j++)  theb[i][j]=read (); -      for(intI=1; i<=n;i++) in       for(intj=1; j<m;j++) thec1[i][j]=read (); the      for(intI=1; i<=n;i++) About       for(intj=1; j<=m;j++) thec2[i][j]=read (); the      for(intI=0; i<= +; i++){ the DP (i); +     }           -printf"%i64d\n", ans);  the fclose (stdin); fclose (stdout);Bayi     return 0; the}

FJ Provincial Team Training final Test T3

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.