D (I, j) represents the minimum cost required to cover the former J villages with the I post Office
Then there is a state transition equation: D (i, j) = min{D (i-1, K) + W (k+1, J)}
Where the value of W (i, J) can be preprocessed.
The following is the code for optimization of quadrilateral inequalities:
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5 using namespacestd;6 7 Const intMAXP = -+Ten;8 Const intMAXV = -+Ten;9 Const intINF =0x3f3f3f3f;Ten One intN, M; A - intA[MAXV], SUM[MAXV]; - intD[MAXP][MAXV], S[MAXP][MAXV]; the - intWintXinty) - { - intT = (x + y)/2; + return(t-x) * a[t]-(sum[t-1]-sum[x-1]) + (Sum[y]-sum[t])-(Y-T) *A[t]; - } + A intMain () at { - while(SCANF ("%d%d", &n, &m) = =2) - { - for(inti =1; I <= N; i++) scanf ("%d", A +i); - for(inti =1; I <= N; i++) Sum[i] = sum[i-1] +A[i]; - inmemset (D,0x3f,sizeof(d)); - for(inti =1; I <= N; i++) {d[1][i] = W (1, i); s[1][i] =0; } to for(inti =2; I <= m; i++) + { -s[i][n+1] =N; the for(intj = N; J > i; j--) * { $ for(intK = s[i-1][J]; K <= s[i][j+1]; k++)Panax Notoginseng { - if(d[i-1][k] + W (k +1, j) <D[i][j]) the { +S[I][J] =K; AD[I][J] = d[i-1][k] + W (k +1, j); the } + } - } $ } $printf"%d\n", D[m][n]); - } - the return 0; -}
code June
POJ 11,604 Edge shape inequality optimization dp Post Office