区间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去搞一下就可以,dp[i][j][0]表示送外区间 ( i, j ) 后此人位于区间的左侧,dp[i][j][1]表示在区间的右侧。想一下,当你知道区间(i, j)的最小花费后,他可以转移到哪些状态呢?很好想的就是 (i-1, j) 和 (i, j+1)这两个区间吧,然后这个问题就解决了,唯一在注意的一个地方就是初始化的细节。

代码示例:

#define ll long long
const ll inf = 0x3f3f3f3f;

ll n, v, x;
struct node
{
    ll x, b;
    
    bool operator< (const node &v){
        return x < v.x;
    }
}a[1005];
ll sum[1005];
ll dp[1005][1005][2];

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    while(~scanf("%lld%lld%lld", &n, &v, &x)){
        for(ll i = 1; i <= n; i++){
            scanf("%lld%lld", &a[i].x, &a[i].b);        
        }
        a[n+1].x = x, a[n+1].b = 0;
        n++;
        sort(a+1, a+1+n);        
        for(ll i = 1; i <= n; i++) sum[i] = sum[i-1]+a[i].b;
        memset(dp, inf, sizeof(dp));
        for(ll i = 1; i <= n; i++){
            if (a[i].x == x) {
                dp[i][i][0] = dp[i][i][1] = 0;
                break;
            }
        }          
        for(ll len = 2; len <= n; len++){
            for(ll i = 1; i <= n; i++){
                ll j = i+len-1;
                if (j > n) break;
                ll t1 = (a[i+1].x-a[i].x)*v;
                ll s1 = sum[n]-sum[j]+sum[i];
                ll t2 = (a[j].x-a[i].x)*v;
                dp[i][j][0] = min(dp[i+1][j][0]+s1*t1, dp[i+1][j][1]+t2*s1);
                ll s2 = sum[n]-sum[j-1]+sum[i-1];
                ll t3 = (a[j].x-a[j-1].x)*v;
                dp[i][j][1] = min(dp[i][j-1][0]+t2*s2, dp[i][j-1][1]+t3*s2);
            }
        }
        
        printf("%lld\n", min(dp[1][n][0], dp[1][n][1]));
    }
    return 0;
}

 

posted @ 2018-06-11 20:18  楼主好菜啊  阅读(231)  评论(0编辑  收藏  举报