【dp】ZOJ 3469

最近的题解都是写在本子上的……

不对……最近也没写多少题解……

还是恢复写题解的习惯吧……做了的题再分析一下印象深刻些……这就是本弱写题解的目的了……

废话完毕!

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

题目大意:有一个人他外卖员要去送外卖,他的店在X位置,他的速度为V,然后现在有N个人要外卖,坐标分别为Xi, 这N个人每等一分钟不满意度增加Bi,他送完所有人要让总的不满意度最少求最少的不满意度是多少……(语死早,总是描述不清楚题目在说什么)
看起来就像个区间dp,但是……于是就去看题解了,果然功力还很弱……
首先我们可以知道,他肯定是从店铺所在位置向两边送(至于每次每边送多远就是需要dp算的了)
f[i][j][0]表示送完[i,j]这个区间且送货员停在i位置,的最小不满值
f[i][j][1]表示送完[i,j]这个区间且送货员停在j位置,的最小不满值

f[i][j][0] = min(f[i][j][0], f[i + 1][j][0] + (a[i + 1].x - a[i].x) * (add + a[i].v)) * v;
f[i][j][1] = min(f[i][j][1], f[i][j - 1][1] + (a[j].x - a[j - 1].x) * (add + a[j].v)) * v ;
f[i][j][0] = min(f[i][j][0], f[i + 1][j][1] + (a[j].x - a[i].x) * (add + a[i].v)) * v;
f[i][j][1] = min(f[i][j][1], f[i][j - 1][0] + (a[j].x - a[i].x) * (add + a[j].v)) * v;

其中add 是所有不在[i,j]区间里的每个客户的单位时间不满意度增长量之和(可用前缀和O(1)求出)
这里要注意由于 “ Your speed is V-1 meters per minute.” 
也就是是说V其实是速度的倒数……
因为add + a[i].v 等为每个客户的单位时间不满意度增长量之和
而(a[i + 1].x - a[i].x)等为等待的路程
所以 等待的时间是  (a[i + 1].x - a[i].x) * V
但是,如果把V直接在转移的时候乘进去的话中间结果会爆int,得用long long
所以可以一直不乘 V,到输出的时候再乘上!
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;
int n, v, x;
struct ty
{
    int x, v;
}a[1010];
int f[1010][1010][3];
int sum[1010];
bool comp(ty a, ty b)
{
    return a.x < b.x;
}
int calc(int i, int j, int k, int l)
{
    return sum[j] - sum[i - 1] + sum[l] - sum[k - 1];
}
int main()
{
    while (scanf("%d%d%d", &n, &v, &x) != EOF)
    {
        for (int i = 1; i <= n; i++)
        {
            scanf("%d%d", &a[i].x, &a[i].v);
        }
        a[n + 1].x = x, a[n + 1].v = 0;
        n++;
        sort(a + 1, a+ n + 1, comp);
        memset(sum, 0, sizeof(sum));
        for(int i = 1; i <= n; i++)
            sum[i] = sum[i - 1] + a[i].v;

        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            f[i][j][0] = f[i][j][1] = 0x3f3f3f3f;
        }
        int pos = 0;
        for (int i = 1; i <= n; i++)
        {
            if (a[i].x == x && a[i].v == 0) {pos = i; break;}
        }

        f[pos][pos][0] = f[pos][pos][1] = 0;
        for(int i = pos; i >= 1; i--)
        {
            for (int j = pos; j <= n; j++)
            {
                if (i == j) continue;
                int add = calc(1, i - 1, j + 1, n);
                f[i][j][0] = min(f[i][j][0], f[i+1][j][0]+(a[i+1].x-a[i].x)*(add+a[i].v));
                f[i][j][1] = min(f[i][j][1], f[i][j-1][1]+(a[j].x-a[j-1].x)*(add+a[j].v));
                f[i][j][0] = min(f[i][j][0], f[i+1][j][1]+(a[j].x-a[i].x)*(add+a[i].v));
                f[i][j][1] = min(f[i][j][1], f[i][j-1][0]+(a[j].x-a[i].x)*(add+a[j].v));
            }
        }

        printf("%d\n", min(f[1][n][0], f[1][n][1]) * v);
    }
    return 0;
}



你可能感兴趣的:(dp,ZOJ)