HDU 4540 DP

Source: Internet
Author: User
Tags gopher

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

Related Article

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.