Luo Gu [P4850] [IOI2009] raisins (dp, cards often)

Topic Link

Topic Portal

Analytical title

Here we introduce aDo not open \ (O2 \)Methods

Good push dp equation of this problem:

\ (F [x 1] [y_1] [x_2] [y_2] = min (f [x 1] [y_1] [x_2] [y_2], f [x 1] [y_1] [i] [y_2] + f [i + 1] [y_1] [x_2] [y_2] + s [x 1] [y_1] [x_2] [y_2]) \)

as well as:

\(f[x_1][y_1][x_2][y_2]=min(f[x_1][y_1][x_2][y_2],f[x_1][y_1][x_2][i]+f[x_1][i+1][x_2][y_2]+s[x_1][y_1][x_2][y_2])\)

Specific arrays meaning look at the code to understand the

Then make a memory search up - \ (TLE \) a

We found that four-dimensional array of access is very slow, so you can use a similar method to hash into a four-dimensional

Then on the cardhighLater

Code

#include <stdio.h>

using namespace std;

template <typename T> inline void Read(T &t)
{
    int c=getchar(),f=0;
    for (;c<'0'||c>'9';c=getchar()) f=(c=='-');
    for (t=0;c>='0'&&c<='9';c=getchar()) t=(t<<3)+(t<<1)+(c^48);
    if (f) t=-t;
}

const int N=55,t[]={1,55,3025,166375,9150625};
int n,m,s[N][N];
int dp[N*N*N*N];

inline int min(int a, int b) {return a<b?a:b;}

int dfs(int x, int y, int _x, int _y)
{
    int a=x*t[3]+y*t[2]+_x*t[1]+_y*t[0];
    if (dp[a]) return dp[a];
    if (x==_x&&y==_y) return 0;
    dp[a]=0x7fffffff;
    for (int i=x;i<_x;i++)
        dp[a]=min(dp[a],dfs(x,y,i,_y)+dfs(i+1,y,_x,_y));
    for (int i=y;i<_y;i++)
        dp[a]=min(dp[a],dfs(x,y,_x,i)+dfs(x,i+1,_x,_y));
    return dp[a]+=s[_x][_y]-s[x-1][_y]-s[_x][y-1]+s[x-1][y-1];
} 

int main()
{
    Read(n),Read(m);
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
            Read(s[i][j]),s[i][j]+=s[i-1][j]+s[i][j-1]-s[i-1][j-1];
    printf("%d\n",dfs(1,1,n,m));
    return 0;   
} 

Guess you like

Origin www.cnblogs.com/asd369-blog/p/p4850-raisins.html