Poj 1160 Post Office

Source: Internet
Author: User

Poj 1160 Post Office

I don't know about optimization. I only know the most violent method. O (V ^ 3) is actually less than Ms.

When DP [I] [J] [k] is set, it indicates that J post offices are put in consideration of the first I town, and the town where the last post office is located is KBefore KThe minimum distance between all towns and the nearest post office (Tm made such a detour for itself ..)

So there is DP [I] [J] [I] = min {DP [I-1] [J-1] [k] + dis [k] [I]} Where k <I

DP [I] [J] [k] = DP [I-1] [J] [k]

In this example, DIS [k] [I] indicates the sum of the minimum values of all towns in the [K, I] range from post office K to post office I, that is:

Dis [I] [J] = sum {min (X [k]-X [I], X [J]-X [k])} I <= k <= J

Considering the boundary condition, for DP [I] [J] [K], since one of the first towns is not put, then K's getting any value is illegal (because I represent 2333333 of the status ..), therefore, for any I, consider the case where j = 1 on one side, that is, DP [I] [1] [k] = dis [0] [K], default X [0] =-INF

The final answer is: min {DP [v] [p] [k] + dis [k] [v]} p <= k <= V

The rest of the above logic is correct, and I submitted it happily. tm actually told me that the memory has exceeded 300 × 300 × 30. How can this problem be solved !!! TM memory only gives 10000, there is no way to compress the size of the first dimension to 2 (because the current I is only related to the I-1 ..)

79 Ms, O (V ^ 3), I smiled happily

 1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype>10 #include <cstring>11 #include <cstdlib>12 #include <iostream>13 #include <algorithm>14 using namespace std;15 #define INF 10000000016 #define inf (-((LL)1<<40))17 #define lson k<<1, L, mid18 #define rson k<<1|1, mid+1, R19 #define mem0(a) memset(a,0,sizeof(a))20 #define mem1(a) memset(a,-1,sizeof(a))21 #define mem(a, b) memset(a, b, sizeof(a))22 #define FOPENIN(IN) freopen(IN, "r", stdin)23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)24 template<class T> T CMP_MIN ( T a, T b ) { return a < b;   }25 template<class T> T CMP_MAX ( T a, T b ) { return a > b;   }26 template<class T> T MAX ( T a, T b ) { return a > b ? a : b; }27 template<class T> T MIN ( T a, T b ) { return a < b ? a : b; }28 template<class T> T GCD ( T a, T b ) { return b ? GCD ( b, a % b ) : a; }29 template<class T> T LCM ( T a, T b ) { return a / GCD ( a, b ) * b;       }30 template<class T> T SWAP( T& a, T& b ) { T t = a; a = b;  b = t; }31 32 //typedef __int64 LL;33 typedef long long LL;34 const int MAXN = 255;35 const int MAXM = 110000;36 const double eps = 1e-12;37 38 int V, P;39 int x[310], DP[3][35][310];40 int dis[310][310], pre[310];41 42 void initDis()43 {44         mem0(dis);mem0(pre);45         for(int i=1;i<=V;i++)46         for(int j=i;j<=V;j++)47         for(int k=i;k<=j;k++)48         dis[i][j] += MIN(x[k]-x[i], x[j]-x[k]);49         for(int i=1;i<=V;i++)for(int j=i;j>=1;j--)50                 pre[i] += x[i] - x[j];51 }52 53 int main()54 {55         //FOPENIN ( "in.txt" );56        //FOPENOUT("out.txt");57        while(~scanf("%d %d", &V, &P))58        {59                 for(int i=1;i<=V;i++)60                         scanf("%d", &x[i]);61                 initDis();62                 for(int i=0;i<2;i++)63                 for(int j=0;j<=MIN(i,P);j++)64                 for(int k=0;k<=i;k++){65                         DP[i][j][k] = INF;66                 }67                 int now = 0;68                 for(int i=1;i<=V;i++)69                 {70                         now = !now;71                         int s = 0;72                         for(int j=1;j<=i;j++)   DP[now][1][j] = pre[j];73                         for(int j=2;j<=MIN(i, P); j++)74                         {75                                 DP[now][j][i] = INF;76                                 for(int k=j-1;k<i;k++)if(DP[!now][j-1][k] != INF)77                                 {78                                         DP[now][j][i] = MIN(DP[now][j][i], DP[now][j-1][k] + dis[k][i]);79                                 }80                                 for(int k=j;k<i;k++) DP[now][j][k] = DP[!now][j][k];81                         }82                 }83                 int ans = INF;84                 for(int k=P;k<=V;k++)if(DP[now][P][k] != INF)85                 {86                         int s = 0;87                         for(int j = k + 1; j <= V; j ++ ) s += x[j] - x[k];88                         ans = MIN(ans, DP[now][P][k] + s);89                 }90                 printf("%d\n", ans);91        }92         return 0;93 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.