poj 1160 Post Office(dp)
2014-11-07 21:53
387 查看
Post Office
Description
There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same
position. The distance between two positions is the absolute value of the difference of their integer coordinates.
Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village
and its nearest post office is minimum.
You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office.
Input
Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains
V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.
Output
The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.
Sample Input
Sample Output
题意:x轴上v个村庄,要建p个邮局,问每个点到它的最近距离的邮局的和最小为多少?
题解:设村庄x到邮局y最近,我们称y负责x。那么一个邮局一定是负责一个连续的区间。
dp[i][j]表示建了i个邮局,前j个村庄已经被邮局负责的最小花费。转移就是:
dp[i][j]=min(dp[i-1][k]+w(k+1,j)),0<=k<=j
w(i,j)表示一个邮局负责i到j的村庄的最小花费。
负责度O(p*v^2)。
注意:后台数据有v>300的数据。
代码如下:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 16131 | Accepted: 8757 |
There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same
position. The distance between two positions is the absolute value of the difference of their integer coordinates.
Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village
and its nearest post office is minimum.
You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office.
Input
Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains
V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.
Output
The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.
Sample Input
10 5 1 2 3 6 7 9 11 22 44 50
Sample Output
9
题意:x轴上v个村庄,要建p个邮局,问每个点到它的最近距离的邮局的和最小为多少?
题解:设村庄x到邮局y最近,我们称y负责x。那么一个邮局一定是负责一个连续的区间。
dp[i][j]表示建了i个邮局,前j个村庄已经被邮局负责的最小花费。转移就是:
dp[i][j]=min(dp[i-1][k]+w(k+1,j)),0<=k<=j
w(i,j)表示一个邮局负责i到j的村庄的最小花费。
负责度O(p*v^2)。
注意:后台数据有v>300的数据。
代码如下:
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #define nn 3100 #define inff 0x3fffffff using namespace std; int dp[nn][350]; int x[nn]; int sm[nn]; int n,m; int solve(int l,int r) { int id=(l+r)/2; return x[id]*(id-l+1)-sm[id]+sm[l-1]+sm[r]-sm[id]-x[id]*(r-id); } int main() { int i,j,k; while(scanf("%d%d",&n,&m)!=EOF) { sm[0]=0; for(i=1;i<=n;i++) { scanf("%d",&x[i]); sm[i]=sm[i-1]+x[i]; } for(i=1;i<=n;i++) dp[0][i]=inff; dp[0][0]=0; for(i=1;i<=m;i++) { dp[i][0]=0; for(j=1;j<=n;j++) { if(i>=j) { dp[i][j]=0; continue; } dp[i][j]=inff; for(k=0;k<j;k++) { dp[i][j]=min(dp[i][j],dp[i-1][k]+solve(k+1,j)); } } } printf("%d\n",dp[m] ); } return 0; }
相关文章推荐
- poj 1160 Post Office & SCAU 07校赛10320 Post Office ( dp )
- POJ-1160 Post office 四边形优化DP
- POJ1160 Post Office (四边形不等式优化DP)
- poj 1160 Post Office(DP-简单DP)
- POJ 1160 Post Office (水DP)
- POJ 1160 Post Office(dp)
- POJ 1160 Post Office(DP)
- poj 1160 Post Office (区间DP)
- poj 1160 post office------DP
- POJ 1160 Post Office (DP)
- dp四边形优化 poj 1160 Post Office题解
- poj-1160 post office (dp)
- POJ 1160 Post Office(经典DP)
- [DP优化] POJ 1160 Post Office
- POJ 1160 Post Office(四边形不等式优化DP)
- 【DP】 POJ 1160 Post Office
- Poj-1160 Post Office(经典dp
- [DP 四边形不等式优化] POJ 1160 Post Office
- [dp] poj1160 Post office
- poj 1160 Post Office(四边形不等式优化dp)