POJ 1160 Post Office (动态规划)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 15412 | Accepted: 8351 |
Description
two positions is the absolute value of the difference of their integer coordinates.
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
order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.
Output
Sample Input
10 5
1 2 3 6 7 9 11 22 44 50
Sample Output
9
Source
题目大意:
有n个村庄,m个邮局。每一个村庄的位置坐标告诉你,如今要将m个邮局设立在这n个村庄里面,问你最小花费是多少?花费为每一个村庄到近期的邮局的距离和。
解题思路:
dp[i][j] 记录 i个邮局 j个村庄的最小花费,cost[k+1][j]。记录在k+1号村庄到 j 号村庄设立一个邮局的最小花费。
那么:dp[i][j]=min { dp[i][k]+cost[k+1][j] }
最后输出dp[m][n]就可以。
可是在k+1号村庄到 j 号村庄设立一个邮局的最小花费是多少呢?答案:中位数。设置在中间那个村庄就可以。
解题代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn=310;
const int maxm=40;
int cost[maxn][maxn],dp[maxm][maxn],a[maxn];
int n,m,s[maxn][maxn]; void input(){
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
memset(dp,-1,sizeof(dp));
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
cost[i][j]=0;
int mid=(i+j)/2;
for(int k=i;k<=j;k++){
cost[i][j]+=abs(a[k]-a[mid]);
}
}
}
} int DP(int c,int r){
if(dp[c][r]!=-1) return dp[c][r];
if(c==1) return cost[1][r];
int ans=(1<<30);
for(int i=1;i<r;i++){
int tmp=DP(c-1,i)+cost[i+1][r];
if(tmp<ans) ans=tmp;
}
return dp[c][r]=ans;
} void solve(){
cout<<DP(m,n)<<endl;
} int main(){
while(scanf("%d%d",&n,&m)!=EOF){
input();
solve();
}
return 0;
}
POJ 1160 Post Office (动态规划)的更多相关文章
- 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 (间隔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个村庄建邮局,问怎么选择,才能使每个村庄到其最近邮局的距离和最小?最 ...
- POJ.1160.Post Office(DP 四边形不等式)
题目链接 \(Description\) 一条直线上有n个村庄,位置各不相同.选择p个村庄建邮局,求每个村庄到最近邮局的距离之和的最小值. \(Solution\) 先考虑在\([l,r]\)建一个邮 ...
- POJ 1160 DP
题目: poj 1160 题意: 给你n个村庄和它的坐标,现在要在其中一些村庄建m个邮局,想要村庄到最近的邮局距离之和最近. 分析: 这道题.很经典的dp dp[i][j]表示建第i个邮局,覆盖到第j ...
随机推荐
- 2^x mod n = 1 【杭电-HDOJ-1395】 附题
/* 2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Android --Vibrator--震动服务
1.取得震动服务的句柄 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);或者vibrator = (Vibrator)getAppli ...
- 网页制作之JavaScript部分 1 - 语法(复制教材内容)
一.简介 1.JavaScript它是个什么东西? 它是个脚本语言,需要有宿主文件,他的宿主文件是html文件. 2.它与Java有什么关系? 没有什么直接联系,java是Sun公司(已经没有了,被O ...
- inheritAll 及 ant antfile案例分析
<?xml version="1.0"?> <project name="a" default="targeta"> ...
- java--继承的一些笔记
public class Person { public void display(){ System.out.println("Play Person..."); } stati ...
- 03-IOSCore - XML及解析、Plist
一.XML 可扩展标记语言 是什么?是一段有规范的字符串, 用在哪?用在任何地方 语法: * 结点Node <结点名 属性名="属性值"> 结点内容 </结点名& ...
- 国产CPU走到十字路口:谁来取代英特尔芯片?(少写了一个OpenPower)
国内的几支CPU研发力量各自选择的指令体系都有自己的优点和问题,选择其中的哪一支都会有对应的成本和风险.最终谁能担大任,且拭目以待. 文 | 瞭望智库特约科技观察员 王强 用上内置国产CPU的个人电脑 ...
- traceroute工作原理
traceroute, 也就是 trace route,跟踪路由.这个程序最早是Van Jacobson实现的.源代码在网上能够找到,只是我还没有去找.基本的原理是IP路由过程中对数据包TTL(T ...
- while和do while习题
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 练习 { ...
- android Gallery滑动不流畅的解决
import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; impo ...