POJ 1160:Post Office 邮局经典DP】的更多相关文章

Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17168   Accepted: 9270 Description There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each villa…
<题目链接> 转载于:>>> 题目大意: 一条高速公路,有N个村庄,每个村庄均有一个唯一的坐标,选择P个村庄建邮局,问怎么选择,才能使每个村庄到其最近邮局的距离和最小?最后打印这个最小值. 思路:典型的DP问题. 当我们在v个村庄中只建一个邮局,可以推导出,只有邮局位于中间位置,距离和才最小:有一个特殊情况是,当村庄数为偶数,中间位置有两个村庄,经过计算,两个村庄的距离和相等,所以俩位置均可. 可以联想到,N个村庄建P个邮局,相当于每个邮局均有一个作用范围,该邮局位于其作用范…
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 positio…
Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15966   Accepted: 8671 Description There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each villa…
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22278 Accepted: 12034 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…
题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i][j]表示到第i个村庄为止建立j个邮局的最小距离和,dis[i][j]表示i~j之间建一个邮局的最小距离和,我们很容易得出状态转移方程:dp[i][j]=min{dp[k][j]+dis[k+1][i]}(k<i). 主要是dis[i][j]的预处理很巧妙,从别人的博客上看的“将邮局建在i~j中间即…
题意: 给出m个村庄及其距离,给出n个邮局,要求怎么建n个邮局使代价最小. 析:一般的状态方程很容易写出,dp[i][j] = min{dp[i-1][k] + w[k+1][j]},表示前 j 个村庄用 k 个邮局距离最小,w可以先预处理出来O(n^2),但是这个方程很明显是O(n^3),但是因为是POJ,应该能暴过去..= =,正解应该是对DP进行优化,很容易看出来,w是满足四边形不等式的,也可以推出来 s 是单调的,可以进行优化. 代码如下: #pragma comment(linker,…
题目链接 \(Description\) 一条直线上有n个村庄,位置各不相同.选择p个村庄建邮局,求每个村庄到最近邮局的距离之和的最小值. \(Solution\) 先考虑在\([l,r]\)建一个邮局,最优解肯定是建在中间. 这样\(mid\)两边对称,距离和是最小的:若建在\(mid-1\),(假设\(mid\)与\(mid-1\)相距\(1\))虽然左边\(mid-1\)个村庄\(dis\)都\(-1\)了,但是右边有\(mid\)个村庄的\(dis\)会\(+1\). 如果区间长度为偶数…
题目:poj1161Post Officeid=1160" target="_blank">点击打开链接 题意:给出一条直线上的n个坐标表示村庄的位置,然后要在上面建p个邮局.村民优先选择去近的邮局.问全部村庄去邮局的最小距离和是多少? 分类:区间dp 分析:对于随意一个村庄,仅仅有两种选择,要么在这儿建邮局.要么不建,我们能够预处理出来随意两件建立一个邮局的的最小距离w[i][j],而对于随意两点,建立一个邮局的最优方案是建立在两点的中位数上,即(i+j)/2.位置.…
Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15412   Accepted: 8351 Description There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each villa…