ZOJ 3469 Food Delivery

Source: Internet
Author: User

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3469


Food Delivery Time limit: 2 Seconds Memory Limit: 65536 KB

When we is focusing on solving problems, we usually prefer to stay in front of computers rather than go out for lunch. At the this time, we are delivery.

Suppose there was N people living in a straight street that was just lies on an x-coordinate axis. Theith person ' s coordinate isXi meters. And in the street there are a take-out restaurant which has coordinatesX meters. One day at lunchtime, each person takes a 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 BAC K to the restaurant. Your speed isV-1 meters per minute.

You know that the N people has different personal characters; Therefore they has different feeling on the time their food arrives. Their feelings is measured bydispleasure Index. At the beginning, thedispleasure Index for each person is 0. When waiting is the food, thei-th person would gainBi displeasure Index per minute.

If one's displeasure Index goes too high, he won't buy your food any more. So you need to keep the sum of all people'sdispleasure Index as low as possible on 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 was started with three integersn (1 <=n <=), v ( v > 0), x ( X >= 0), then N lines followed. Each line contains integers XI ( Xi >= 0), bi ( bi >= 0), which is D Escribed above.

You can safely assume this all numbers in the input and output would be less than 231-1.

Please process to the end-of-file.

Output

Which is the minimal sum of displeasure Indexin the for all test case. One test case per line.

Sample Input

5 1 0
1 1
2 2
3 3
4 4
5 5

Sample Output

55


Test instructions

A straight line has n personal point takeaway. Everyone has a coordinate of XI. Everyone has an unhappy value, every second of the unhappy value increases by one times, the initial value is 0. The takeaway brother initially on the point X, at a speed V-1 give this straight to the N-person to deliver the takeout. The sum of the total number of unhappy values is at least.

Problem Solving Ideas:

First put the starting position in the x-coordinate. Then from the starting position to the left and right to send meals.

Dp[i][j][k] indicates that the person from the first person to the J has already delivered the meal last stop at k== 0 on the left, k = = 1 on the right of the most dissatisfied value.

Dp[i][j][0] may be transferred from Dp[i+1][j][0],dp[i+1][j][1] (i+1 on I right, two on the left of I), dp[i][j][1] may be transferred from dp[i][j-1][0],dp[i][j-1][1]. Refer to the code for the specific state transfer equation.

And then special note is that each transfer, will increase a certain time, the back of the person to take the meal time is also late, that is, the influence of the latter must be considered in the current state, and for everyone behind the impact is the same, then the current transfer time t, followed by X (outside the i-j interval) people, The current state will be more 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));

Analysis of Complexity:

Complexity of Time: O (n?n)
Space complexity: 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;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ZOJ 3469 Food Delivery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.