zoukankan      html  css  js  c++  java
  • ZOJ3469 Food Delivery —— 区间DP

    题目链接:https://vjudge.net/problem/ZOJ-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. 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 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'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


    Author: LI, Cheng
    Contest: ZOJ Monthly, February 2011

    题解:

    题意:有n个人的住宅分布在一条线上且互不重叠,他们同时叫了同一家店的外卖。由于这些人都想尽快拿到外卖,所以等待的时间越长,他们就越不开心(每个人都有个不开心值)。外卖店也坐落在这条直线上,为了使得这些人的不开心总值最小,外卖小哥应该怎么派送呢?外卖小哥的速度为1/v,且忽略外卖小哥把外卖交给顾客的时间。

    1.可知:当外卖小哥送完顾客k的外卖后,它的下一个配送对象必须为与顾客k相邻且未派送的顾客l或者顾客r。为什么?因为如果不派送l或者r,而去派送较远的顾客,那么途中就必须要经过顾客l或者r,既然都经过了,那就肯定先把外卖送给他们啦。

    2.根据上述结论,我们就能进一步总结出:对于初始状态,为一个点,然后下一个目标就是与这个点相邻的点,经过这步之后,已经派送的对象就构成了一个区间,那么下一个派送的对象必定是与这个区间相邻的点,然后一直下去……,推到这里,就可以看出这是一个典型的区间DP模型了。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int MOD = 1e9+7;
    17 const int MAXN = 1e3+10;
    18 
    19 int n, v, x;
    20 int sum[MAXN];
    21 LL dp[MAXN][MAXN][2];
    22 
    23 struct node
    24 {
    25     int pos, val;
    26     bool operator<(const node &a)const{
    27         return pos<a.pos;
    28     }
    29 }q[MAXN];
    30 
    31 int main()
    32 {
    33     while(scanf("%d%d%d", &n,&v,&x)!=EOF)
    34     {
    35         for(int i = 1; i<=n; i++)
    36             scanf("%d%d", &q[i].pos, &q[i].val);
    37 
    38         q[++n].pos = x; q[n].val = 0;   //把餐馆加进去
    39         sort(q+1, q+1+n);           //根据位置进行排序
    40 
    41         int st;             //记录餐馆的下标
    42         sum[0] = 0;
    43         for(int i = 1; i<=n; i++)
    44         {
    45             sum[i] = sum[i-1]+q[i].val;
    46             if(q[i].pos==x) st = i;
    47         }
    48 
    49         for(int l = 1; l<=n; l++)   //初始化
    50         for(int r = l; r<=n; r++)
    51             dp[l][r][0] = dp[l][r][1] = INF;
    52 
    53         dp[st][st][0] = dp[st][st][1] = 0;
    54         for(int len = 2; len<=n; len++)
    55         for(int l = 1; l<=n-len+1; l++)
    56         {
    57             int r = l+len-1;
    58             dp[l][r][0] = min(dp[l][r][0], dp[l+1][r][0]+1LL*v*(q[l+1].pos-q[l].pos)*(sum[l]+sum[n]-sum[r]));
    59             dp[l][r][0] = min(dp[l][r][0], dp[l+1][r][1]+1LL*v*(q[r].pos-q[l].pos)*(sum[l]+sum[n]-sum[r]));
    60             dp[l][r][1] = min(dp[l][r][1], dp[l][r-1][1]+1LL*v*(q[r].pos-q[r-1].pos)*(sum[l-1]+sum[n]-sum[r-1]));
    61             dp[l][r][1] = min(dp[l][r][1], dp[l][r-1][0]+1LL*v*(q[r].pos-q[l].pos)*(sum[l-1]+sum[n]-sum[r-1]));
    62         }
    63 
    64         LL ans = min(dp[1][n][0], dp[1][n][1]);
    65         printf("%lld
    ", ans);
    66     }
    67 }
    View Code

     

  • 相关阅读:
    AsyncTask,MailTask,ScheduledTask
    Mysql的事务理解
    MySQL初识
    HTTP 的原理零散知识点
    SpringBoot简单搭建开发
    Android 的生命周期
    C51 虚拟元器件
    JavaSE 知识整合 (更新中……)
    java关键字篇
    Android开启网络权限
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7966111.html
Copyright © 2011-2022 走看看