ZOJ 3469 - Food Delivery (区间DP)

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个人点了外卖,店家在x点上。每个人在xi等待,等待一分钟会长bi个愤怒值。

问你送完后愤怒值最少是多少。


POINT:

要知道DP的区间肯定要包含店家这个点。不然没有意义。

DP[x][y][0]代表送完x到y这个区间后店家在x

DP[x][y][1]代表送完x到y这个区间后店家在y

愤怒值怎么算:每次把没送到的人的愤怒值全部加上。 即区间外的人。


DP做:
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define  LL long long
const LL maxn = 1e3+55;
const LL mod = 1e9+7;
const LL inf = 1e18;
LL to = 0;
struct node
{
	LL x,b;

	bool friend operator < (node a,node b)
	{
		return a.x<b.x;
	}

}a[maxn];

LL dp[1111][1111][2];
LL n,v,x;
LL sum[1111];


LL Fsum(LL l,LL r)
{
	return sum[n]-(sum[r]-sum[l-1]);
}

LL dis(LL l,LL r)
{
	return abs(a[r].x-a[l].x);
}

int main()
{

	while(~scanf("%lld %lld %lld",&n,&v,&x)){
		for(LL i=1;i<=n;i++){
			scanf("%lld %lld",&a[i].x,&a[i].b);
		}
		a[++n].x=x;
		a[n].b=0;
		sort(a+1,a+1+n);
		for(int i=1;i<=n;i++){
			sum[i]=sum[i-1]+a[i].b;
			for(int j=1;j<=n;j++)
				dp[i][j][0]=dp[i][j][1]=inf;
		}
		for(LL i=1;i<=n;i++){
			if(x==a[i].x){
				to=i;
				break;
			}
		}
		dp[to][to][0]=dp[to][to][1]=0;
		for(LL l=to;l>=1;l--){
			for(LL r=to;r<=n;r++){
				if(l==r) continue;
				dp[l][r][0]=min(dp[l][r][0],dp[l+1][r][0]+dis(l,l+1)*v*Fsum(l+1,r));
				dp[l][r][0]=min(dp[l][r][0],dp[l+1][r][1]+dis(l,r)*v*Fsum(l+1,r));
				dp[l][r][1]=min(dp[l][r][1],dp[l][r-1][0]+dis(l,r)*v*Fsum(l,r-1));
				dp[l][r][1]=min(dp[l][r][1],dp[l][r-1][1]+dis(r-1,r)*v*Fsum(l,r-1));


			}
		}

		printf("%lld\n",min(dp[1][n][0],dp[1][n][1]));
	}



}




DFS形式:

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define  LL long long
const LL maxn = 1e3+55;
const LL mod = 1e9+7;
const LL inf = 1e18;
LL to = 0;
struct node
{
	LL x,b;

	bool friend operator < (node a,node b)
	{
		return a.x<b.x;
	}

}a[maxn];

LL dp[1111][1111][2];
LL n,v,x;
LL sum[1111];
int vis[1111][1111];

void dfs(LL l,LL r)
{
	if(vis[l][r]) return;
	if(!(l<=to&&r>=to)) return;
	if(l==r&&l==to){
		dp[l][r][0]=dp[l][r][1]=0;
		return;
	}
	dfs(l+1,r);
	dfs(l,r-1);
	dp[l][r][0]=min(dp[l][r][0],dp[l+1][r][0]+(a[l+1].x-a[l].x)*v*(sum[n]-(sum[r]-sum[l])));
	dp[l][r][0]=min(dp[l][r][0],dp[l+1][r][1]+(a[r].x-a[l].x)*v*(sum[n]-(sum[r]-sum[l])));
	dp[l][r][1]=min(dp[l][r][1],dp[l][r-1][0]+(a[r].x-a[l].x)*v*(sum[n]-(sum[r-1]-sum[l-1])));
	dp[l][r][1]=min(dp[l][r][1],dp[l][r-1][1]+(a[r].x-a[r-1].x)*v*(sum[n]-(sum[r-1]-sum[l-1])));
	vis[l][r]=1;
}

int main()
{

	while(~scanf("%lld %lld %lld",&n,&v,&x)){
		for(LL i=1;i<=n;i++){
			scanf("%lld %lld",&a[i].x,&a[i].b);
		}
		a[++n].x=x;
		a[n].b=0;
		sort(a+1,a+1+n);
		for(int i=1;i<=n;i++){
			sum[i]=sum[i-1]+a[i].b;
			for(int j=1;j<=n;j++)
				vis[i][j]=0,dp[i][j][0]=dp[i][j][1]=inf;
		}
		for(LL i=1;i<=n;i++){
			if(x==a[i].x){
				to=i;
				break;
			}
		}
		dfs(1,n);
		printf("%lld\n",min(dp[1][n][0],dp[1][n][1]));
	}



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值