博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU - 3516 Tree Construction
阅读量:6288 次
发布时间:2019-06-22

本文共 1214 字,大约阅读时间需要 4 分钟。

思路:

平行四边形不等式优化dp

:)

代码:

#pragma GCC optimize(2)#pragma GCC optimize(3)#pragma GCC optimize(4)#include
using namespace std;#define y1 y11#define fi first#define se second#define pi acos(-1.0)#define LL long long#define LD long double//#define mp make_pair#define pb push_back#define ls rt<<1, l, m#define rs rt<<1|1, m+1, r#define ULL unsigned LL#define pll pair
#define pli pair
#define pii pair
#define piii pair
#define pdd pair
#define mem(a, b) memset(a, b, sizeof(a))#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//headconst int N = 1e3 + 5;int dp[N][N], s[N][N], x[N], y[N], n;int main() { while(~scanf("%d", &n)) { for (int i = 1; i <= n; ++i) scanf("%d %d", &x[i], &y[i]); for (int i = n; i >= 1; --i) { dp[i][i] = 0; s[i][i] = i; for (int j = i+1; j <= n; ++j) { dp[i][j] = 0x3f3f3f3f; for (int k = s[i][j-1]; k <= s[i+1][j]; ++k) { if(k+1 <= j && dp[i][k]+dp[k+1][j]+y[k]-y[j]+x[k+1]-x[i] < dp[i][j]) { dp[i][j] = dp[i][k]+dp[k+1][j]+y[k]-y[j]+x[k+1]-x[i]; s[i][j] = k; } } } } printf("%d\n", dp[1][n]); } return 0;}

 

转载于:https://www.cnblogs.com/widsom/p/10955045.html

你可能感兴趣的文章