Optimization of HDU 3516 DP quadrilateral Inequalities Tree construction

Source: Internet
Author: User

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

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.