POJ——T 1160 Post Office
http://poj.org/problem?id=1160
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20218 | Accepted: 10899 |
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 <cstring>
#include <cstdio> inline void read(int &x)
{
x=; register char ch=getchar();
for(; ch>''||ch<''; ) ch=getchar();
for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
} const int N();
#define min(a,b) (a<b?a:b)
int n,m,x[N],minx[N][N],f[N][N]; int Presist()
{
// freopen("post.in","r",stdin);
// freopen("post.out","w",stdout); read(n),read(m);
if(m==n) { puts(""); return ; }
for(int i=; i<=n; ++i) read(x[i]);
for(int i=; i<=n; ++i)
for(int j=i+; j<=n; ++j)
minx[i][j]=minx[i][j-]+x[j]-x[i+j>>];
memset(f,/,sizeof(f));
for(int i=; i<=n; ++i)
f[i][]=minx[][i],f[i][i]=;
for(int k=; k<=m; ++k)
for(int i=; i<=n; ++i)
{
for(int j=i-; j; --j)
f[i][k]=min(f[i][k],f[j][k-]+minx[j+][i]);
}
printf("%d\n",f[n][m]);
return ;
} int Aptal=Presist();
int main(){;}
POJ——T 1160 Post Office的更多相关文章
- 【POJ】1160 Post Office
http://poj.org/problem?id=1160 题意:直线上有n个城市,其中有p个城市有邮局,问如何建p个邮局使得每个城市到最近的邮局和最小.(n<=300, p<=30&a ...
- POJ 1160 Post Office(区间DP)
Description There is a straight highway with villages alongside the highway. The highway is represen ...
- POJ 1160 Post Office(DP+经典预处理)
题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i] ...
- POJ 1160 Post Office (动态规划)
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15412 Accepted: 8351 Desc ...
- poj 1160 Post Office (间隔DP)
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15966 Accepted: 8671 Desc ...
- [IOI 2000]POJ 1160 Post Office
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22278 Accepted: 12034 Descrip ...
- POJ 1160 Post Office (四边形不等式优化DP)
题意: 给出m个村庄及其距离,给出n个邮局,要求怎么建n个邮局使代价最小. 析:一般的状态方程很容易写出,dp[i][j] = min{dp[i-1][k] + w[k+1][j]},表示前 j 个村 ...
- POJ 1160 Post Office
题意:有n个村庄,要在其中m个村庄里建邮局,每个村庄去邮局的代价为当前村庄到最近的一个有邮局村庄的路程,问总最小代价是多少. 解法:dp.dp[i][j]表示在前j个村庄建立i个邮局后的代价,则状态转 ...
- poj 1160 Post Office 【区间dp】
<题目链接> 转载于:>>> 题目大意: 一条高速公路,有N个村庄,每个村庄均有一个唯一的坐标,选择P个村庄建邮局,问怎么选择,才能使每个村庄到其最近邮局的距离和最小?最 ...
随机推荐
- P1272 重建道路(树形dp)
P1272 重建道路 题目描述 一场可怕的地震后,人们用N个牲口棚(1≤N≤150,编号1..N)重建了农夫John的牧场.由于人们没有时间建设多余的道路,所以现在从一个牲口棚到另一个牲口棚的道路是惟 ...
- 字典(Trie树)
4189 字典 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 最经,skyzhong得到了一本好厉害的字典,这个 ...
- jmeter 3.x plugins 的使用
JMeter Plugins 一直以来,JMeter Plugins为我们提供了很多高价值的JMeter插件,比如: 用于服务器性能监视的PerfMon Metrics Collector 用于建立压 ...
- 涨知识 - II
计算机网络相关 1.在无盘工作站向服务器申请IP地址时,使用的是( )协议. A.ARP B.RARP C.ICMP D.IGMP 解析: ARP(地址解析协议)是设备通过自己知道的IP地址来 ...
- Android 自己搭建一个直播系统吧
服务端用 SRS(Simple Rtmp Server),在这里下载simple-rtmp-server需要Linux系统最好是Ubuntu,装个Ubuntu虚拟机就行了在Linux里,解压缩SRS ...
- 使用WindowBuilder设计Swing程序
Swing程序表示Java的客户端窗体程序,除了通过手动编写代码的方式设计Swing程序之外,Eclipse中还提供了一种WindowBuilder工具,该工具是一种非常好用的Swing可视化开发工具 ...
- 注解是建立在class文件基础上的东西,同C语言的宏有异曲同工的效果
注解是建立在class文件基础上的东西,同C语言的宏有异曲同工的效果 https://www.cnblogs.com/deman/p/5519901.html @是java注解,即annotation ...
- CAD绘制一个线型标注(com接口VB语言)
主要用到函数说明: _DMxDrawX::DrawDimRotated 绘制一个线型标注.详细说明如下: 参数 说明 DOUBLE dExtLine1PointX 输入第一条界线的起始点X值 DOUB ...
- drf07 过滤 排序 分页 异常处理 自动生成接口文档
4. 过滤Filtering 对于列表数据可能需要根据字段进行过滤,我们可以通过添加django-fitlter扩展来增强支持. pip install django-filter 在配置文件sett ...
- 使用MySQL Yum存储库的快速指南【mysql官方文档】
使用MySQL Yum存储库的快速指南 抽象 MySQL Yum存储库提供用于在Linux平台上安装MySQL服务器,客户端和其他组件的RPM包.这些软件包还可以升级和替换从Linux发行版本机软件存 ...