【HDU6024】Building Shops

meaning of the title

   There are n classrooms lined up in a row, and each classroom has a coordinate. Now, Xiao Q wants to build some candy stores in these n classrooms. The total cost is divided into two parts. It costs ci to build a candy store in classroom i. For P without any candy store, the cost is the distance from the classroom to the candy store on its left. How to build a candy store to minimize the cost? n<=3000.

analyze

  Compared with the obvious dp, each classroom has two choices, building a candy classroom or not building a candy classroom. f[i][0] The minimum cost when the i-th classroom does not build a candy store. f[i][1] The minimum cost of building a candy store in the ith classroom . The complexity of direct transfer is O(N^3). We can preprocess the sum of all i,j direct distances. Well, just sauce~

  By the way, it is said that the ltx boss on the field converted this into a tree dp? ? ?

  Metaphysics: When submitting on vj, G++ can AC, but C++ can WA

  

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 
 6 using namespace std;
 7 const int maxn=3000+10;
 8 const int INF=1e14;
 9 struct Node{
10     long long x,c;
11     bool operator <(const Node& rhs)const{
12         return x<rhs.x;
13     }
14 }node[maxn];
15 int n;
16 long long f[maxn][3],sum[maxn][maxn];
17 int main(){
18     while(scanf("%d",&n)!=EOF){
19         for(int i=1;i<=n;i++){
20             scanf("%lld%lld",&node[i].x,&node[i].c);
21         }
22         sort(node+1,node+1+n);
23         memset(sum,0,sizeof(sum));
24         for(int i=1;i<=n;i++){
25                 sum[i][i]=0;
26             for(int j=i+1;j<=n;j++){
27                 sum[i][j]=sum[i][j-1]+node[j].x-node[i].x;
28             }
29         }
30        /* for(int i=1;i<=n;i++){
31             for(int j=i+1;j<=n;j++){
32                 printf("%d--%d:%d\n",i,j,sum[i][j]);
33             }
34         }*/
35         for(int i=1;i<=n;i++){
36             f[i][0]=f[i][1]=INF;
37         }
38         f[1][1]=node[1].c;
39         for(int i=2;i<=n;i++){
40             f[i][1]=min(f[i-1][0],f[i-1][1])+node[i].c;
41             f[i][0]=f[i-1][1]+node[i].x-node[i-1].x;
42             for(int j=1;j<i;j++){//在j处建
43                f[i][0]=min(f[i][0],f[j][1]+sum[j][i]);
44             }
45         }   
46         long long ans=min(f[n][1],f[n][0]);
47         printf("%lld\n",ans);
48       //  for(int i=1;i<=n;i++){
49         //    printf("%d:%lld %lld\n",i,f[i][0],f[i][1]);
50         //}
51     }
52 return 0;
53 }
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324895848&siteId=291194637