zoj 3469 Food Delivery(区间dp)

题目链接

Food Delivery

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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. Theith person's coordinate is Xi meters. And in the street there is a take-out restaurant which has coordinatesX 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 theN 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 byDispleasure Index. At the beginning, the Displeasure Index for each person is 0. When waiting for the food, theith 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'sDispleasure Index as low as possible in order to maximize your income. Your task is to find the minimal sum ofDispleasure Index.

Input

The input contains multiple test cases, separated with a blank line. Each case is started with three integersN ( 1 <= N <= 1000 ), V ( V > 0), X (X >= 0 ), then N lines followed. Each line contains two integersXi ( 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


题意:在x坐标轴上有n个人,已知每个人的位置。有一家外面店在x坐标轴上,位置为X。现在n个人同时点了外面,外卖员要送外买,送外卖的速度1米花v分钟。每个人有一个不高兴系数bi,每个人的不高兴值为(他等待外买的时间)*(不高兴系数)。求所有人的不高兴值的和,最小为多少?

题解:分析问题可知,最后一个收到外卖的人一定为n个人中最左边的或者最右边的。换句话说,要把对于下标 区间为[l,r] 的人来说,该区间最后一收到外卖的一定为下标为l或者r的人。根据这个特点,我们可以用动态规划解决此问题。

用dp[l][r][0] 表示送完区间[l,r]的人,最后一个送的人为l的最下花费。

用dp[l][r][1] 表示送完区间[l,r] 的人,最后一个送的人为r的最小花费。

转移就是:

dp[l][r][0]=min(dp[l+1][r][0]+cost(l,l+1)*(b1+b2+..bl+b(r+1)+b(r+2)+...bn),dp[l+1][r][1]+cost(l,r)*(b1+b2+..bl+b(r+1)+b(r+2)+...bn));

dp[l][r][1]=min(dp[l][r-1][0]+cost(r,l)*(b1+b2+...b(l-1)+br+b(r+1)+..bn),dp[l][r-1][1]+cost(r,r-1)*(b1+b2+...b(l-1)+br+b(r+1)+..bn));

cost(x,y)为从下标为x的人到下标为y的人所花的时间。

详情见代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
#define nn 1100
#define inff 0x7fffffff
#define eps 1e-8
typedef long long LL;
const LL inf64=LL(inff)*inff;
using namespace std;
bool use[nn][nn][2];
int dp[nn][nn][2];
int n;
int v;
int X;
int x[nn],b[nn];
int sum[nn];
int dfs(int l,int r,int s)
{
    if(use[l][r][s])
        return dp[l][r][s];
    if(l==r)
    {
        use[l][r][s]=true;
        return dp[l][r][s]=fabs(x[l]-X)*sum[n];
    }
    use[l][r][s]=true;
    dp[l][r][s]=inff;
    if(s==0)
    {
        dp[l][r][s]=min(dp[l][r][s],dfs(l+1,r,0)+(sum[n]-(sum[r]-sum[l]))*(x[l+1]-x[l]));
        dp[l][r][s]=min(dp[l][r][s],dfs(l+1,r,1)+(sum[n]-(sum[r]-sum[l]))*(x[r]-x[l]));
    }
    else
    {
        dp[l][r][s]=min(dp[l][r][s],dfs(l,r-1,1)+(sum[n]-(sum[r-1]-sum[l-1]))*(x[r]-x[r-1]));
        dp[l][r][s]=min(dp[l][r][s],dfs(l,r-1,0)+(sum[n]-(sum[r-1]-sum[l-1]))*(x[r]-x[l]));
    }
    return dp[l][r][s];
}
int main()
{
    int i,j;
    while(scanf("%d%d%d",&n,&v,&X)!=EOF)
    {
        sum[0]=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&x[i],&b[i]);
        }
        for(i=1;i<=n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                if(x[j]-x[i]<eps)
                {
                    swap(x[j],x[i]);
                    swap(b[j],b[i]);
                }
            }
        }
        for(i=1;i<=n;i++)
        {
            sum[i]=sum[i-1]+b[i];
        }
        memset(use,false,sizeof(use));
        printf("%d\n",min(dfs(1,n,0),dfs(1,n,1))*v);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值