因为人的路线是来回走的,所以 当前已送的区间为[x,y],那么再送一次餐,已到区间为[x-1,y]或者为[x,y+1]。
区间DP。
dp[le][ri][k]表示当前已送完[le,ri]区间,如果k==0,那么人正在第le点,如果k==1,那么人正在第ri点上。
坑点:
1.速度为1/v,所以不用double表示
2.数据范围并没有明确给出,用long long。
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-1meters 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
Author: LI, Cheng
Contest: ZOJ Monthly, February 2011
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=1000 ;
int v;
int n,st;
struct Node
{
int x,ind,B;
bool operator<(const Node y)const
{
return x<y.x;
}
} a[maxn+7];
ll dp[maxn+6][maxn+6][2];
ll sum[maxn+6];
void update(ll & ans,ll x)
{
if(x<ans) ans=x;
}
ll time(int b,int c)
{
return (ll)abs(a[b].x-a[c].x)*v;
}
int main()
{
while(~scanf("%d%d%d",&n,&v,&st))
{
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].B);
a[i].ind=i;
}
n++;
a[n].x=st;a[n].ind=n;a[n].B=0;
sort(a+1,a+1+n);
sum[0]=0;
for(int i=1;i<=n;i++)
{
if(a[i].ind==n)
{
st=i;
}
sum[i]=sum[i-1]+a[i].B;
}
for(int le=st;le>=1;le--)
{
for(int ri=st;ri<=n;ri++)
{
for(int k=0;k<2;k++)
{
dp[le][ri][k]=1e15;
}
}
}
dp[st][st][0]=0;
dp[st][st][1]=0;
for(int le=st;le>=1;le--)
{
for(int ri=st;ri<=n;ri++)
{
for(int k=0;k<2;k++)
{
int now=k?ri:le;
if(le-1>=1)
{
update(dp[le-1][ri][0], dp[le][ri][k]+(sum[le-1]+(sum[n]-sum[ri] ) )*time(le-1,now) ) ;
}
if(ri+1<=n)
{
update(dp[le][ri+1][1],dp[le][ri][k]+(sum[le-1]+(sum[n]-sum[ri] ) )*time(now,ri+1) );
}
}
}
}
printf("%lld\n", min(dp[1][n][0],dp[1][n][1]) );
}
return 0;
}