码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 3469 Food Delivery

时间:2015-08-16 09:27:17      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=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. Theith person‘s coordinate is Xi meters. And in the street there is a take-out restaurant which has coordinatesX 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 theN 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 byDispleasure Index. At the beginning, the Displeasure Index for each person is 0. When waiting for the food, theith 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‘sDispleasure Index as low as possible in order to maximize your income. Your task is to find the minimal sum ofDispleasure Index.

Input

The input contains multiple test cases, separated with a blank line. Each case is started with three integersN ( 1 <= N <= 1000 ), V ( V > 0), X (X >= 0 ), then N lines followed. Each line contains two integersXi ( 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个人点外卖。每个人都有一个坐标xi。每个人都有一个不开心值,每过一秒不开心值增加一倍,初始值都为0。外卖小哥初始在点x上,以速度v-1给这直线上的n个人送外卖。求不开心值总和最少多少。

解题思路:

先把起始位置放到x坐标里面。然后从起始位置开始向左右送餐.

设dp[i][j][k]表示从第i个人到第j个人都已经送完餐最后停在 k== 0 在左边,k == 1 在右边的最不满值。

dp[i][j][0] 可能从dp[i+1][j][0],dp[i+1][j][1] (i+1在i右边,两个都在i左边)转移而来,dp[i][j][1]可能从dp[i][j-1][0],dp[i][j-1][1]转移而来。具体状态转移方程参见代码。

然后特别要注意的是每转移一次,都会增加一定的时间,后面的人拿到餐的时间也晚了,也就是说必须把对后面的影响考虑在当前状态下,并且对后面的每个人影响都是一样的,那么当前转移用时t,后面还有x(i-j区间之外)人,当前的状态就要多加 x * t,

人在左边,目标也在左边的状态转移方程:

dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][0]+(sum[i]+sum[n]-sum[j])*(ord[i+1].x-ord[i].x));

人在左边,目标在右边的状态转移方程:

dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(sum[i]+sum[n]-sum[j])*(ord[j].x-ord[i].x));

人在右边,目标也在右边的状态转移方程:

dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][0]+(sum[i-1]+sum[n]-sum[j-1])*(ord[j].x-ord[i].x));

人在右边,目标在左边的状态转移方程:

dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][1]+(sum[i-1]+sum[n]-sum[j-1])*(ord[j].x-ord[j-1].x));

复杂度分析:

时间复杂度 :O(n?n)
空间复杂度 :O(n?n)


#include<iostream>
 #include<cstdio>
 #include<cstring>
 #include<cmath>
 #include<algorithm>
 using namespace std;
 #define INF 1e9
 const int maxn=1010;
 int sum[maxn];
 int dp[maxn][maxn][3];
 struct node{
    int x;
    int b;
}a[maxn];

bool cmp(node a,node b)
{
    return a.x<b.x;
}
int main()
{
    int n,v,x;
    while(cin>>n>>v>>x)
    {
        for(int i=1;i<=n;i++)
        {
            cin>>a[i].x>>a[i].b;
        }
        n++;
        a[n].x=x;
        a[n].b=0;
        sort(a+1,a+n+1,cmp);
        for(int i=0;i<=n;i++){
            for(int j=0;j<=n;j++){
                dp[i][j][0]=dp[i][j][1]=INF;
           }
        }
        sum[0]=0;
        for(int i=1;i<=n;i++)
        {
            sum[i] =sum[i-1]+a[i].b;
        }
        int res;
        for(int i=1;i<=n;i++)
        {
            if(a[i].x==x){
                res=i;
                break;
            }
        }
        dp[res][res][0]=dp[res][res][1]=0;
        for(int i=res;i>=1;i--)
        {
            for(int j=res;j<=n;j++)
            {
                if(i==j) continue;
                dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][0]+(sum[i]+sum[n]-sum[j])*(a[i+1].x-a[i].x));
                dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(sum[i]+sum[n]-sum[j])*(a[j].x-a[i].x));
                dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][0]+(sum[i-1]+sum[n]-sum[j-1])*(a[j].x-a[i].x));
                dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][1]+(sum[i-1]+sum[n]-sum[j-1])*(a[j].x-a[j-1].x));
            }
        }
        cout<<v*min(dp[1][n][0],dp[1][n][1]) << endl;
    }
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

ZOJ 3469 Food Delivery

标签:

原文地址:http://blog.csdn.net/hellohelloc/article/details/47699599

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!