zoukankan      html  css  js  c++  java
  • Food Delivery 区间dp

    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

    ***************************************************************************************************************************

    经典状态转化dp[i][j][k],k是标志符

    ***************************************************************************************************************************

     1 /*
     2 dp[i][j][k]表示从i到j之间的花费,k表示从左还是右开始
     3 */
     4 #include<iostream>
     5 #include<string>
     6 #include<cstring>
     7 #include<cmath>
     8 #include<cstdio>
     9 #include<algorithm>
    10 #define inf 99999999999
    11 using namespace std;
    12 int dp[1101][1101][2];
    13 int sum[1101];
    14 struct node
    15 {
    16     int x,v;
    17 }st[11001];
    18 int min(int a,int b)
    19 {
    20     if(a>b)
    21     return b;
    22     return a;
    23 }
    24 bool cmp(node a,node b)
    25   {
    26       return a.x<b.x;
    27   }
    28 int X,n,speed;
    29 int i,j,k,res;
    30 int get_sum(int le,int ri)
    31   {
    32       if(le>ri)return 0;
    33       return sum[ri]-sum[le-1];
    34   }
    35 int main()
    36 {
    37     while(scanf("%d%d%d",&n,&speed,&X)!=EOF)
    38     {
    39         for(i=1;i<=n;i++)
    40         {
    41             scanf("%d%d",&st[i].x,&st[i].v);
    42 
    43         }
    44         st[++n].x=X;st[n].v=0;
    45          sort(st+1,st+n+1,cmp);
    46          memset(sum,0,sizeof(sum));
    47          for(i=1;i<=n;i++)
    48            sum[i]=sum[i-1]+st[i].v;
    49          for(i=1;i<=n;i++)
    50            for(j=1;j<=n;j++)
    51             dp[i][j][0]=dp[i][j][1]=inf;
    52             res=0;
    53         for(i=1;i<=n;i++)
    54           if(st[i].x==X)
    55             {
    56                 res=i;//find out restanrant
    57                 break;
    58             }
    59         //cout<<"res "<<res<<endl;
    60         dp[res][res][0]=0;//此处要初始化
    61         dp[res][res][1]=0;
    62         for(i=res;i>=1;i--)
    63          for(j=res;j<=n;j++)//从中间向两边开始
    64          {
    65             if(i==j)continue;
    66             int umt=get_sum(1,i-1)+get_sum(j+1,n);//0表示从左边开始,1表示从右边开始
    67             //cout<<umt<<endl;
    68             //cout<<dp[i][j][1]<<' '<<dp[i][j][0]<<endl;
    69             dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][0]+(umt+st[j].v)*(st[j].x-st[i].x));
    70             dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][1]+(umt+st[j].v)*(st[j].x-st[j-1].x));
    71             dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][0]+(umt+st[i].v)*(st[i+1].x-st[i].x));
    72             dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(umt+st[i].v)*(st[j].x-st[i].x));
    73            // cout<<dp[i][j][1]<<' '<<dp[i][j][0]<<endl;
    74         }
    75         int maxn=min(dp[1][n][0],dp[1][n][1]);
    76         printf("%d
    ",maxn*speed);
    77 
    78     }
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    电脑开机自动上线(电脑开机之后自动将电脑的远程端口映射到服务器,可以通过其他设备进行连接,实现随时访问自己的电脑)
    批处理(bat)用来监测Windows网络状态脚本
    js取消点击事件
    uni-app 几个实用的功能
    前端几个比较方便的正则验证
    element.ui时间选择器
    Vue pc端项目打包后在nginx中无法正常浏览,点击页面出现404
    微信小程序之选项卡的实现方法
    文本溢出显示省略号
    分享几道前端面试题(3)
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3359016.html
Copyright © 2011-2022 走看看