/*
* poj 1160 Post Office
* antsmall
* 2010-09-26
*/
#include<iostream>
using namespace std;
int dist[301][301];
int x[301];
int pv[31][301];
const int MIN_INT = 100000000;
void getDist(int v) {
memset(dist, 0, sizeof(dist));
for(int i = 1; i < v; i++) {
for(int j = i+1; j <= v;j++) {
int mid = (i+j)/2;
for(int k = i; k <= j; k++) {
dist[i][j] += abs(x[k] - x[mid]);
}
}
}
}
int solve(int v, int p) {
memset(pv, 0, sizeof(pv));
for(int i = 1; i <= v; i++) {
pv[1][i] = dist[1][i];
}
for(int i = 2; i <= p; i++) {
for(int j = i + 1; j <= v; j++) {
int min = MIN_INT;
for(int k = i - 1; k < j; k++) {
int tmp = pv[i-1][k] + dist[k+1][j];
if(tmp < min)
min = tmp;
}
pv[i][j] = min;
}
}
return pv[p][v];
}
int main() {
int v, p;
scanf("%d%d", &v, &p);
for(int i = 1; i <= v; i++) {
scanf("%d", &x[i]);
}
getDist(v);
printf("%d\n", solve(v,p));
//system("pause");
return 0;
}
分享到:
相关推荐
poj经典动态规划题目解题报告,包括经典的动态规划题目20多道,可以作为学习动态规划系统的资料,包括题目: Pku acm 1179 Polygon Pku acm 1125 Stockbroker Grapevine Pku acm 1160 post office Pku ...
5. 1160 Post Office:经典的最短路径问题,可以使用动态规划或Floyd-Warshall算法。 6. 1458 Common Subsequence:寻找两个字符串的最长公共子序列,经典动态规划问题。 7. 1953 World Cup Noise:可能需要计算某种...
* 1160 Post Office:本题目使用动态规划来计算邮局的最短距离。 * 1458 Common Subsequence:本题目使用动态规划来计算两个序列的最长公共子序列。 * 1579 Function Run Fun:本题目使用动态规划来计算函数的执行...
* 1160 Post Office * 1163 The Triangle * 1458 Common Subsequence * 1579 Function Run Fun * 1887 Testing the CATCHER * 1953 World Cup Noise * 2386 Lake Counting 简单、模拟题 简单、模拟题是 POJ 上的...
poj1160There is a straight highway with villages alongside the highway. The highway is represented as an integer axisPost offices will be built in someYou are to write a program which
- **1160 (Post Office)**:本题要求利用数据结构(如优先队列)来优化邮局选址问题。 以上是对这份题目总结中提到的部分算法类型的概述。每个算法都有其独特的应用场景和解决问题的方式,通过练习这些题目可以帮助...
相关题目有:1037 A decorative fence、1050 To the Max、1088 滑雪、1125 Stockbroker Grapevine、1141 Brackets Sequence、1159 Palindrome、1160 Post Office、1163 The Triangle、1458 Common Subsequence、1579...