ZOJ 3469 区间dp
2014-03-12 00:34
344 查看
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 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
题意:一条直线上,有一些需要送饭的客户,每一个客户有一个不满意度,随着时间的增加而增加,求使得所有客户不满意度最小值。、
dp[i][j][[0]表示送完[i,j]区间停在左边,dp[i][j][1]表示送完[i,j]区间停在右边。然后dp。
代码:
/* ***********************************************
Author :xianxingwuguan
Created Time :2014/3/12 0:09:36
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
ll dp[1010][1010][2];
struct node{
ll x,y;
}pp[1010];
bool cmp(node a,node b){
return a.x<b.x;
}
ll sum[1010];
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
ll n,r,P;
while(~scanf("%lld%lld%lld",&n,&r,&P)){
for(ll i=1;i<=n;i++)scanf("%lld%lld",&pp[i].x,&pp[i].y);
n++;pp
.x=P;pp
.y=0;
sort(pp+1,pp+n+1,cmp);
sum[0]=0;
for(ll i=1;i<=n;i++)
sum[i]=sum[i-1]+pp[i].y;
ll tmp;
for(ll i=1;i<=n;i++)
if(pp[i].x==P){
tmp=i;break;
}
for(ll i=1;i<=n;i++)
for(ll j=1;j<=n;j++)
dp[i][j][0]=dp[i][j][1]=INF;
dp[tmp][tmp][0]=dp[tmp][tmp][1]=0;
for(ll i=tmp;i>=1;i--)
for(ll j=tmp;j<=n;j++){
if(i==j)continue;
dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][0]+(sum[i]+sum
-sum[j])*(pp[i+1].x-pp[i].x));
dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(sum[i]+sum
-sum[j])*(pp[j].x-pp[i].x));
dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][0]+(sum[i-1]+sum
-sum[j-1])*(pp[j].x-pp[i].x));
dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][1]+(sum[i-1]+sum
-sum[j-1])*(pp[j].x-pp[j-1].x));
}
printf("%lld\n",r*min(dp[1]
[0],dp[1]
[1]));
}
return 0;
}
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 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
题意:一条直线上,有一些需要送饭的客户,每一个客户有一个不满意度,随着时间的增加而增加,求使得所有客户不满意度最小值。、
dp[i][j][[0]表示送完[i,j]区间停在左边,dp[i][j][1]表示送完[i,j]区间停在右边。然后dp。
代码:
/* ***********************************************
Author :xianxingwuguan
Created Time :2014/3/12 0:09:36
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
ll dp[1010][1010][2];
struct node{
ll x,y;
}pp[1010];
bool cmp(node a,node b){
return a.x<b.x;
}
ll sum[1010];
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
ll n,r,P;
while(~scanf("%lld%lld%lld",&n,&r,&P)){
for(ll i=1;i<=n;i++)scanf("%lld%lld",&pp[i].x,&pp[i].y);
n++;pp
.x=P;pp
.y=0;
sort(pp+1,pp+n+1,cmp);
sum[0]=0;
for(ll i=1;i<=n;i++)
sum[i]=sum[i-1]+pp[i].y;
ll tmp;
for(ll i=1;i<=n;i++)
if(pp[i].x==P){
tmp=i;break;
}
for(ll i=1;i<=n;i++)
for(ll j=1;j<=n;j++)
dp[i][j][0]=dp[i][j][1]=INF;
dp[tmp][tmp][0]=dp[tmp][tmp][1]=0;
for(ll i=tmp;i>=1;i--)
for(ll j=tmp;j<=n;j++){
if(i==j)continue;
dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][0]+(sum[i]+sum
-sum[j])*(pp[i+1].x-pp[i].x));
dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(sum[i]+sum
-sum[j])*(pp[j].x-pp[i].x));
dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][0]+(sum[i-1]+sum
-sum[j-1])*(pp[j].x-pp[i].x));
dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][1]+(sum[i-1]+sum
-sum[j-1])*(pp[j].x-pp[j-1].x));
}
printf("%lld\n",r*min(dp[1]
[0],dp[1]
[1]));
}
return 0;
}
相关文章推荐
- [百度面试] 2013百度笔试和面试分享(社会招聘-Android方向)
- 腾讯、百度、网易游戏、华为Offer及笔经面经
- JDK源代码分析聚集篇-------Collection(文明人应该排队)
- 网站中嵌入的一键分享
- 《超越自己》--刘墉
- 游戏引擎架构2:How,Why?
- SQLite优化方法
- 环境不同,心情也许就不同
- Android SQLite学习指南
- Hadoop 2.2.0 单节点安装
- 对比两个集合A和B,删除B中没有的A,新增A中没有的B
- linux nano
- 470计算机毕业设计
- 无向图的最短路径求解算法之——Dijkstra算法【转】
- hadoop学习之hadoop完全分布式集群安装
- C# 能否获取一个对象所占内存的大小?
- javascript笔记
- 时间按时区转换算法
- 【转】正确理解PHP程序编译时的错误信息
- poj1694 排序+DFS