HDU 1227 Fast Food DP *

75 篇文章 0 订阅
70 篇文章 2 订阅

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

状态很好定义,关键是怎么递推,加入一个仓库后的影响怎么算出来

思路:http://www.cnblogs.com/jackge/archive/2013/03/27/2984463.html

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=200+5;
const int INF=0x3f3f3f3f;
int rest[maxn],d[maxn][maxn],cost[maxn][maxn];
int inline getCost(int x,int y)
{
	if(cost[x][y]) return cost[x][y];

	int mid=(x+y)/2;
	int sum=0;
	for(int i=x;i<=y;i++)
		sum+=abs(rest[i]-rest[mid]);
	return cost[x][y]=sum;
}
int main(int argc, char const *argv[])
{
	int n,k,kase=0; 
	while(cin>>n>>k,(n+k))
	{
		memset(d,INF,sizeof(d));
		memset(cost,0,sizeof(cost));

		for(int i=1;i<=n;i++)
			scanf("%d",&rest[i]);

		for(int i=1;i<=k;i++)
			for(int j=1;j<=n;j++){
				if(i==1) d[1][j]=getCost(1,j);
				else for(int m=i-1;m<=j-1;m++)
					d[i][j]=min(d[i-1][m]+getCost(m+1,j),d[i][j]);
			}
		printf("Chain %d\nTotal distance sum = %d\n\n",++kase,d[k][n]);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值