Test instructions
Assume:
1, every moment we can only hit a gopher, and after the end of the time all the hamster will disappear immediately;
2, the mouse appears in a straight line, if the last moment we hit the X1 position in the ground mouse, the next moment we hit the X2 position in the ground mouse, then, at this time we consume energy for ABS (X1-X2);
3, hit the first hamster without energy consumption.
Now, we know the location of all the hamsters that come out of the ground at each moment, and if you want to hit a gopher at every moment, calculate the minimum amount of energy that needs to be consumed.
Link: Point Me
DP[I][J] is expressed as the least energy consumed by the first I dozen J hamsters, and the transfer equation is shown in code
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <queue>7#include <map>8 using namespacestd;9 #defineMOD 1000000007Ten Const intinf=0x3f3f3f3f; One Const Doubleeps=1e-5; A #defineCL (a) memset (A,0,sizeof (a)) - #defineTS printf ("*****\n"); - Const intmaxn=1005; the intN,m,tt; - intA[MAXN][MAXN],DP[MAXN][MAXN]; - intMain () - { + inti,j,k; - #ifndef Online_judge +Freopen ("1.in","R", stdin); A #endif at while(SCANF ("%d%d", &n,&m)! =EOF) - { - for(i=0; i<n;i++) - for(j=0; j<m;j++) - { -scanf"%d",&a[i][j]); indp[i][j]=INF; - } to for(i=0; i<m;i++) dp[0][i]=0; + for(i=1; i<n;i++) - { the for(j=0; j<m;j++) * { $ for(k=0; k<m;k++)Panax Notoginseng { -Dp[i][j]=min (dp[i][j],dp[i-1][k]+abs (a[i][j]-a[i-1][k])); the } + } A } the intminn=INF; + for(i=0; i<m;i++) - { $Minn=min (minn,dp[n-1][i]); $ } -printf"%d\n", Minn); - } the}
HDU 4540 DP