poj1160Post Office(DP)

http://poj.org/problem?id=1160

算水过的吧 四重循环没优化 CZ说爆可过 就爆了

dp[i][j] = min(dp[i][j],dp[i-1][g]-s) 第i个点建在第j个村庄上 s 是这个点比上个点少的距离

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<stdlib.h>
 6 #include<cmath>
 7 using namespace std;
 8 #define N 1010
 9 #define INF 0xfffffff
10 int h[N],dp[40][N];
11 int main()
12 {
13     int i,j,k,n,m,g;
14     scanf("%d%d",&n,&m);
15     for(i = 1; i <= n; i++)
16     scanf("%d",&h[i]);
17     for(i = 1; i <= n ; i++)
18     {
19         int s = 0;
20         for(j = 1; j  <= n ; j++)
21         s+=abs(h[j]-h[i]);
22         dp[1][i] = s;
23     }
24     for(i = 2 ; i <= m ; i++)
25         for(j = i ; j <= n ; j++)
26         {
27             dp[i][j] = INF;
28             for(g = i-1; g < j ; g++)
29             {
30                 int s=0,ss=0;
31                 for(k = g+1 ; k < j; k++)
32                 {
33                     s+=min(h[k]-h[g],h[j]-h[k]);
34                     ss+=(h[k]-h[g]);
35                 }
36                 dp[i][j] = min(dp[i][j],dp[i-1][g]-ss-(h[j]-h[g])*(n-j+1)+s);
37             }
38         }
39     int minz=INF;
40     for(i = m; i <= n ; i++)
41     minz = min(minz,dp[m][i]);
42     printf("%d\n",minz);
43     return 0;
44 }
View Code

 

posted @ 2013-08-15 22:52  _雨  阅读(191)  评论(0编辑  收藏  举报