【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 BiDispleasure 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个客人要让其送餐,送货员将以1/v的速度一家一家的送,但是每一个家庭都会有一个愤怒值。每过一分钟都会增加一倍的不开心值,现在要求出送完餐后最小的不开心值,

思路:区间dp,比较像一个关路灯问题类似的吧,

dp[i][j][k],表示在[i,j]区间内送饭的最小不满意度,k=0时表示停留在i点,k=1时表示停留在j点,

其中划分区间时,要加上等待的愤怒值。

下面直接给出状态转移方程:

 dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][0]+(delay+node[i].d)*(node[i+1].x-node[i].x));
 dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(delay+node[i].d)*(node[j].x-node[i].x));

 dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][0]+(delay+node[j].d)*(node[j].x-node[i].x));
 dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][1]+(delay+node[j].d)*(node[j].x-node[j-1].x));

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define inf 0x3ffffff
#define maxn 1010
using namespace std;

struct Node
{
    int x,d;
} node[maxn];

int dp[maxn][maxn][2];
int sum[maxn];

int cmp(Node a,Node b)
{
    return a.x<b.x;
}

int nu(int i,int j)
{
    if(i>j)
        return 0;
    return sum[j]-sum[i-1];
}

int solve(int n,int x)
{
    int res,delay;

    for(int i=0; i<n; i++)
        if(node[i].x==x)
        {
            res=i;
            break;
        }
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            dp[i][j][1]=dp[i][j][0]=inf;
    dp[res][res][1]=dp[res][res][0]=0;

    for(int i=res;i>=0;i--)
    {
        for(int j=res;j<n;j++)
        {
            if(i==j)
                continue;
            delay=nu(0,i-1)+nu(j+1,n-1);
            dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][0]+(delay+node[i].d)*(node[i+1].x-node[i].x));
            dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(delay+node[i].d)*(node[j].x-node[i].x));

            dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][0]+(delay+node[j].d)*(node[j].x-node[i].x));
            dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][1]+(delay+node[j].d)*(node[j].x-node[j-1].x));
        }
    }
    return min(dp[0][n-1][0],dp[0][n-1][1]);
}


int main()
{
    int n,v,x;
    int i;
    while(~scanf("%d%d%d",&n,&v,&x))
    {
        for(i=0;i<n;i++)
            scanf("%d%d",&node[i].x,&node[i].d);
        node[n].x=x;
        node[n].d=0;
        n++;
        sort(node,node+n,cmp);
        memset(sum,0,sizeof(sum));
        for(sum[0]=node[0].d,i=1;i<n;i++)
            sum[i]=sum[i-1]+node[i].d;
        printf("%d\n",solve(n,x)*v);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wentong_Xu/article/details/82261062