sgu 183 分类: sgu 2015-03-08 11:09 41人阅读 评论(0) 收藏
动态规划
开始不会做,
看了Owaski的题解才会的
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#define INF (1<<30)-1
const int MAXN = 10005, MAXM = 105;
int c[MAXN] = {0} , n , m;
//f(i,j) = min{f(k,i)} + c(j) //j-i < m i-k < m
int f[MAXM][MAXM] = {0};
#define mod(a) (a%MAXM)
int main()
{
int ans = INF;
#ifndef ONLINE_JUDGE
freopen("sgu183.in","r",stdin);
freopen("sgu183.out","w",stdout);
#endif
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++)
{
scanf("%d",&c[i]);
f[0][i] = c[i];
}
for(int i = 1; i < n; i ++)
for(int j = std::min(i + m -1, n); j > i; j --)
{
f[mod(i)][mod(j)] = INF;
for(int k = std::max(j - m,0); k < i ; k ++)
{
f[mod(i)][mod(j)] = std::min(f[mod(i)][mod(j)],f[mod(k)][mod(i)] + c[j]);
}
}
for(int i = std::max(0 ,n - m + 1); i < n; i ++)
for(int j = i + 1 ; j <= n ; j++)
{
ans = std::min(ans ,f[mod(i)][mod(j)]);
}
printf("%d",ans);
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
}