Food Delivery ZOJ - 3469(区间dp)

Food Delivery 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 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个点要送外卖。餐厅坐标为x,工人的速度为V-1,也就是 1/v 米每分钟(不是v米每分钟)。给出n的点的x坐标和参数v,外卖没送到,客户就会不愉快,每一分钟的不愉快指数增加v。问怎么样的送货策略,使得所有客户的总不愉悦指数和最小。

分析:

这道题关键要找到一个规律:
  派送任意一个区间[i,j]的用户,按照最优路线走,一定最后停留在该区间的两端处,即 i 和 j !!!

所以,就会有以下的状态设计,从而可以轻松写出状态转移方程了:

   dp[i][j][0]:=[i,j]i d p [ i ] [ j ] [ 0 ] := 派 送 区 间 [ i , j ] 的 用 户 并 且 最 后 派 送 i 时 的 最 小 值
  
   dp[i][j][1]:=[i,j]j d p [ i ] [ j ] [ 1 ] := 派 送 区 间 [ i , j ] 的 用 户 并 且 最 后 派 送 j 时 的 最 小 值

   dp[i][j][0]=min(dp[i+1][j][0]+cost1,dp[i+1][j][1]+cost2) d p [ i ] [ j ] [ 0 ] = m i n ( d p [ i + 1 ] [ j ] [ 0 ] + c o s t 1 , d p [ i + 1 ] [ j ] [ 1 ] + c o s t 2 )
  
   dp[i][j][1]=min(dp[i][j1][0]+cost3,dp[i][j1][1]+cost4) d p [ i ] [ j ] [ 1 ] = m i n ( d p [ i ] [ j − 1 ] [ 0 ] + c o s t 3 , d p [ i ] [ j − 1 ] [ 1 ] + c o s t 4 )

   cost1=(x[i+1]x[i])(sum[n]sum[j]+sum[i]) c o s t 1 = ( x [ i + 1 ] − x [ i ] ) ∗ ( s u m [ n ] − s u m [ j ] + s u m [ i ] )
  
   cost2=(x[j]x[i])(sum[n]sum[j]+sum[i]) c o s t 2 = ( x [ j ] − x [ i ] ) ∗ ( s u m [ n ] − s u m [ j ] + s u m [ i ] )
  
   cost3=(x[j]x[i])(sum[n]sum[j1]+sum[i1]) c o s t 3 = ( x [ j ] − x [ i ] ) ∗ ( s u m [ n ] − s u m [ j − 1 ] + s u m [ i − 1 ] )
  
   cost4=(x[j]x[j1])(sum[n]sum[j1]+sum[i1]) c o s t 4 = ( x [ j ] − x [ j − 1 ] ) ∗ ( s u m [ n ] − s u m [ j − 1 ] + s u m [ i − 1 ] )
  
我们是先求出每个人每分钟不满意值增长数的前缀和,然后乘距离,相当于算的是走这些距离的除去区间[i,j]其他部分的不满意值的增加数,因为每分钟走 1V 1 V 米,那么1米就需要V时间,因此最终结果乘上V就是最终答案

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000+5;
int dp[maxn][maxn][2];
int sum[maxn];
struct P{
    int x,b;
    bool operator < (const P &other)const{
        return x < other.x;
    }
    bool operator < (int X)const{
        return x < X;
    }
}p[maxn];

int main(){
    int N,V,X;
    while(~scanf("%d%d%d",&N,&V,&X)){
        for(int i = 1; i <= N; i++){
            scanf("%d%d",&p[i].x,&p[i].b);
        }
        ++N;
        p[N].x = X;
        p[N].b = 0;
        sort(p+1,p+1+N);
        for(int i = 1; i <= N; i++) 
            sum[i] = sum[i-1] + p[i].b;
        int pos = lower_bound(p+1,p+1+N,X) - p;
        for(int i = 1; i <= N; i++){
            for(int j = 1; j <= N; j++){
                dp[i][j][0] = dp[i][j][1] = INF;
            }
        }
        dp[pos][pos][0] = dp[pos][pos][1] = 0;
        for(int i = pos; i >= 1; i--){
            for(int j = pos; j <= N; j++){
                if(i == j) continue;
                dp[i][j][0] = min(dp[i][j][0],dp[i+1][j][0]+(sum[i]+sum[N]-sum[j])*(p[i+1].x-p[i].x));
                dp[i][j][0] = min(dp[i][j][0],dp[i+1][j][1]+(sum[i]+sum[N]-sum[j])*(p[j].x-p[i].x));
                dp[i][j][1] = min(dp[i][j][1],dp[i][j-1][0]+(sum[i-1]+sum[N]-sum[j-1])*(p[j].x-p[i].x));
                dp[i][j][1] = min(dp[i][j][1],dp[i][j-1][1]+(sum[i-1]+sum[N]-sum[j-1])*(p[j].x-p[j-1].x));
            }
        }
        printf("%d\n",V*min(dp[1][N][0],dp[1][N][1]));
        memset(dp,0,sizeof(dp));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值