voj1242 邮局问题

voj1242 邮局问题
#include < cstdio >
using   namespace  std;

#define  INF 0xFFFFFFF
int  N,M;
int  a[ 310 ];
int  f[ 310 ][ 31 ];
int  p[ 310 ][ 310 ];
void  init()
{
    scanf(
" %d%d " , & N, & M);
    
for ( int  i = 1 ;i <= N; ++ i)
        scanf(
" %d " ,a + i);
    
for ( int  i = 0 ;i <= N; ++ i)
        
for ( int  j = 0 ;j <= M; ++ j)
            f[i][j]
= INF;
    f[
0 ][ 0 ] = 0 ;
}
void  solve()
{
    
for ( int  j = 1 ;j <= N; ++ j){
        p[j][j]
= 0 ;
        
for ( int  i = j - 1 ;i >= 1 ; -- i)
            p[i][j]
= p[i + 1 ][j] + a[(i + j + 1 ) / 2 ] - a[i];
    }
    
for ( int  i = 1 ;i <= N; ++ i)
        
for ( int  j = 1 ;j <= M; ++ j)
            
for ( int  k = 0 ;k < i; ++ k)
                
if (f[i][j] > f[k][j - 1 ] + p[k + 1 ][i])
                    f[i][j]
= f[k][j - 1 ] + p[k + 1 ][i];
    printf(
" %d\n " ,f[N][M]);
}
int  main()
{
    init();
    solve();
    
return   0 ;
}




你可能感兴趣的:(voj1242 邮局问题)