zoj 3469 Food Delivery 【区间dp】

来源:互联网 发布:mysql decimal 截断 编辑:程序博客网 时间:2024/04/29 10:07

Food Delivery

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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



题意:饭店要你给N个客户送食物,现在已经给出饭店的位置坐标X和N个客户的位置坐标x[]。已知这N个客户都有一个不开心系数d[],初始为0,以后每分钟会增加b[],直到食物送到为止。你的速度为v^-1每分钟。现在为了获得最大收益,你必须保证这N个客户的d[]之和最小。问你最小的d[]之和。


思路:设置dp[i][j]为区间[i, j]的最优解
首先可以用一个相当暴力的方案——枚举区间[i, j]里面最后一个送达食物的客户。
这样转移状态的时间复杂度即使最优的情况下也为O(n),会TLE。
我们考虑去优化一些中间的状态,发现dp[i][j]的最优解只会产生于两个状态——
1,最后送第i个客户。
2,最后送第j个客户。
只有两个状态,这样,在转移状态的时候只需要O(1),整个程序的总时间复杂度最坏为O(n^2)。足够解决该题了。


定义三维数组dp[i][j][0,1]
其中dp[i][j][0]表示在区间[i, j]里面最后送第i个客户,dp[i][j][1]表示在区间[i, j]里面最后送第j个客户。

注意在处理区间[i, j]时,区间外的人的不高兴系数是会增加的,需要用add记录区间[i, j]外所有人不高兴系数每分钟增加之和。求解add——可以先预处理前缀和,然后O(1)的时间复杂度就可以求出add。

状态转移就很简单了:
dp[i][j][0] = 
min(dp[i][j][0], min(dp[i+1][j][0]+(num[i+1].x-num[i].x)*(add+num[i].b), dp[i+1][j][1]+(num[j].x-num[i].x)*(add+num[i].b)));

dp[i][j][1] = min(dp[i][j][1], min(dp[i][j-1][1]+(num[j].x-num[j-1].x)*(add+num[j].b), dp[i][j-1][0]+(num[j].x-num[i].x)*(add+num[j].b)));

最后乘以V就可以了。


AC代码:


#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#define INF 0x3f3f3f3f#define eps 1e-8#define MAXN (1000+10)#define MAXM (100000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1using namespace std;struct Node{    int x, b;};Node num[MAXN];bool cmp(Node A, Node B){    return A.x < B.x;}int sum[MAXN];int dp[MAXN][MAXN][2];int main(){    int N, V, X;    while(scanf("%d%d%d", &N, &V, &X) != EOF)    {        for(int i = 1; i <= N; i++)            Ri(num[i].x), Ri(num[i].b);        sort(num+1, num+N+1, cmp); CLR(dp, INF);        sum[0] = 0;        for(int i = 1; i <= N; i++)            sum[i] = sum[i-1] + num[i].b;        for(int i = N; i >= 1; i--)        {            for(int j = i; j <= N; j++)            {                int add = sum[N] - sum[j] + sum[i-1];                if(i == j)                    dp[i][j][0] = dp[i][j][1] = abs(num[i].x-X) * (add+num[i].b);                else                {                    dp[i][j][0] = min(dp[i][j][0], min(dp[i+1][j][0]+(num[i+1].x-num[i].x)*(add+num[i].b), dp[i+1][j][1]+(num[j].x-num[i].x)*(add+num[i].b)));                    dp[i][j][1] = min(dp[i][j][1], min(dp[i][j-1][1]+(num[j].x-num[j-1].x)*(add+num[j].b), dp[i][j-1][0]+(num[j].x-num[i].x)*(add+num[j].b)));                }            }        }        Pi(min(dp[1][N][0], dp[1][N][1])*V);    }    return 0;}


0 0
原创粉丝点击