Food Delivery(区间dp zoj-3469)

参考题解

When we are focusing on solving problems, we usually prefer to stay in front of computers rather than go out for lunch. At this time, we may call for food delivery.

Suppose there are N people living in a straight street that is just lies on an X-coordinate axis. The ith person's coordinate is Xi meters. And in the street there is a take-out restaurant which has coordinates X meters. One day at lunchtime, each person takes an order from the restaurant at the same time. As a worker in the restaurant, you need to start from the restaurant, send food to the N people, and then come back to the restaurant. Your speed is V-1 meters per minute.

You know that the N people have different personal characters; therefore they have different feeling on the time their food arrives. Their feelings are measured by Displeasure Index. At the beginning, the Displeasure Index for each person is 0. When waiting for the food, the ith person will gain Bi Displeasure Index per minute.

If one's Displeasure Index goes too high, he will not buy your food any more. So you need to keep the sum of all people's Displeasure Index as low as possible in order to maximize your income. Your task is to find the minimal sum of Displeasure Index.

Input

The input contains multiple test cases, separated with a blank line. Each case is started with three integers N ( 1 <= N <= 1000 ), V ( V > 0), X ( X >= 0 ), then Nlines followed. Each line contains two integers Xi ( Xi >= 0 ), Bi ( Bi >= 0), which are described above.

You can safely assume that all numbers in the input and output will be less than 231- 1.

Please process to the end-of-file.

<b< dd="">

Output

For each test case please output a single number, which is the minimal sum of Displeasure Index. One test case per line.

<b< dd="">

Sample Input

5 1 0
1 1
2 2
3 3
4 4
5 5

<b< dd="">

Sample Output

55


说实话这道题参考着题解a完了以后还是迷迷糊糊的,写篇博客来总结一下,思考一些细节。

题意可见参考题解。

在思考题目的时候,直接思考是联系不到区间dp的。直接思考得到的一种感觉是,一旦餐厅左边的某家顾客i被送达,那么从i到餐厅位置X之间的所有顾客都是会被送达的。想到了这一点以后,其实就想通了,单独讨论某一边的路线是没有意义的,我们可以想想,这个送餐员实际送餐的过程当中,一定是先到一边,没有跑到头,就折返回另一边,通过这种方式来达到总的不满意程度最低的目的。想到这里,直接思路就走不下去了,会想着贪心,但是要顾忌到的东西太多,贪心要同时考虑每一个顾客的位置远近和不满意程度的增长快慢。

这种情况下思考问题,就可以考虑动态规划这一种只关注条件转移而不关注整体实际操作先后的算法。

但是运用普通的区间dp,如石子归并问题中的模板,会发现它的特殊性。首先它的状态转移,当区间长度加一时,并不能随意从中间断开区间,并且从左还是从右边过来转移的思路思考点不一样。

于是就要开到三维数组,第三个下标来指示状态是从左边转移还是右边。dp[i][j][k],k=0表示从左边转移,k=1表示从右边转移。

明显是要找到餐厅的位置,以它为参考点进行操作的,同时还应该注意到,dp的起点也只能是这个特殊的点。所以在读入所有顾客位置和顾客不满意度之后,同时加入餐厅位置并将餐厅位置的不满意增量设定为0,把这n+1个量进行排序。找到餐厅的位置并且用loc保存下来。

这里顺带复习对结构体变量数组排序的时候,重新定义<号可以在结构体定义当中进行:friend bool operator<(node a,node b){return a.x<b.x;}

排序完以后要对所有的dp值赋成无穷大,并把dp[loc][loc][k]赋初值成0。这里却不能把任意的dp[i][i][k]赋值成0,不然dp[i+1][j]可能会在状态转移方程中出现一些问题。具体如何这里留个疑问,这个点我也没有彻底钻透。

接下来就是状态转移了。这里要注意,这个地方的dp数组含义并不是孤立在每个状态里的,它是进行到当前状态时,会对最终结果造成的不满意值的影响。所以在状态转移的时候,先分类它是从哪个状态转移过来的,再把转移前的dp值,加上进行该段路程配送会对总不满意值造成的影响。比如从i+1配送到i,转移前是i+1到j的区间,那么除了i+1和j以及他们之间的顾客,其他顾客的不满意值都在随时间累加。这样来理解这四个状态转移方程。

dp[i][j][0]=dp[i+1][j][0]+(cos[i+1].x-cos[i].x)*(sum[n+1]-(sum[j]-sum[i]));//从左边的i+1走到i cos[i].y也是要累计起来的

dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(cos[j].x-cos[i].x)*(sum[n+1]-(sum[j]-sum[i])));//从右边的j走到

dp[i][j][1]=dp[i][j-1][1]+(cos[j].x-cos[j-1].x)*(sum[n+1]-(sum[j-1]-sum[i-1]));

dp[i][j][1]=min(dp[i][j][1],

dp[i][j-1][0]+(cos[j].x-cos[i].x)*(sum[n+1]-(sum[j-1]-sum[i-1])));

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f;
struct cos
{
    int x,y;
    friend bool operator <(cos a,cos b)
    {
        return a.x<b.x;
    }
}cos[1010];
int sum[1010],dp[1010][1010][3];
int main()
{
    int n,v,X;
    while(~scanf("%d%d%d",&n,&v,&X))
    {
        memset(dp,0x3f,sizeof(dp));
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&cos[i].x,&cos[i].y);
        }
        cos[n+1].x=X,cos[n+1].y=0;
        sort(cos+1,cos+n+2);
        for(int i=1;i<n+2;i++)
        {
            sum[i]=sum[i-1]+cos[i].y;
        }
        int loc=0;
        while(cos[++loc].x!=X);//以上的操作把X定成了一个基准位置,loc来记录它
        dp[loc][loc][0]=dp[loc][loc][1]=0;
        for(int i=loc;i>0;i--)
            for(int j=loc;j<n+2;j++)
        {
            if(i==j) continue;
            dp[i][j][0]=dp[i+1][j][0]+(cos[i+1].x-cos[i].x)*(sum[n+1]-(sum[j]-sum[i]));//从左边的i+1走到i cos[i].y也是要累计起来的
            dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(cos[j].x-cos[i].x)*(sum[n+1]-(sum[j]-sum[i])));//从右边的j走到i
            dp[i][j][1]=dp[i][j-1][1]+(cos[j].x-cos[j-1].x)*(sum[n+1]-(sum[j-1]-sum[i-1]));
            dp[i][j][1]=min(dp[i][j][1],
                            dp[i][j-1][0]+(cos[j].x-cos[i].x)*(sum[n+1]-(sum[j-1]-sum[i-1])));
        }
        printf("%d\n",v*min(dp[1][n+1][0],dp[1][n+1][1]));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值