Food Delivery(区间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

一个送外卖的小哥从x处出发,要送n个人家,每个人家有一个坐标xi和怒气值bi,每分钟怒气值增长bi,直到拿到饭为止,问最小的怒气值和为多少?

跟以前那个机器人修长城是一个题(我之前在博客写过),首先这是区间dp,假设i和j之间有个k,你是无法逾越k送给j再返回送给k,这样明显不是最优的,所以他始终是由区间内向外扩散的过程。
区间dp转移过程中,把其他人的怒气值加为总和乘以这一次送餐时间,才是到达这个状态的代价,就是在送i人时,把其他人在这个时间的怒气和提前计算在这个状态里(这句话要到代码中看)。
还有,这种题目有时候会卡初始化,所以从后向前写循环也不错。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
ll n,v,x;
ll sum[1005];
ll dp[1005][1005][2];
struct Node
{
    ll x,b;
}node[1005];
void init()
{
    sum[0] = 0;
    for(int i = 1;i <= n;i++)
        sum[i] = sum[i - 1] + node[i].b;
}
ll dfs(ll l,ll r,ll leap)
{
    if(dp[l][r][leap] >= 0)return dp[l][r][leap];
    if(l == r)
    {
        return dp[l][r][leap] = sum[n]*abs(node[l].x - x)*v;
    }
    if(leap == 0)
    {
        dp[l][r][leap] = min(dfs(l + 1,r,0) + (sum[n] - (sum[r] - sum[l]))*(node[l + 1].x - node[l].x)*v,dfs(l + 1,r,1) + (sum[n] - (sum[r] - sum[l]))*(node[r].x - node[l].x)*v);
    }
    else
    {
        dp[l][r][leap] = min(dfs(l,r - 1,0) + (sum[n] - (sum[r - 1] - sum[l - 1]))*(node[r].x - node[l].x)*v,dfs(l,r - 1,1) + (sum[n] - (sum[r - 1] - sum[l - 1]))*(node[r].x - node[r - 1].x)*v);
    }
    return dp[l][r][leap];
}
bool cmp(Node a,Node b)
{
    if(a.x == b.x)return a.b < b.b;
    return a.x < b.x;
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    while(scanf("%lld%lld%lld",&n,&v,&x) != EOF)
    {
        for(int i = 1;i <= n;i++)
            scanf("%lld%lld",&node[i].x,&node[i].b);
        for(int i = 1;i <= n;i++)
            for(int j = i;j <= n;j++)
            dp[i][j][0] = dp[i][j][1] = -1;
        sort(node + 1,node + 1 + n,cmp);
        init();
        printf("%lld\n",min(dfs(1,n,1),dfs(1,n,0)));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值