[ACM] HDU 1227 Fast Food (经典Dp)
Fast Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2173 Accepted Submission(s): 930
ingredients. Naturally, these depots should be placed so that the average distance between a restaurant and its assigned depot is minimized. You are to write a program that computes the optimal positions and assignments of the depots.
To make this more precise, the management of McBurger has issued the following specification: You will be given the positions of n restaurants along the highway as n integers d1 < d2 < ... < dn (these are the distances measured from the company's headquarter,
which happens to be at the same highway). Furthermore, a number k (k <= n) will be given, the number of depots to be built.
The k depots will be built at the locations of k different restaurants. Each restaurant will be assigned to the closest depot, from which it will then receive its supplies. To minimize shipping costs, the total distance sum, defined as
must be as small as possible.
Write a program that computes the positions of the k depots, such that the total distance sum is minimized.
one integer each, giving the positions di of the restaurants, ordered increasingly.
The input file will end with a case starting with n = k = 0. This case should not be processed.
Output a blank line after each test case.
6 3
5
6
12
19
20
27
0 0
Chain 1
Total distance sum = 8
解题思路:
题意为 一条路上有 n个商店,每一个商店有一个x坐标位置,有k个仓库,要把k个仓库安放在n个位置中的k个上面,每一个商店都向近期的仓库来获得补给,求怎么安放这k个仓库,使得每一个商店到相应仓库的距离仅仅和加起来最小,输出最小值。
解决本题要意识到两点:
1. 假设要在第i个位置和第j个位置之间安放仓库,那么要把它安放在 (i+j)/2 个位置上,(i+j)/2为整数, 才干保证从i到j个商店到仓库之间的距离之和最短。
2.假设依照题意把k个仓库安放在n个位置上,使得距离和最短,这样求得了最小值, 那么一定符合题意:每一个商店都向近期的仓库来获得补给,由于假设不是向近期的,距离和肯定不是最短
用dp[i][j] 代表 前j个商店,有i个仓库
那么状态转移方程为:
dp [ i ] [ j ] = min ( dp [ i ] [ j ] , dp [ i-1 ] [ k] + cost [ k+1 ] [ j ] ) i-1<=k<=j-1
dp[i][j] 要从前一个状态推出来,及前k个商店有i-1个仓库,k是不确定的,但能够确定它的范围,最小是i-1 (一个商店位置上放一个仓库),最大是j-1 ( 把第i个仓库放在第j个位置上) , cost [ i ] [ j ]为在i ,j之间放一个仓库的最小距离和,即前面提到的第1点。
代码:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <string.h>
using namespace std;
int n,k;
int dp[32][210];//dp[i][j]前j个商店有i个仓库
int dis[210];
const int inf=0x3f3f3f3f; int cost(int i,int j)
{
int ans=0;
int mid=dis[(i+j)/2];
for(int k=i;k<=j;k++)
ans+=abs(dis[k]-mid);
return ans;
} int main()
{
int c=1;
while(scanf("%d%d",&n,&k)!=EOF)
{
if(!n||!k)
break;
for(int i=1;i<=n;i++)
scanf("%d",&dis[i]);
memset(dp,inf,sizeof(dp));
for(int i=1;i<=n;i++)
dp[1][i]=cost(1,i);
for(int i=2;i<=k;i++)//第i个仓库
for(int j=1;j<=n;j++)//前j个商店
{
for(int k=i-1;k<=j-1;k++)
dp[i][j]=min(dp[i][j],dp[i-1][k]+cost(k+1,j));
} printf("Chain %d\n",c++);
printf("Total distance sum = %d\n",dp[k][n]);
printf("\n");
}
return 0;
}
[ACM] HDU 1227 Fast Food (经典Dp)的更多相关文章
- hdu 1227 Fast Food(DP)
题意: X轴上有N个餐馆.位置分别是D[1]...D[N]. 有K个食物储存点.每一个食物储存点必须和某个餐厅是同一个位置. 计算SUM(Di-(离第i个餐厅最近的储存点位置))的最小值. 1 < ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- HDU 1227 Fast Food
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1227 题意:一维坐标上有n个点,位置已知,选出k(k <= n)个点,使得所有n个点与选定的点中 ...
- HDU 1227 Fast Food (DP)
题目链接 题意 : 有n个饭店,要求建k个供应点,要求每个供应点一定要建造在某个饭店的位置上,然后饭店都到最近的供应点拿货,求出所有饭店到最近的供应点的最短距离. 思路 : 一开始没看出来是DP,后来 ...
- [ACM] hdu 4405 Aeroplane chess (概率DP)
Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 ...
- [ACM] hdu 5045 Contest (减少国家Dp)
Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...
- HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4064 Problem Description Carcassonne is a tile-based ...
- HDU 2859 Phalanx(对称矩阵 经典dp样例)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2859 Phalanx Time Limit: 10000/5000 MS (Java/Others) ...
- HDU 4616 Game(经典树形dp+最大权值和链)
http://acm.hdu.edu.cn/showproblem.php?pid=4616 题意:给出一棵树,每个顶点有权值,还有存在陷阱,现在从任意一个顶点出发,并且每个顶点只能经过一次,如果经过 ...
随机推荐
- 为智能硬件提供一站式解决方案——机智云GoKit评测
为智能硬件提供一站式解决方案——机智云GoKit评测 2014年12月24日 作者: ladouyu 3 17,414+ 4 EMW3162GoKit开发板STM32F103智能硬件机智云2.0 从物 ...
- A Game of Thrones(7) -Arya
Arya’s stitches were crooked again. She frowned down at them with dismay and glanced over to where h ...
- SWT的TableVierer的使用一
1,简单显示,表格的式样见注释中的内容 import org.eclipse.jface.viewers.TableViewer;import org.eclipse.swt.SWT;import o ...
- 业务系统需要什么样的ID生成器
业务系统需要什么样的ID生成器 ID 生成器在微博我们一直叫发号器,微博就是用这样的号来存储,而我微博里讨论的时候也都是以发号器为标签.它的主要目的确如平常大家理解的“为一个分布式系统的数据objec ...
- ASP.NET关于Login控件使用,LoginView 控件,CreateUserWizard 控件
原文:ASP.NET关于Login控件使用,LoginView 控件,CreateUserWizard 控件 Login控件它是属于Membership服务的一部分,必须配置Membership提供程 ...
- 一个带动画的页面底部的TabBar的实现
有时有这样一个需求,页面底部有几个图标能够点击,假设一个screenWidth显示不下这些图标,则这一列图标最后一个是more,点击more,能够通过动画展示两列图标 这样来增加layout中: &l ...
- android windows 上JNI编程
昨天学习windows上的JNI编程,JNI说白了就是java和c语言的一个互相沟通的桥梁.java能够调用JNI来完毕调用C语言实现的方法. JNI的全称是(Java native interfac ...
- hihocoder第42周 k*N骨牌覆盖(状态dp+矩阵快速幂)
上周的3*N的骨牌,因为状态只有8中,所以我们可以手算出状态转移的矩阵 但是这周是k*N,状态矩阵不好手算,都是我们改成用程序自动生成一个状态转移的矩阵就行了,然后用这个矩阵进行快速幂即可 枚举枚举上 ...
- linux sort,uniq,cut,wc命令详解 (转)
sort sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些文件连接起来,并当作一个文件进行排序. sort语法 ...
- Google 开源项目的风格指南
谷歌C++代码风格指南.农业所需的代码.更难得的是不FQ,决定性的最爱!! . http://zh-google-styleguide.readthedocs.org/en/latest/google ...