POJ 1160 题解
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18835 | Accepted: 10158 |
Description
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
Output
Sample Input
10 5
1 2 3 6 7 9 11 22 44 50
Sample Output
9
Source
#include "cstdio"
#include "cstring"
#include "cstdlib"
#include "iostream" using namespace std ;
const int INF = ;
const int maxN = 1e3 ; int d [ maxN ] , f [ maxN ][ maxN ] , cost[ maxN ][ maxN ] ; int gmin ( int x , int y ) { return x > y ? y : x ; } inline int INPUT ( ) {
int x = , F = ; char ch = getchar ( ) ;
while ( ch < '' || '' < ch ) { if ( ch == '-' ) F = - ; ch = getchar ( ) ; }
while ( '' <= ch && ch <= '' ) { x = ( x << ) + ( x << ) + ch - '' ; ch = getchar ( ) ; }
return x * F ;
} void calc ( int M ) {
for ( int i= ;i<=M ; ++i ) {
for ( int j=i+ ; j<=M ; ++j ){
int Dis = ;
int mid = ( i + j ) >> ;
for ( int k=i ; k<mid ; ++k )Dis += d[mid] - d[k] ;
for ( int k=mid+ ; k<=j ; ++k )Dis += d[k] - d[mid] ;
cost[ i ][ j ] = Dis ;
}
}
} void Init_2 ( int N , int M ) {
for ( int i= ; i<=M ; ++i ) {
for ( int j= ; j<=N ; ++j ) {
if ( j== ) f[ i ][ j ] = cost[ ][ i ] ;
else f[ i ][ j ] = INF ;
}
}
}
void Init_1 ( int M ) {
for ( int i= ; i<=M; ++i )
for ( int j=i+ ; j<=M; ++j )
cost[ i ][ j ] = INF ;
} int main ( ) {
int M = INPUT ( ) , N = INPUT ( ) ;
for ( int i= ; i<=M ; ++i )
d[ i ] = INPUT ( ) ;
Init_1( M ) ;
calc ( M ) ;
Init_2 ( N , M ) ;
for ( int i= ; i<=M ; ++i ) {
for ( int j= ; j<=N ; ++j ) {
for ( int k= ; k<i ; ++k ){
f[ i ][ j ] = gmin ( f[ i ][ j ] , f[ k ][ j - ] + cost[ k + ][ i ] ) ;
}
}
}
printf ( "%d\n" , f[ M ][ N ] ) ;
return ;
}
2016-10-20 21:01:34
POJ 1160 题解的更多相关文章
- POJ 1160 Post Office(区间DP)
Description There is a straight highway with villages alongside the highway. The highway is represen ...
- POJ 1160 DP
题目: poj 1160 题意: 给你n个村庄和它的坐标,现在要在其中一些村庄建m个邮局,想要村庄到最近的邮局距离之和最近. 分析: 这道题.很经典的dp dp[i][j]表示建第i个邮局,覆盖到第j ...
- POJ 1160 经典区间dp/四边形优化
链接http://poj.org/problem?id=1160 很好的一个题,涉及到了以前老师说过的一个题目,可惜没往那上面想. 题意,给出N个城镇的地址,他们在一条直线上,现在要选择P个城镇建立邮 ...
- POJ 1160 Post Office(DP+经典预处理)
题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i] ...
- POJ 1160 Post Office (四边形不等式优化DP)
题意: 给出m个村庄及其距离,给出n个邮局,要求怎么建n个邮局使代价最小. 析:一般的状态方程很容易写出,dp[i][j] = min{dp[i-1][k] + w[k+1][j]},表示前 j 个村 ...
- poj 3744 题解
题目 题意: $ yyf $ 一开始在 $ 1 $ 号节点他要通过一条有 $ n $ 个地雷的道路,每次前进他有 $ p $ 的概率前进一步,有 $ 1-p $ 的概率前进两步,问他不领盒饭的概率. ...
- poj 3061 题解(尺取法|二分
题意 $ T $ 组数据,每组数据给一个长度 $ N $ 的序列,要求一段连续的子序列的和大于 $ S $,问子序列最小长度为多少. 输入样例 2 10 15 5 1 3 5 10 7 4 9 2 8 ...
- POJ 3977 题解
题目 Given a list of N integers with absolute values no larger than \(10^{15}\), find a non empty subs ...
- DP中环形处理 +(POJ 1179 题解)
DP中环形处理 对于DP中存在环的情况,大致有两种处理的方法: 对于很多的区间DP来说,很常见的方法就是把原来的环从任意两点断开(注意并不是直接删掉这条边),在复制一条一模一样的链在这条链的后方,当做 ...
随机推荐
- c#在字符串中计算加减乘除...
DataTable dt = new DataTable(); Response.Write(dt.Compute("1+1*5", ...
- python基础七
subprocess subprocess是专门用来替代os.system;os.spawn更加的先进. 但是subprocess.run()是在python3.5之后才出现的 实例 >> ...
- toast组件小结
简介:toast是"吐司"的意思,它属于android杂项组件,是一个简单的消息提示框,类似于javascript中的alert. 作用 显示文本 显示图片 显示图文 3.常用方法 ...
- PageRank的java实现
一个网络(有向带权图)中节点u的PageRank的计算公式: PR(u)表示节点u的PageRank值,d为衰减因子(damping factor)或阻尼系数,一般取d=0.85,N为网络中的节点总数 ...
- java并行计算Fork和Join的使用
Java在JDK7之后加入了并行计算的框架Fork/Join,可以解决我们系统中大数据计算的性能问题.Fork/Join采用的是分治法,Fork是将一个大任务拆分成若干个子任务,子任务分别去计算,而J ...
- selenium page object & Page Factory
package demo; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa ...
- 在MonthCalendar控件中选中日期
Calendar.MONTH Calendar now=Calendar.getInstance();System.out.print(now.get(Calendar.MONTH));得到的月份少1 ...
- mysql 5.7.14 免安装配置方法教程
仅供参考 一.下载 1. 进入mysql官网,下载Mysql-5.7.14,下载地址:http://dev.mysql.com/downloads/mysql/ 2.将下载好的文件解压到自定义目录 二 ...
- 获取json数据
通过异步获取json来展示数据表格,性能提高不少.实例如下: 前台: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999 ...
- svn: how to set the executable bit on a file?
http://stackoverflow.com/questions/17846551/svn-how-to-set-the-executable-bit-on-a-file svn uses pro ...