ZOJ 3469 Food Delivery (区间DP)

来源:互联网 发布:radio js checked的值 编辑:程序博客网 时间:2024/06/05 16:16

题意

一个外卖员要从x出发,送一条线上的n个人的外卖,每个人都有一个不满意度,这个会随着时间变化而增加,求送完所有人家外卖之后最低的不满意度。

思路

dp[l][r][0/1]表示送完了l和r这段区间之后,现在快递员待在l/r时这段区间的最小不满意度。
那么dp[i][j]都能够进行递推了,需要注意的是,因为时间是会累加的,但是如果递推到某个地方再计算的话时间无法统计,所以我们在计算每个区间的时候就加上还没有送到的区间的当前不满意度。

代码

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define LL long long#define lowbit(x) ((x)&(-x))#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1|1#define MP(a, b) make_pair(a, b)const int INF = 0x3f3f3f3f;const int maxn = 1e3 + 7;const double eps = 1e-8;const int MOD = 1000000009;const double PI = acos(-1.0);struct node{    int x, v;}a[1010];LL sum[1010];LL dp[1010][1010][2];bool cmp(node a, node b){    return a.x < b.x;}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int n, st, v;    while (scanf("%d%d%d", &n, &v, &st) != EOF)    {        for (int i = 1; i <= n; i++)            scanf("%d%d", &a[i].x, &a[i].v);        a[n+1].x = st;        a[n+1].v = 0;        sort(a + 1, a + n + 2, cmp);        sum[0] = 0;        for (int i = 1; i <= n + 1; i++)            sum[i] = sum[i-1] + a[i].v;        int pos = 0;        for (int i = 1; i <= n + 1 && !pos; i++)            if (a[i].x == st) pos = i;        memset(dp, INF, sizeof(dp));        dp[pos][pos][0] = dp[pos][pos][1] = 0;        for (int i = pos; i >= 1; i--)            for (int j = pos; j <= n + 1; j++)            {                if (i == j) continue;                LL now = sum[i-1] + sum[n+1] - sum[j];                dp[i][j][0] = min(dp[i][j][0], dp[i+1][j][0] + (a[i+1].x - a[i].x) * (now + a[i].v));                dp[i][j][0] = min(dp[i][j][0], dp[i+1][j][1] + (a[j].x - a[i].x) * (now + a[i].v));                dp[i][j][1] = min(dp[i][j][1], dp[i][j-1][0] + (a[j].x - a[i].x) * (now + a[j].v));                dp[i][j][1] = min(dp[i][j][1], dp[i][j-1][1] + (a[j].x - a[j-1].x) * (now + a[j].v));            }        printf("%lld\n", v * min(dp[1][n+1][0], dp[1][n+1][1]));    }    return 0;}
0 0
原创粉丝点击