题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1227

题意:一维坐标上有n个点,位置已知,选出k(k <= n)个点,使得所有n个点与选定的点中最近的点的距离总和最小,求出最小值。

思路:

将点i的距离记为为dis[i],从i到j选出一点使此段距离和最小,则此点坐标为dis[(i + j) / 2]

cost[i][j]为从i到j选出一点距离和的最小值。则求cost[i][j]的代码如下:

  cost[i][j] = 0;
  for(int k = i; k <= j; k++){
    cost[i][j] += abs(dis[(i + j)/2] - dis[k]);
  }

dp[i][j]表示从1到i选择j个点的最小值,则状态转移方程如下:

dp[i][j] = min(dp[k][j - 1] + cost[k + 1][i]) (j - 1 <= k <i)

代码如下,注意边界情况的处理,表示我也很头疼边界情况

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = ;
LL dp[N][], cost[N][N];
int dis[N];
int main(){
int n, m, cs = ;
while(~scanf("%d %d", &n ,&m) && n && m ){
for(int i = ; i <= n ; i++){
scanf("%d", &dis[i]);
}
memset(cost, 0x3F, sizeof(cost));
memset(dp, 0x3F, sizeof(dp));
for(int i = ; i <= n ; i++){
for(int j = i; j <= n ;j++){
cost[i][j] = ;
for(int k = i; k <= j; k++){
cost[i][j] += abs(dis[(i + j)/] - dis[k]);
} }
}
for(int i = ; i <= n; i++){
dp[i][] = cost[][i];
}
for(int j = ; j <= m ; j++){
for(int i = j; i <= n; i++){
for(int k = j - ; k < i; k++){
dp[i][j] = min(dp[i][j], dp[k][j - ] + cost[k + ][i]);
}
}
}
cs++;
printf("Chain %d\nTotal distance sum = %I64d\n\n",cs,dp[n][m]);
}
return ;
}

HDU 1227 Fast Food的更多相关文章

  1. [ACM] HDU 1227 Fast Food (经典Dp)

    Fast Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  2. HDU 1227 Fast Food (DP)

    题目链接 题意 : 有n个饭店,要求建k个供应点,要求每个供应点一定要建造在某个饭店的位置上,然后饭店都到最近的供应点拿货,求出所有饭店到最近的供应点的最短距离. 思路 : 一开始没看出来是DP,后来 ...

  3. hdu 1227 Fast Food(DP)

    题意: X轴上有N个餐馆.位置分别是D[1]...D[N]. 有K个食物储存点.每一个食物储存点必须和某个餐厅是同一个位置. 计算SUM(Di-(离第i个餐厅最近的储存点位置))的最小值. 1 < ...

  4. hdu 4965 Fast Matrix Calculation(矩阵高速幂)

    题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...

  5. HDU 4965 Fast Matrix Calculation(矩阵高速幂)

    HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...

  6. hdu 4965 Fast Matrix Calculation

    题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...

  7. HDU 3577 Fast Arrangement (线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3577 题意不好理解,给你数字k表示这里车最多同时坐k个人,然后有q个询问,每个询问是每个人的上车和下车 ...

  8. HDU - 3577 Fast Arrangement 线段树

    Fast Arrangement Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. hdu 1227(动态规划)

    Fast Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

随机推荐

  1. 求最大公约数和小于n的所有质数

    //algorithm.h enum SWAP_TYPE{MEMORY, COMPLEX}; struct SIntArray { int *pData; int num; SIntArray():p ...

  2. window下安装mongodb及php mongo扩展

    1.安装mongoDB 下载最新版本的 MongoDB 下载地址:http://www.mongodb.org/downloads 解压文件到 D:\mongodb 解压后的文件列表如下: 创建数据存 ...

  3. 【System】shell 实现 bat 的pause功能

    read -rsp $'Press enter to continue...\n' 参考资料: http://stackoverflow.com/questions/92802/what-is-the ...

  4. 将数据导入带模板EXCEL

    在EXCEL模板里设置好样式和格式 点击事件 private void btnReport_Click(object sender, EventArgs e)        {            ...

  5. Unity3d 制作动态Mesh且可以随地面凹凸起伏

    适用情景:主角带着光环,光环用一张贴图,要贴在地面上,并且随地面凹凸起伏 //代码 using UnityEngine; using System.Collections; [RequireCompo ...

  6. Innodb 表空间卸载、迁移、装载

    从MySQL的Innodb特性中我们知道,Inndob的表空间有共享和独享的特点,如果是共享的.则默认会把表空间存放在一个文件中(ibdata1),当开启独享表空间参数Innodb_file_per_ ...

  7. 密码加SALT原理

    原来这个技术叫SALT,以前我们经常这么用 ============================================================================== ...

  8. gtk+2.24.0-glib-2.28.1-staticLib-mingw32-x86-2016-08-10.7z

    GTK_PATH=D:/MSYS/opt/gtk+2.24.0-staticLib b1-static.sh --------------------------------------------- ...

  9. JDK1.7 中的HashMap源码分析

    一.源码地址: 源码地址:http://docs.oracle.com/javase/7/docs/api/ 二.数据结构 JDK1.7中采用数组+链表的形式,HashMap是一个Entry<K ...

  10. Django~NewProject and APP

    New Project 1.新建 django-admin startproject mysite 2.运行 manage.py runserver 8080 New APP 1.manage.py ...