首页 > 代码库 > hdu 4362 Dragon Ball

hdu 4362 Dragon Ball

Dragon Ball

                                                          Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                                         Total Submission(s): 2204    Accepted Submission(s): 770


Problem Description
Sean has got a Treasure map which shows when and where the dragon balls will appear. some dragon balls will appear in a line at the same time for each period.Since the time you got one of them,the other dragon ball will disappear so he can only and must get one Dragon ball in each period.Digging out one ball he will lose some energy.Sean will lose |x-y| energy when he move from x to y.Suppose Sean has enough time to get any drogan ball he want in each period.We want to know the minimum energy sean will lose to get all period’s dragon ball.
 

Input
In the first line a number T indicate the number of test cases.Then for each case the first line contain 3 numbers m,n,x(1<=m<=50,1<=n<=1000),indicate m period Dragon ball will appear,n dragon balls for every period, x is the initial location of sean.Then two m*n matrix. For the first matrix,the number in I row and J column indicate the location of J-th Dragon ball in I th period.For the second matrix the number in I row and J column indicate the energy sean will lose for J-th Dragon ball in I-th period.
 

Output
For each case print a number means the minimum energy sean will lose.
 

Sample Input
1 3 2 5 2 3 4 1 1 3 1 1 1 3 4 2
 

Sample Output
8
 

Author
FZU
 

Source
2012 Multi-University Training Contest 7
 




   题意:
         m个周期,每个周期出现n个龙珠,刚开始在x位置,从x位置到y位置消耗的能量值是两者差的绝对值+在y位    置的龙珠要吸收的能量值,每个周期必须要在一个龙珠的位置。
 


   题解:
           很容易想到动态规划方程a[i][k].dp=min(a[i][k].dp,(a[i-1][j].dp+|a[i][k].x-a[i-1][j].x|+a[i][k].v)),这样直接用复    杂度为O(m*n*n)会超时,需要优化,把绝对值去掉,会出现两种情况


   1.a[i][k].x>=a[i-1][j].x(前一个周期龙珠的位置小于后一个周期龙珠的位置)
         这时a[i-1][j].dp-a[i-1][j].x+a[i][k].x+a[i][k].v,设mini=a[i-1][j].dp-a[i-1][j].x  此时a[i][k].dp=mini+a[i][k].x+a[i]      [k].v 对a[i][k].dp,a[i][k].x和a[i][k].v为固定的,在所有的a[i][k].x>=a[i-1][j].x中,只需找到最小的mini就可以          了,此时对所有j,只需遍历一遍j就可以了,不需要每个k都遍历一遍j了,因为对后一个k值,其i-1行x值小于现在      的x值的,只会多在前面的基础上多数,前面的最小的只需与后来多的比较就可以了。



   2.a[i][k].x<=a[i-1][j].x(前一周期龙珠的位置大于后一个周期龙珠的位置)
          这时a[i-1][j].dp+a[i-1][j].x-a[i][k].x+a[i][k].v,设mini=a[i-1][j].dp-a[i-1][j].x,此时只需在所有的前一周期龙珠的    位置大于后一个周期龙珠的位置中找最小的mini就可以了。

  代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf=1999999999;
struct node
{
    int x;
    int v;
    int dp;
}a[55][1100];
int bbs(int x)
{
    if(x<0)
    x=-x;
    return x;
}
bool cmp(node l,node r)
{
    return l.x<r.x;
}
int main()
{
    int t,n,m,x;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&m,&n,&x);
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&a[i][j].x);
                a[i][j].dp=inf;
            }
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&a[i][j].v);
            }
        }
        sort(a[1]+1,a[1]+1+n,cmp);
        for(int i=1;i<=n;i++)
        a[1][i].dp=bbs(x-a[1][i].x)+a[1][i].v;//第1周期特殊处理
        for(int i=2;i<=m;i++)
        {
            sort(a[i]+1,a[i]+1+n,cmp);
            //前一个周期龙珠的位置小于后一个周期龙珠的位置
            int j=1;
            int mini=inf;
            for(int k=1;k<=n;k++)
            {
                     while(j<=n&&a[i][k].x>=a[i-1][j].x)
                     {
                         mini=min(mini,(a[i-1][j].dp-a[i-1][j].x));
                         j++;
                     }
                     a[i][k].dp=min(a[i][k].dp,(mini+a[i][k].x+a[i][k].v));
            }
            //前一周期龙珠的位置大于后一个周期龙珠的位置
            j=n;
            mini=inf;
            for(int k=n;k>=1;k--)
            {
                while(j>=1&&a[i-1][j].x>=a[i][k].x)
                {
                    mini=min(mini,(a[i-1][j].dp+a[i-1][j].x));
                    j--;
                }
                a[i][k].dp=min(a[i][k].dp,(mini-a[i][k].x+a[i][k].v));
            }
        }
        int ans=inf;
        for(int i=1;i<=n;i++)
        {
            ans=min(ans,a[m][i].dp);
        }
        printf("%d\n",ans);
    }
    return 0;
}




hdu 4362 Dragon Ball