Set D (I, j) to the minimum length of the tree connecting the point I to the J point, there is a state transition equation:
D (i, j) = min{D (i, K) + D (k + 1, j) + P[k].y-p[j].y + p[k+1].x-p[i].x}
Then we use the quadrilateral inequality to optimize it.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <map>6 #defineMP Make_pair7 #defineX First8 #defineY Second9 using namespacestd;Ten Onetypedef pair<int,int>PII; A - Const intMAXN = ++Ten; - Const intINF =0x3f3f3f3f; the intN; - - PII P[MAXN]; - + intD[MAXN][MAXN], S[MAXN][MAXN]; - + intMain () A { at while(SCANF ("%d", &n) = =1&&N) - { - for(inti =1; I <= N; i++) scanf ("%d%d", &p[i].x, &p[i].y); - for(inti =1; I <= N; i++) - { -S[i][i +1] =i; inD[i][i +1] = p[i+1].x-p[i].x + p[i].y-p[i+1].y; - } to + for(intL =3; L <= N; l++) - { the for(inti =1; i + L-1<= N; i++) * { $ intj = i + L-1;Panax NotoginsengD[I][J] =INF; - for(intK = s[i][j-1]; K <= s[i+1][J]; k++) the { + intt = d[i][k] + d[k+1][J] + p[k].y-p[j].y + p[k+1].x-p[i].x; A if(T <D[i][j]) the { +D[I][J] =T; -S[I][J] =K; $ } $ } - } - } the -printf"%d\n", d[1][n]);Wuyi } the - return 0; Wu}
code June
Optimization of HDU 3516 DP quadrilateral Inequalities Tree construction