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来说,很常见的方法就是把原来的环从任意两点断开(注意并不是直接删掉这条边),在复制一条一模一样的链在这条链的后方,当做 ...
随机推荐
- CentOS 7 Git安装
Git安装 yum -y install git 安装后,在srv目录下建立Git的目录. 初始化一个git空仓库 git init --bare project.git 增加用于访问git仓库的用户 ...
- Redis学习手册(主从复制)
一.Redis的Replication: 这里首先需要说明的是,在Redis中配置Master-Slave模式真是太简单了.相信在阅读完这篇Blog之后你也可以轻松做到.这里我们还是先列出一些理 ...
- 使用 Git Hooks 实现自动项目部署
最近在某服务器上面搭建 git 开发和部署环境,git 开发环境很简单,按照 ProGit 一书的相关知识就可以轻松搞定,实现了类似 Github 的使用 SSH + 私有 Clone 的方式. 关于 ...
- CSS3中的px,em,rem,vh,vw辨析
1.px:像素,精确显示 2.em:继承父类字体的大小,相当于"倍",如:浏览器默认字体大小为16px=1em,始终按照div继承来的字体大小显示,进场用于移动端 em换算工具:h ...
- 如何将Sphinx生成的html文档集成进入Django
参考 http://stackoverflow.com/questions/10594618/django-and-sphinx-how-to-view-the-html-sphinx-generat ...
- 《Neural Network and Deep Learning》_chapter4
<Neural Network and Deep Learning>_chapter4: A visual proof that neural nets can compute any f ...
- 第18讲——ActiveX控件
1,容器应用程序是可以嵌入或链接对象的应用程序.服务器应用程序是创建对象并且当对象被双击时,可以被启动的应用程序. 2,可以安装一个TstCon32来测试ActiveX控件 3,可以用 Invalid ...
- SQL 表连接查询出现重复列,由此理清LEFT JOIN、INNER JOIN的区别
1.先创建两个临时表,并插入数据 CREATE TABLE #TEMP1( ID INT IDENTITY(1,1) PRIMARY KEY, name NVARCHAR(50)) CREATE TA ...
- iOS 创建模型时自动生成属性
转载 mark666(简书作者), 链接:http://www.jianshu.com/p/63ee533a7705 我们在创建模型的时候,常常要写一大堆恶心的@property(nonatomic, ...
- JS获取URL参数
$(function () { var action = getUrlParam("action"); alert(action) }); //根据URL中的参数获取值 funct ...