ZOJ - 3469 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 Nlines 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個客人同時叫外賣,每個顧客在等外買的時間裏每秒增加的憤怒值不同。給出客人和餐廳的位置,以及客人每分鐘增加的憤怒值,還有快遞小哥的行走一公里需要的時間。問送完外賣後n個客人的最小憤怒值?

其實一開始真的完全懵逼,還以爲會是一個貪心題==,然後感覺貪不出來,又開在了dp專題裏面,不免就想到了自己每次寫的毫無理由的區間dp,接下來就是找動態轉移方程了。先把外賣小哥所在的點加進去,然後根據所在地點從小到大的排序,然後外賣小哥所在的地點開始向左向右掃描

dp[i][j][0]表示送完i到j的外賣客戶的最小不開心值,此時停在i點;dp[i][j][1]表示送完i到j的外賣客戶的最小不開心值,此時停在j點;動態轉移方程有四個:

dp[i][j][0] = min(dp[i][j][0] , dp[i+1][j][0] + (no[i+1].x - no[i].x) *(sum[n]-sum[j]+sum[i]));

dp[i][j][0] = min(dp[i][j][0] , dp[i+1][j][1] + (no[j].x - no[i].x) *(sum[n]-sum[j]+sum[i]));

dp[i][j][1] = min(dp[i][j][1] , dp[i][j-1][1] + (no[j].x - no[j-1].x) *(sum[n]-sum[j-1]+sum[i-1]));

dp[i][j][1] = min(dp[i][j][1] , dp[i][j-1][0] + (no[j].x - no[i].x) *(sum[n]-sum[j-1]+sum[i-1]));


#include<stdio.h>
#include<iostream>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
struct node{
    int x , b ;
}no[1005];
int dp[1005][1005][2];
bool cmp(node x , node y){
    return x.x < y.x;
}
int sum[1005];
int n , v , x;
int main()
{
    while(~scanf("%d%d%d" , &n , &v , &x)){
        for(int i = 1 ; i <= n ; i ++)
            scanf("%d%d" , &no[i].x , &no[i].b);
        no[++n].x = x ;  no[n].b = 0 ;
        sort(no + 1 , no + n + 1 , cmp);
        sum[0] = 0 ;
        for(int i = 1 ; i <= n ; i ++){
            sum[i] = sum[i - 1] + no[i].b;
        }
        for(int i = 0 ; i <= n ; i ++){
            for(int j = 0 ; j <= n ; j ++){
                dp[i][j][0] = dp[i][j][1] = inf;
            }
        }
        int mid = 0;
        for(int i = 1 ; i <= n ; i ++){
            if(no[i].x == x){
                mid = i ;
                dp[i][i][0] = dp[i][i][1] = 0;
                break;
            }
        }//初始化起點,即送餐開始位置
        for(int i = mid ; i > 0 ; i --){//分別向左向右dp
            for(int j = mid ; j <= n ; j ++){
                if(i == j)
                    continue;
                dp[i][j][0] = min(dp[i][j][0] , dp[i+1][j][0] + (no[i+1].x - no[i].x) *(sum[n]-sum[j]+sum[i]));
                dp[i][j][0] = min(dp[i][j][0] , dp[i+1][j][1] + (no[j].x - no[i].x) *(sum[n]-sum[j]+sum[i]));
                dp[i][j][1] = min(dp[i][j][1] , dp[i][j-1][1] + (no[j].x - no[j-1].x) *(sum[n]-sum[j-1]+sum[i-1]));
                dp[i][j][1] = min(dp[i][j][1] , dp[i][j-1][0] + (no[j].x - no[i].x) *(sum[n]-sum[j-1]+sum[i-1]));
            }
        }
        cout<<(min(dp[1][n][1] , dp[1][n][0]))*v<<endl;//最後可以停在最左邊或者最右邊
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章