【kuangbin帶你飛-區間DP-4】F - 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 XiX_{i} ( XiX_{i} >= 0 ), BiB_{i} ( BiB_{i}>= 0), which are described above.

You can safely assume that all numbers in the input and output will be less than 23112^{31}-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[l][r][0]爲區間【l,r】中且最後停在區間的左邊,dp[l][r][1]表示停在右邊。因爲在直線上送外賣,相當於直線覆蓋,直線覆蓋下的點都可以直接給送達。相關的狀態轉移見代碼。需要注意排序。
另外,在從一個區間點直線到達另一個區間點的時候,除了已經掃描覆蓋的區間外,其他爲覆蓋的區間都有累加的不開心度,需要使用前綴和來進行累加,詳情見代碼。
最後答案爲min(dp[1][n][0],dp[1][n][1])*V
這裏我覺得有一點不好的地方,題目中說了速度是V1V^{-1},而輸入的時候寫的是V,最後結果應該出V,但是最後卻是乘V,有點歧義,因爲就算最後是乘V,但是V又是整數,說明原始的速度每分鐘纔不到1米,這顯然不和常理。。。

代碼

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1000 + 5;
const int inf = 0x3f3f3f3f;
typedef pair<int, int> ii;
int dp[maxn][maxn][2];
int sum[maxn];
int n, v, x;
int main() {
    ios::sync_with_stdio(false);//優化
    while(cin >> n >> v >> x) {
        vector<ii> p;
        for(int i = 0; i < n; i++) {
            int a, b;
            cin >> a >> b;
            p.emplace_back(a, b);
        }
        n++;
        p.emplace_back(x, 0);
        sum[0] = 0;
        //memset(sum,inf,sizeof(sum));
        memset(dp, inf, sizeof(dp));
        sort(p.begin(), p.end());
        int s = 0;
        for(int i = 1; i <= n; i++) {
            sum[i] = sum[i - 1] + p[i - 1].second;
            if(p[i - 1].first == x && p[i - 1].second == 0)
                s = i;
        }
        dp[s][s][0] = dp[s][s][1] = 0;
        for(int k = 2; k <= n; k++) {
            for(int i = 1; i + k - 1 <= n; i++) {
                int j = i + k - 1;
                dp[i][j][0] = min(dp[i][j][0], dp[i + 1][j][0] + (sum[n] - sum[j] + sum[i]) * (p[i].first - p[i - 1].first));
                dp[i][j][0] = min(dp[i][j][0], dp[i + 1][j][1] + (sum[n] - sum[j] + sum[i]) * (p[j - 1].first - p[i - 1].first));
                dp[i][j][1] = min(dp[i][j][1], dp[i][j - 1][1] + (sum[n] - sum[j - 1] + sum[i - 1]) * (p[j - 1].first - p[j - 2].first));
                dp[i][j][1] = min(dp[i][j][1], dp[i][j - 1][0] + (sum[n] - sum[j - 1] + sum[i - 1]) * (p[j - 1].first - p[i - 1].first));
            }
        }
        cout << min(dp[1][n][0], dp[1][n][1])*v << endl;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章