ZOJ-3469 Food Delivery (interval dp)

Source: Internet
Author: User

Food Delivery
Time Limit: 2000MS
Memory Limit: 65536KB
64bit IO Format: %lld &%llu

Submit Status

Description

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 is Xi 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 is V-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, the displeasure Index for each person is 0. When waiting is the food, thei-th person would gain Bidispleasure Index per minute.

If one's displeasure Index goes too high, he won't buy your food any more. So-need to keep the sum of all people'sdispleasure Index as low as possible on order to maximize your incom E. 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 DESC Ribed 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 ofdispleasure 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: There is a restaurant on the x-axis, and there are n points to take out for sale. The restaurant coordinates are X, the worker's speed is v-1, that is 1/v meters per minute (not v meters per minute). Given the x-coordinate and the parameter V of the point of N, the customer will be unhappy when the takeaway is not delivered, and the unpleasant index per minute is increased by V. Ask how the delivery strategy makes all the customer's total not pleasant index and minimum.


Idea: Interval DP. With an array of DP maintenance.

DP's idea is that if you want to access [I, j], then its sub-range must be visited.

Dp[i][j][0] indicates that the current [I, j] Interval has been completely delivered and the worker is at the left end of the interval, which is the coordinate I

DP[I][J][1] indicates that the current [I, j] Interval has been completely delivered, and at this moment the worker is at the end of the range, which is the coordinate J

State transition equation:

To the location Pos of the restaurant is the midpoint, I is at the left of the restaurant POS position , J to the right of the restaurant POS location.

Dp[i][j][0] = min (dp[i][j][0], dp[i+1][j][0]+ (a[i+1].x-a[i].x) * (DELAY+A[I].V));

In this equation, the delay represents the sum of the V values of all points outside the interval [i,j],a[i+1].x-a[i].x represents the distance, Dp[i+1][j][0] is the sub-range that has been calculated, plus (a[i+1). x-a[i].x) * (DELAY+A[I].V)), get dp[i][j][0].



#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <string > #include <algorithm> #include <queue> #include <stack>using namespace std;const int INF = 1<< 29;const Double PI = ACOs ( -1.0); const double e = 2.718281828459;const double eps = 1e-8;const int maxn = 1010;int DP[MAXN] [MAXN]    [2];int sum[maxn];struct node{int x; int v;} A[maxn];int CMP (node x, node Y) {return x.x < y.x;}    int Delay (int l, int r) {if (L > R) return 0; return SUM[R]-SUM[L-1];}    int main () {//freopen ("In.txt", "R", stdin);    Freopen ("OUT.txt", "w", stdout);    int n, v, x; while (scanf ("%d%d", &n, &v, &x)! = EOF) {for (int i = 1; I <= n; i++) scanf ("%d%d        ", &a[i].x, &AMP;A[I].V);        n++;        a[n].x = x;        A[N].V = 0;        Sort (a+1, a+1+n, CMP);        Sum[0] = 0;        for (int i = 1; I <= n; i++) sum[i] = SUM[I-1]+A[I].V; int POS= 1;                for (int i = 1; I <= n; i++) {if (a[i].x = = x) {pos = i;                cout<<pos<<endl;            Break                }} for (int i = 1, i <= N; i++) {for (int j = 1; J <= N; j + +) {            Dp[i][j][0] = dp[i][j][1] = INF;        }} Dp[pos][pos][0] = dp[pos][pos][1] = 0;                for (int i = pos; I >= 1; i--) {//i loop pos left for (int j = pos; J <= N; j + +) {                J Loop pos Right int delay = delay (1, i-1) +delay (j+1, N);                if (i = = j) Continue;                Dp[i][j][0] = min (dp[i][j][0], dp[i+1][j][0]+ (a[i+1].x-a[i].x) * (DELAY+A[I].V));                Dp[i][j][0] = min (dp[i][j][0], dp[i+1][j][1]+ (a[j].x-a[i].x) * (DELAY+A[I].V));                Dp[i][j][1] = min (dp[i][j][1], dp[i][j-1][0]+ (a[j].x-a[i].x) * (DELAY+A[J].V)); Dp[i][j][1] = mIn (Dp[i][j][1], dp[i][j-1][1]+ (a[j].x-a[j-1].x) * (DELAY+A[J].V));        }} printf ("%d\n", Min (dp[1][n][0], dp[1][n][1]) *v); The V of the subject must be left behind to multiply, when the middle DP may overflow, leading to WA  } return 0;


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

ZOJ-3469 Food Delivery (interval dp)

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.