Food Delivery ZOJ - 3469 (区间dp)

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 N lines 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.

 

Output

 

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

 

Sample Input

 

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

 

Sample Output

 

55

不要把本题想的 过于复杂

刚看到此题时我一直在想怎么在dp数字中记录当前的已用时间

 当我在思考如何在当前时间和目前的价值之间平衡时

在看到题解时才明白 其实本题可以不考虑时间

直接把所有的点全都看成0

每次在走的过程中我们只需要将每秒的增长的不满意度加上

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
long long dp[1004][1004][2];//0左边 1右边
long long  sum[1004];
struct node
{
    long long b;
    long long x;
}p[1004];
int cmp(node p,node  q)
{
    return p.x<q.x;
}
int main()
{
    long long n,v,x;
    while(~scanf("%lld%lld%lld",&n,&v,&x))
    {
        for(int i=0;i<=n+1;i++)
            for(int j=0;j<=n+1;j++)
                dp[i][j][0]=dp[i][j][1]=1e18;
        //printf("%lld\n",dp[1][1]);
//        for(int i=1;i<=n;i++)
//            dp[i][i][0]=dp[i][i][1]=0;
        for(int i=1;i<=n;i++)
            scanf("%lld%lld",&p[i].x,&p[i].b);
        n++;
        p[n].x=x;
        p[n].b=0;
        sort(p+1,p+1+n,cmp);
        sum[0]=0;
        for(int i=1;i<=n;i++)
            sum[i]=sum[i-1]+p[i].b;
        int tmp;
        for(int i=1;i<=n;i++)
            if(p[i].x==x) tmp=i;
        dp[tmp][tmp][1]=dp[tmp][tmp][0]=0;
        for(int i=tmp;i<=n;i++)//second->左边 first->右边
        {
            for(int j=tmp;j>=1;j--)
            {//零在左边 一在右边
                if(i==j) continue;
                dp[j][i][1]=min(dp[j][i][1], dp[j][i-1][0]+(sum[n]-sum[i-1]+sum[j-1])*(p[i].x-p[j].x) );
                dp[j][i][1]=min(dp[j][i][1], dp[j][i-1][1]+(sum[n]-sum[i-1]+sum[j-1])*(p[i].x-p[i-1].x) );

                dp[j][i][0]=min(dp[j][i][0], dp[j+1][i][0]+(sum[n]-sum[i]+sum[j])*(p[j+1].x-p[j].x) );
                dp[j][i][0]=min(dp[j][i][0], dp[j+1][i][1]+(sum[n]-sum[i]+sum[j])*(p[i].x-p[j].x) );
                //cout<<j<<i<<"*"<<dp[j][i][0]<<" "<<dp[j][i][1]<<endl;
            }
        }
    long long ans=min(dp[1][n][1],dp[1][n][0]);
    printf("%lld\n",ans*v);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值