# 【DP】【P4539】 [SCOI2006]zh_tree

2019/01/02 19:05

## Hint

$1~\leq~n~\leq~30~,~0~\leq~d_i~\leq~200​$

## Solution

\begin{align} ans~ & =~\min~{~\sum_{i = 1}^{n} h_i~\times~f_i}\ & =~\min~{\sum_{i = 1}^n~h_i~\times~\frac{d_i}{S}}\ & =~\min~{\frac{1}{S}~\times~sum_{i = 1}^n~h_i~\times~d_i}\ & =~\min~{\frac{1}{S}~\times~\sum_{i = 1}^n~[k~(r_i~+~1)~c]~\times~d_i\ & =~\min~{\frac{1}{S}~\times~\sum_{i = 1}^n~[k~(r_i~+~1)~d_i]~+~\sum_{i = 1}^n d_i~\times~c}\ & =~\min~{\frac{1}{S}~\times~k~\sum_{i = 1}^n~[(r_i~+~1)~d_i]~+~S~\times~c}\ \end{align}

\begin{align} ans~ & =~\frac{1}{S}~+~S~\times~c~+~\min{k~\sum_{i = 1}^n~(r_i~+~1)~d_i}\ & =~\frac{1}{S}~+~S~\times~c~+~k~\times~\min{\sum_{i = 1}^n~(r_i~+~1)~d_i} \end{align}

## Code

#include <cstdio>
#include <cstring>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long

typedef long long int ll;

namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
}

template <typename T>
inline void qr(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}

template <typename T>
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
}

namespace OPT {
char buf[120];
}

template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
rg int top=0;
do {OPT::buf[++top] = x % 10 + '0';} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
}

const int maxn = 35;

int n;
double c, k;
double MU[maxn], frog[maxn][maxn], sum[maxn];

int main() {
freopen("1.in", "r", stdin);
memset(frog, 127, sizeof frog);
for (rg int i = 1; i <= n; ++i) {
frog[i][i] = sum[i]; frog[i + 1][i] = 0;
sum[i] += sum[i - 1];
}
frog[1][0] = 0;
for (rg int len = 1; len < n; ++len) {
for (rg int l = 1; l < n; ++l) {
int r = l + len;
if (r > n) break;
for (rg int i = l; i <= r; ++i) frog[l][r] = std::min(frog[l][r], frog[l][i - 1] + frog[i + 1][r]);
frog[l][r] += sum[r] - sum[l - 1];
}
}
printf("%.3lf\n", (k * frog[1][n] + sum[n] * c) / sum[n]);
}


0
0 收藏

0 评论
0 收藏
0