程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> ZOJ

ZOJ

編輯:關於C++
Food Delivery Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

Submit Status

Description

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

 

題意:在X-軸上有一個餐廳,以及有n個點要送外賣。餐廳坐標為x,工人的速度為V-1,也就是 1/v 米每分鐘(不是v米每分鐘)。給出n的點的x坐標和參數v,外賣沒送到,客戶就會不愉快,每一分鐘的不愉快指數增加v。問怎麼樣的送貨策略,使得所有客戶的總不愉悅指數和最小。

 

思路:區間DP。用一個數組 dp 維護。

DP的思路就是,如果要訪問完 [i, j] ,那麼它的子區間一定訪問完了。

dp[i][j][0] 表示當前 [ i, j ] 區間內已經全部送餐完畢,並且這個時刻工人位於區間左端,也就是坐標 i

dp[i][j][1] 表示當前 [ i, j ] 區間內已經全部送餐完畢,並且這個時刻工人位於區間又端,也就是坐標 j

狀態轉移方程:

以餐廳的位置 pos 為中間點,i 在餐廳pos位置的左邊,j 在餐廳pos位置的右邊。

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));

這個方程裡面,delay 表示區間 [i,j] 之外的所有點的v值之和,a[i+1].x-a[i].x 表示距離,dp[i+1][j][0]是已經求出來的子區間,加上(a[i+1].x-a[i].x)*(delay+a[i].v)),就得到 dp[i][j][0]。

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 %d", &n, &v, &x) != EOF)
    {
        for(int i = 1; i <= n; i++)
            scanf("%d %d", &a[i].x, &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<= 1; i--)
        {
            //i循環pos左邊
            for(int j = pos; j <= n; j++)
            {
                //j循環pos右邊
                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);
        //本題的v一定要留到後面來乘,中間DP的時候乘可能會溢出,導致wa
    }
    return 0;
}



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved