脑子进水了,犯了个巨简单的错误,调了半天~~
建议先考虑只有一个depot时候的情况。
Accepted | 1196 | C++ | 00:00.13 | 576K |
#include<stdio.h>
#include <string.h>
#include <math.h>
int f[201][31], d[200], c[200][200 ];
int solve(int n, int k)
{
memset(c, 0, sizeof (c));
memset(f, 0x7f, sizeof (f));
for(int i = 1; i <= n; i++ )
scanf("%d", & d[i]);
for(int i = 1; i < n; i ++ )
for(int j = i + 1; j <= n; j++ )
for(int p = i; p <= j; p++ )
c[i][j] += fabs((float)d[p] - d[(i + j + 1) / 2 ]);
for(int i = 1; i <= k; i++ )
f[i][i] = 0 ;
for(int i = 1; i <= n; i++ )
f[i][1] = c[1 ][i];
for(int i = 1; i <= n; i++ )
for(int j = 2; j <= k && j <= i; j++ )
for(int p = j - 1; p < i; p++ )
if(f[i][j] > f[p][j - 1] + c[p + 1 ][i])
f[i][j] = f[p][j - 1] + c[p + 1 ][i];
return f[n][k];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1196.txt", "r" , stdin);
#endif
int t = 0 , n, k;
while(scanf("%d%d", &n, &k) != EOF && !(n == 0 && k == 0 ))
printf("Chain %d/nTotal distance sum = %d/n/n", ++ t, solve(n, k));
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0 ;
}
#include <string.h>
#include <math.h>
int f[201][31], d[200], c[200][200 ];
int solve(int n, int k)
{
memset(c, 0, sizeof (c));
memset(f, 0x7f, sizeof (f));
for(int i = 1; i <= n; i++ )
scanf("%d", & d[i]);
for(int i = 1; i < n; i ++ )
for(int j = i + 1; j <= n; j++ )
for(int p = i; p <= j; p++ )
c[i][j] += fabs((float)d[p] - d[(i + j + 1) / 2 ]);
for(int i = 1; i <= k; i++ )
f[i][i] = 0 ;
for(int i = 1; i <= n; i++ )
f[i][1] = c[1 ][i];
for(int i = 1; i <= n; i++ )
for(int j = 2; j <= k && j <= i; j++ )
for(int p = j - 1; p < i; p++ )
if(f[i][j] > f[p][j - 1] + c[p + 1 ][i])
f[i][j] = f[p][j - 1] + c[p + 1 ][i];
return f[n][k];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1196.txt", "r" , stdin);
#endif
int t = 0 , n, k;
while(scanf("%d%d", &n, &k) != EOF && !(n == 0 && k == 0 ))
printf("Chain %d/nTotal distance sum = %d/n/n", ++ t, solve(n, k));
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0 ;
}